折半查找法 谭浩强习题

#include
#include
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;
}

 

你可能感兴趣的:(折半查找法 谭浩强习题)