首页 > 代码库 > 大理石在哪里UVa 10474

大理石在哪里UVa 10474

我自己写的代码

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int N,a[100],b[100],Q,flag;
    int k=1;
    while(cin>>N>>Q)
    {
        for(int i=0;i<N;i++)
        {
            cin>>a[i];
        }
        for(int j=0;j<Q;j++)
        {
            cin>>b[j];
        }
        sort(a,a+N);

        for(int j=0;j<Q;j++)
        {
            flag=0;
            cout<<"CASE# "<<k++<<endl;
           for(int i=0;i<N;i++)
           {
               if(a[i]==b[j])
               {
                   cout<<b[j]<<" found at "<<i+1<<endl;
                   flag=1;
                   break;
               }
           }
           if(flag==0)
           {
               cout<<b[j]<<" not found"<<endl;
           }
        }
    }
    return 0;
}

书上写的代码

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int N,a[100],Q,x;
    int k=1;
    while(cin>>N>>Q)
    {
        for(int i=0;i<N;i++)
        {
            cin>>a[i];
        }
        sort(a,a+N);
        cout<<"CASE# "<<k++<<endl;
        while(Q--)
        {
            cin>>x;
            int p=lower_bound(a,a+N,x)-a;//大于等于x的第一个数的下标
            if(a[p]==x)
                cout<<x<<" found at "<<p+1<<endl;
            else
                cout<<x<<" not found"<<endl;
        }

    }
    return 0;
}

哎,自己还是菜的不行啊

大理石在哪里UVa 10474