uva 10474

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n,q,x, s[1000],casek=0;
 9     while(~scanf("%d%d",&n,&q))
10     {
11         printf("CASE#%d:
",++casek);
12         for(int i=0;i<n;i++)
13             scanf("%d",s+i);
14         sort(s,s+n);
15         while(q--)
16         {
17             scanf("%d",&x);
18             int c;
19             c=lower_bound(s,s+n,x)-s;//查找大于等于x的第一个位置
20             if(s[c]==x)
21                 printf("%d found at %d
",x,c+1);
22             else
23                 printf("%d no found
",x);
24         }
25     }
26     return 0;
27 }

 用vector 不定长数组存放数据的

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 vector <int> s;
 7 int main()
 8 {
 9     int n,q,x,casek=0,d;
10     while(~scanf("%d%d",&n,&q))
11     {
12         printf("CASE#%d:
",++casek);
13         s.clear();
14         for(int i=0;i<n;i++)
15         {
16             scanf("%d",&d); s.push_back(d);
17         }
18         sort(s.begin(),s.end());
19         while(q--)
20         {
21             scanf("%d",&x);
22             int c;
23             c=lower_bound(s.begin(),s.end(),x)-s.begin();
24             if(s[c]==x)
25                 printf("%d found at %d
",x,c+1);
26             else
27                 printf("%d no found
",x);
28         }
29     }
30     return 0;
31 }
原文地址:https://www.cnblogs.com/WDKER/p/5470513.html