대학생 자바 좀 도와주세요 ㅠㅠ

대학생 자바 좀 도와주세요 ㅠㅠ

작성일 2023.04.04댓글 1건
    게시물 수정 , 삭제는 로그인 필요

public class BinarySearch { 
public static void binarySearch(int iKey, int arr[]) { 
int mid; 
int left = 0; 
int right = arr.length - 1; 

while (right >= left) { 
mid = (right + left) / 2; 
if (iKey == arr[mid]) { System.out.println(mid); break; } 
else if (iKey < arr[mid]) right = mid -1; 
else left = mid + 1; } } 
public static void main(String[] args) { 
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 
binarySearch(2, arr); 
}
여기에서 

아래 정렬된 정수 배열에 대해 사용자로부터 정수값을 입력 받아 해당 값이 들어있는 곳의 인덱스를 출력하시오.



   int[] arr = { 1, 2, 3, 4, 6, 7, 8};



실행 예)

  검색 값?  2

  비교회수 : 2회


이렇게 수정하라는데 잘 모르겠어요 ㅠ


#대학생 자바 프로젝트 #대학생 자발적 아싸

profile_image 익명 작성일 -

단순히 비교횟수만 출력하는거면... count 선언하면 되지 않나요?

import java.util.Scanner; public class BinarySearch { public static void binarySearch(int iKey, int arr[]) { int mid = 0; int left = 0; int right = arr.length - 1; int count = 0; while (right >= left) { mid = (right + left) / 2; count++; if (iKey == arr[mid]) { break; } else if (iKey < arr[mid]) { right = mid - 1; } else { left = mid + 1; } } System.out.printf("비교회수 : %d회\n", count); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; System.out.print("검색 값? "); int num = sc.nextInt(); binarySearch(num, arr); sc.close(); } }

자바 좀 도와주세요....ㅠㅠ

... 자바 잘하시는 분들 도와주세요..ㅜㅜ 1. 밑에 코드를 참고하여 좌석 예약 프로그램을 완성하시오. public class question1 { public static...

자바 좀 도와주세요....ㅠㅠ

... 자바 고수님들~ 제발 도와주세요....자세한 풀이 부탁드립니다. package test5; class Shared { int sum, i; Shared() { sum=0; i=1; } public void sharedM...

자바 좀 도와주세요....ㅠㅠ

... Book("자바", 150, 2016), new Book("파이썬", 100, 2019), new Book("자바스크립트", 200, 2018)... 에디터와 자바 컴파일러는 모두 저가...