码迷,mamicode.com
首页 > 其他好文 > 详细

大理石在哪里UVa 10474

时间:2016-12-25 23:27:46      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:--   end   case   大于   turn   下标   bre   name   nbsp   

我自己写的代码

#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

标签:--   end   case   大于   turn   下标   bre   name   nbsp   

原文地址:http://www.cnblogs.com/hhm324/p/6220689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!