#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int x = 0; printf("请输入一个整数:"); scanf("%d", &x); int arr[] = { 1,2,3,4,5,6,7,8,9 }; int a = sizeof(arr) / sizeof(arr[0]); //计算元素个数 int left = 0; //左下标 int right = a - 1; //右下标 while (left <= right) { int z = (left + right)/2; if (arr[z] > x) { right = z - 1; } else if (arr[z] < x) { left = z + 1; } else { printf("你想的数字%d在数组中",x); break; } } if (left > right) { printf("输入超出范围或输入错误"); } }
标签:arr,right,int,代码,练习,改进,printf,sizeof,left From: https://www.cnblogs.com/ayue6/p/17741146.html