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