六、查找问题
1.①顺序查找法(在一列数中查找某数x)
基本思想:一列数放在数组a[1]---a[n]中,待查找的数放在x中,把x与a数组中的元素从头到尾一一进行比较查找。用变量p表示a数组元素下标,p初值为1,使x与a[p]比较,如果x不等于a[p],则使p=p+1,不断重复这个过程;一旦x等于a[p]则退出循环;另外,如果p大于数组长度,循环也应该停止。(这个过程可由下语句实现)
void
main()
{ int
a[10],p,x,i;
printf("please
input the array:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("please
input the number you want find:\n");
scanf("%d",&x);
printf("\n");
p=0;
while(x!=a[p]&&p<10)
p++;
if(p>=10)
printf("the
number is not found!\n");
else
printf("the
number is found the no%d!\n",p);
}
思考:将上面程序改