渴望春天247 2021-06-21 21:29 采纳率: 84.6%
浏览 8

求解答,这是怎么回事?

#include<stdio.h>
#define N 15
void main()
{
	int a[N];
	int i,num,top,bott,mid,loca,flag=1,sign;
	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 again:\n");
	}
	printf("\n");
	for(i=0;i<N;i++)
		printf("%5d",a[i]);
	printf("\n");
	while(flag)
	{	printf("input number to look for:");
		scanf("%d",&num);
		sign=0;
		top=0;
		bott=N-1;
		if((num<a[0])||(num>a[N-1]))
			loca=-1;
		while((!sign)&&(top<=bott))
		{mid=(top+bott)/2;
		if(num==a[mid])
			{loca=mid;
			printf("has found %d ,its position is %d\n",num,loca+1);
			sign=1;
			}
		else if(num<a[mid])
			bott=mid-1;
		else
			top=mid+1;
		}
	if(!sign||loca==-1)
		printf("can't find %d.\n",num);
	printf("continue or not(Y/N)?\n");
	scanf("%c",&c);
	if(c=='N'||c=='n')
		flag=0;
	}
}


结果是:

enter data:
1 3 2
enter again:
4 5 6 8 12 23 34 44 45 56 57 58 68

    1    3    4    5    6    8   12   23   34   44   45   56   57   58   68
input number to look for:7
can't find 7.
continue or not(Y/N)?
input number to look for:12
has found 12 ,its position is 7
continue or not(Y/N)?
input number to look for:y
has found 12 ,its position is 7
continue or not(Y/N)?
input number to look for:n
has found 12 ,its position is 7
continue or not(Y/N)?
Press any key to continue

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-29 19:23
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算