C程序输出和题意不相符,问修改?

jasmine 2011-05-11 02:40:32
题目:有15个数按由小到大的顺序存放在一个数组中。输入一个数,要求用折半查找法找出该数是数组中第几个元素的值,
如果该数不在数组中,输出“不在表中”。
要求:在我的程序上进行修改

#define N 15
#include<stdio.h>
void main()
{int number ,top,bott,mid,a[N],sign,flag=1,i=0,loca;
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 data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");

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

system("pause");
}
...全文
133 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
striving_boy 2011-05-12
  • 打赏
  • 举报
回复
还是加个断点,跑一遍吧。。。
如此美丽的你 2011-05-12
  • 打赏
  • 举报
回复
不好意思看错了哈!应该是这样的!
if(!sign||loca==-1) printf("can not find %d.\n ",number);
printf("continue or not(Y/N)?\n");
fflush(stdin);[/color[color=#00FF00]]//注清除缓冲区,因为你输完15个数据后按了一下回车,如果不清除缓冲区
scanf("%c",&c); //则相当于把回车赋给了这里的scanf
if(c=='N'||c=='n')
flag=0;
}
yezhiyaoni 2011-05-11
  • 打赏
  • 举报
回复
没错呀。
如此美丽的你 2011-05-11
  • 打赏
  • 举报
回复
while(flag)
{ printf("the number to look for is:\n");
scanf("%d",&number);
top=0;bott=N-1;sign=0;
if(number<a[0]||number>a[N-1]) loca=-1;
while((!sign)&&(top<=bott))
{mid=(top+bott)/2;
if(number==a[mid]) {
loca=mid;
printf("Has found %d,the position is %d.\n",number,loca+1);
sign=1;
}
else if(number<a[mid]) top=mid-1;//这里错了哈!!!
else bott=mid+1;
}
if(!sign||loca==-1) printf("can not find %d.\n ",number);
printf("continue or not(Y/N)?\n");
scanf("%c",&c);
if(c=='N'||c=='n')
flag=0;
}
newfarmerchi 2011-05-11
  • 打赏
  • 举报
回复

while(flag)
{ printf("the number to look for is:\n");
scanf("%d",&number);
getchar();//<------在这里加个他
杨子aaaaaa 2011-05-11
  • 打赏
  • 举报
回复
if(!sign||loca==-1) printf("can not find %d.\n ",number);
getchar();///这里增加一个接受字符的内容。
printf("continue or not(Y/N)?\n");
HeartWasNot 2011-05-11
  • 打赏
  • 举报
回复
这用的就是折半查找法么。。。只要你记下元素出现的下标即可
赵4老师 2011-05-11
  • 打赏
  • 举报
回复
使用bsearch函数
或者参考bsearch源代码:c:\Microsoft SDK\src\crt\bsearch.c
/***
*bsearch.c - do a binary search
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines bsearch() - do a binary search an an array
*
*******************************************************************************/

#include <cruntime.h>
#include <stdlib.h>
#include <search.h>

/***
*char *bsearch() - do a binary search on an array
*
*Purpose:
* Does a binary search of a sorted array for a key.
*
*Entry:
* const char *key - key to search for
* const char *base - base of sorted array to search
* unsigned int num - number of elements in array
* unsigned int width - number of bytes per element
* int (*compare)() - pointer to function that compares two array
* elements, returning neg when #1 < #2, pos when #1 > #2, and
* 0 when they are equal. Function is passed pointers to two
* array elements.
*
*Exit:
* if key is found:
* returns pointer to occurrence of key in array
* if key is not found:
* returns NULL
*
*Exceptions:
*
*******************************************************************************/

void * __cdecl bsearch (
REG4 const void *key,
const void *base,
size_t num,
size_t width,
int (__cdecl *compare)(const void *, const void *)
)
{
REG1 char *lo = (char *)base;
REG2 char *hi = (char *)base + (num - 1) * width;
REG3 char *mid;
size_t half;
int result;

while (lo <= hi)
if (half = num / 2)
{
mid = lo + (num & 1 ? half : (half - 1)) * width;
if (!(result = (*compare)(key,mid)))
return(mid);
else if (result < 0)
{
hi = mid - width;
num = num & 1 ? half : half-1;
}
else {
lo = mid + width;
num = half;
}
}
else if (num)
return((*compare)(key,lo) ? NULL : lo);
else
break;

return(NULL);
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧