C 查找数的位置

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 int search(int key,int a[], int length);//函数声明 
 6 int main() {
 7     int  a[]={1,2,3,4,5,6,7,8,9,0,13,68,99};
 8     int x,b;
 9     printf("请输入您要找的数\n");
10     scanf("%d",&x);
11     b=search(x,a,sizeof(a)/sizeof(a[0]));
12     if(b==-1){
13         printf("没有您要找的数");
14     } 
15     else{
16         printf("你要找的数在%d",b);
17     } 
18     return 0;
19 }
20 int search(int key,int a[],int length){
21     int m=-1,i;
22     for(i=0;i<length;i++){
23     
24     if(key==a[i]){
25         m=i+1;
26         break;
27     }    
28     }
29     return m;
30 }

测试结果:

猜你喜欢

转载自www.cnblogs.com/Catherinezhilin/p/10272352.html
今日推荐