UVA 10474

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 10474相关的知识,希望对你有一定的参考价值。

题意:给你一组数,再给几个数问是否在一组数中。

题很简单: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;
}

 

以上是关于UVA 10474的主要内容,如果未能解决你的问题,请参考以下文章

Uva10474

D - Where is the Marble? (UVA - 10474)

UVA 10474

uva10474

UVa10474

uva 10474 Where is the Marble?(简单题)