题意:给你一组数,再给几个数问是否在一组数中。
题很简单:STL入门。
没用到STL。
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std;
int a[];
int main()
{
int ncase = ;
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m) != EOF){
if(n == && m == )
break;
printf("CASE# %d:\n",ncase++);
for(int i = ;i <= n; i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
for(int i = ;i <= m; i++){
int ans;
scanf("%d",&ans);
int flag = false;
int j;
for(j = ;j <= n; j++)
if(a[j] == ans){
flag = true;
break;
}
if(flag)
printf("%d found at %d\n",ans,j);
else
printf("%d not found\n",ans);
}
}
return ;
}
STL。
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std;
int a[];
int main()
{
int ncase = ;
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m) != EOF){
if(n == && m == )
break;
printf("CASE# %d:\n",ncase++);
for(int i = ;i <= n; i++)
scanf("%d",&a[i]);
sort(a+,a+n+);
for(int i = ;i <= m; i++){
int ans;
scanf("%d",&ans);
int flag;
// for(j = 1;j <= n; j++)
// if(a[j] == ans){
// flag = true;
// break;
// }
flag = lower_bound(a+,a+n+,ans) - a; //很容易看出怎么操作的
if(a[flag] == ans)
printf("%d found at %d\n",ans,flag);
else
printf("%d not found\n",ans);
}
}
return ;
}