69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int main(){
int arr[15] = {3,6,9,14,19,47,49,55,62,91,124,234,398,576,999};
int num,top,bott,mid,flag = 1,loc,sign = 0;
char c;
while(flag){
printf("请输入要查找的数字:");
scanf("%d",&num);
top = 0;
bott = 14;
if(num < arr[top] || num > arr[bott])
loc = -1;
while(!sign && top <= bott){
mid = (top + bott) / 2;
if(num == arr[mid]){
loc = mid;
printf("%d在第%d位置上\n",num,loc + 1);
sign = 1;
}else if(num <= arr[mid]){
bott = mid - 1;
}else{
top = mid + 1;
}
}
if(!sign || loc == -1)
printf("找不到%d\n",num);
printf("是否继续Y/N:");
scanf("%c",&c);
if(c == 'N' || c == 'n')
flag = 0;
}
return 0;
}
//请今后要用
int c;
scanf("%c",&c);
//时,都改为
char s[2];
int c;
scanf("%1s",s);
c=s[0];