求大神来指点下,这道题目难住我好几天了
题目是这样我的输出是这样
但是正确输出应该是这样
我的是输入一个 输出一个
但是应该输入多个 输出多个
我的代码
程序代码:
#include<stdio.h> typedef struct { int key; }JD; int zheban() { int low,high,mid,found,i,data; //low是下界,high是上界,mid是中间位置 low=1; found=0; //found是查找标志:找到为1;没找到为0 int j=0; JD r[50]; for(i=0;i<50;i++) { scanf("%d",&r[i]); j++; if(getchar()=='\n') break; } scanf("%d",&data); high=j-1; while((low<=high)&&(found==0)) { mid=(low+high)/2; if(data>r[mid].key) { low=mid+1; if(low>high) printf("%d",mid+1); else printf("%d ",mid+1); } else if(data==r[mid].key) {printf("%d",mid+1); found=1;} else { high=mid-1; if(low>high) printf("%d",mid+1); else printf("%d ",mid+1);} } if(found==1) printf("\n%d\n",mid+1); else printf("\n0\n"); return 0; } void main() { while(1){ zheban(); if(getchar()=='\n')continue; else break; } }