1. 程式人生 > >折半查詢法 譚浩強習題

折半查詢法 譚浩強習題

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a[15], x;
	puts("請按從大到小輸入15個數:");
		for (int i = 0; i < 15; i++)
			scanf_s("%d", &a[i]);	
			int  low, high, mid;
			for (low = 0, high = 14, puts("輸入一個數:"), scanf_s("%d", &x); low <= high;) 
			{
				mid = (low + high) / 2;
				if (x > a[mid]) high = mid - 1;
				else if (x < a[mid]) low = mid + 1;
				else
				{
					printf("%d is No.%d number!\n", x, mid + 1);
					break;
				}
			}
				if (x != a[mid])
					printf("無此數\n");
			system("pause");
			return 0;
}