在数组中查找一个数

| October 30, 2007 14:04 | timmy | Via Original
输入一个正整数repeat (0输入一个正整数n (1例:括号内是说明
输入
2   (repeat=2)
5   (n=5)
5 2 7 2 9  (输入的5个整数)
2   (x=2)
4   (n=4)
3 2 7 9  (输入的4个整数)
10  (x=10)
输出
1           (2对应的最小下标是1)
Not found   (没有找到10)

[code]
#include
int main( )
{
  int ri, repeat;
  int i, index, n, x;
  int a[10];

  scanf("%d", &repeat);
  for(ri=1; ri<=repeat; ri++){
     scanf("%d", &n);
     for(i=0; i         scanf("%d", &a[i]);
     scanf("%d", &x);
    index=-1;
    for(i=0;i      if(a[i]==x){        
        index=i;
        break;
      }
     if(index!=-1)
        printf("%d\n", index);
     else
        printf("Not found\n");
  }
}
[\code]
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(248)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember