| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 745 人关注过本帖
标题:[原创]求助:一个有关循环的问题
取消只看楼主 加入收藏
mazy
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-9-7
收藏
 问题点数:0 回复次数:0 
[原创]求助:一个有关循环的问题

有5个数按从大到小的顺序存放在一个数组中,输入一个数,要求按折半查找法找出该数组中的第几个元素的值。如果该数不在数组中,则打印出“无此表”。

程序如下:

#include<stdio.h> #define N 5

main() { int i,number,top,bott,mid,loca,a[N],flag=0,sign=1; char c; printf("Enter data:\n"); scanf("%d",&a[0]); i=1; while(i<N) { scanf("%d",&a[i]); if(a[i]>=a[i-1]) i++; else printf("Enter the number angain:"); } printf("\n");

for(i=0;i<N;i++) printf("%4d",a[i]); printf("\n"); flag=1; while(flag) { printf("Input the number to look for:"); scanf("%d",&number); loca=0; top=0; bott=N-1; if(number<a[0]||number>a[N-1]) flag=-1; while((sign==1)&&(top<=bott)) { mid=(bott+top)/2; if(number==a[mid]) { loca=mid; printf("Find %d ,its position is %d\n",number,loca+1); sign=0; } else if(number<a[mid]) bott=mid-1; else bott=mid+1; } if((sign==1)||loca==-1) printf("%d is not found .\n",number); printf("Continue or not(Y/N)?"); scanf("%c",&c); if(c=='N'||c=='n') flag=0; } }

这是其中一个的结果:

Enter data: 1 2 3 4 5

1 2 3 4 5 Input the number to look for:3 Find 3 ,its position is 3 Continue or not(Y/N)?Input the number to look for:

我的问题是:怎么样使“Continue or not(Y/N)?”这个循环有效,还有就是不要后面那句“Input the number to look for:”。

请高手指点。

搜索更多相关主题的帖子: 循环 有关 问题 
2005-10-08 20:56
快速回复:[原创]求助:一个有关循环的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011633 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved