C 언어 절반 찾기

7530 단어

#define _CRT_SECURE_NO_WARNINGS
#include
#include
int Dichotomy(int Arr[],int size,int tofind) {
	int left = 0;
	int reght = size - 1;
	while (left <= reght) {
		int mid = (left + reght) / 2;
		if (Arr[mid] > tofind)	
			reght = mid - 1;
		else if (Arr[mid] < tofind)
			left = mid + 1;
		else if (Arr[mid] == tofind)
			return mid;
	}
	return -1;
}
int main() {
	int A, key;
	int Arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
	int size = sizeof(Arr) / sizeof(Arr[0]);
	while (1) {
		printf("     0-9    

"
); scanf("%d", &key); A = Dichotomy(Arr, size, key); if (A>=0) printf(" :%d

"
, A); else { printf("-1


"
); } } system("pause"); return 0; }

좋은 웹페이지 즐겨찾기