首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

输入10个数排序,其后输入1个数,在10个数中找到和这是数相同的数

2014-01-12 
输入10个数排序,然后输入1个数,在10个数中找到和这是数相同的数#includestdio.hvoid main(){int a[10]i

输入10个数排序,然后输入1个数,在10个数中找到和这是数相同的数
#include<stdio.h>
void main()
{int a[10];
int i,j,t,z;
printf("input 10numbers :\n");
for(i=0;i<10;i++)
  scanf("%d",&a[i]);
printf("\n");
for(j=0;j<=9;j++)
 for(i=0;i<=9-j;i++)
 if(a[i]<a[i+1])
 {t=a[i];a[i]=a[i+1];a[i+1]=t;}
 printf("the sorted numbers :\n");
 for(i=0;i<10;i++)
 printf(" %d",a[i]);
 printf("\n");int z,mid,top=9,bottom=0;
scanf("%d",&z);
for(;top<=bottom;)
{mid=(top+bottom)/2;
 if(a[mid]==z)
 {printf("%d",mid);
 break;}
 else if(a[mid]<z)
     bottom=mid+1;
 else top=mid-1;
 }
 if(top<=bottom)
 printf("not find\n");
}有错误,请大神查找下错误在哪
[解决办法]
#include<stdio.h>
void main()
{
int a[10];
int i,j,t;
printf("input 10 numbers :\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<=9;j++)
for(i=0;i<=9-j;i++)
if(a[i]>a[i+1])/////冒泡排序
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
printf("the sorted numbers :\n");
for(i=0;i<10;i++)
printf(" %d\t",a[i]);
printf("\n");

int key,mid,top=9,bottom=0;
printf("输入要查找的字\n");
scanf("%d",&key);
for(;bottom<top;)//这里的两个标针逻辑关系你弄反了
{
mid=(top+bottom)/2;
if(a[mid]==key)
{
printf("%d",mid);
break;
}
else if(a[mid]<key)
bottom=mid+1;
else 
top=mid-1;
}
if(top<bottom)
printf("not find\n");
}


//楼主有两个错误,一个是将小于号写成大于号,另一个是将大于号写成小于号

热点排行