江苏网友来相约

江苏的网友,不如来这里相约吧,我是江苏省海门市人,这里也许能找到你知心朋友。

103 成员713 主题4,022 回复0 张照片

请教高手-实验四的问题

@
@南方
2001-02-01 14:40
有15个数存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中的第几个元素的值。如果该数不在数组中,则输出“无此数”。要找的数用scanf函数输入。 #include #define N 15 main() {int i,j,number,top,bott,min,loca,a[N],flag; char c; printf("输入15个数(a[i]>a[i-1])\n"); scanf("%d",&a[0]); i=1; while(i=a[i-1]) i++; else {printf("请重新输入a[]"); printf("必须大于%d\n",a[i-1]); } } printf("\n"); for(i=0;ia[N-1]) loca=-1; while((loca==0)&&(top<=bott)) { min=(bott+top)/2; if(number==a[min]) { loca=min; printf("%d位于表中第%d个数\n",number,loca+1); } else if(numbera[i-1]) 1↙ 2↙ 3↙ 4↙ 5↙ 6↙ 7↙ 8↙ 9↙ 11↙ 12↙ 23↙ 34↙ 45↙ 56↙ 1 2 3 4 5 6 7 8 9 11 12 23 34 45 56 请输入查找数据.67 无此数. 是否继续查找? Y/N!请输入查找数据. 问题:为什么没还没选择Y或N,就输出了“请输入查找数据”
新年好!
L
luck8888_cn
2001-02-12 22:42
我的解答
#include #define N 15 main() {int i,j,number,top,bott,min,loca,a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, flag,true=0; char c; flag=1; /*printf("输入15个数(a[i]>a[i-1]):\n"); scanf("%d",&a[0]); i=1; while(i=a[i-1]) i++; else {printf("请重新输入a[]:"); printf("必须大于%d:\n",a[i-1]); } } */ printf("\n"); for(i=0;ia[N-1]) loca=-1; while((loca==0)&&(top<=bott)) { min=(bott+top)/2; if(number==a[min]) { loca=min; true=1; printf("%d位于表中第%d个数:\n",number,loca+1); break; } else if(number
@
@南方
2001-02-16 01:01
谢谢luck8888_cn
新年好!
@
@南方
2001-02-16 05:17
再请教
我试过了,一切正常。为什么加了空操作就好。
1 / 1

登录后回复。