1041 Be Unique(20 分)

Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,104​​ ]. The first one who bets on a unique number wins. For example, if there are 7 people betting on { 5 31 5 88 67 88 17 }, then the second one who bets on 31 wins.

Input Specification:
Each input file contains one test case. Each case contains a line which begins with a positive integer N (≤10​5​​ ) and then followed by N bets. The numbers are separated by a space.

Output Specification:
For each test case, print the winning number in a line. If there is no winner, print None instead.

Sample Input 1:

7 5 31 5 88 67 88 17

Sample Output 1:

31

Sample Input 2:

5 888 666 666 888 888

Sample Output 2:

None

超时版本

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int v[100001];
    int flag;
    for(int i=0;i<n;i++)
    {
        cin >> v[i];
    }
    for(int j=0;j<n;j++)
    {
        flag = 0;
        for(int k=j+1;k<n;k++)
        {
            if(v[k]==v[j]) {
                flag = 1;
                break;
            }
        }
        for(int m = 0;m<j;m++){
            if(v[m]==v[j]) {
                flag = 1;
                break;
            }
        }
        if(flag==0) {
            cout << v[j];
            break;
        }
    }
    if(flag==1) cout << "None";
    return 0;
}

正确版本

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int v[100001],cnt[10001];
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> v[i];
        cnt[v[i]]++;
    }
    for(int i=0;i<n;i++)
    {
        if(cnt[v[i]]==1) {
            cout << v[i];
            return 0;
        }
    }
    cout << "None";
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,954评论 0 10
  • 今天上午,一老师朋友问我,简便计算:25×44,这道题有好多孩子做错。讲的时候都会做,到考的时候,又有很多孩子做错...
    俞老师阅读 3,286评论 2 9
  • 大雪节令已过,雪却并未见着,期盼了很久的雪,迟迟不来。我在等待中中更为彷徨,焦灼,忧虑伴随孤寂被寒风裹挟而来,塞北...
    傲慢的朱立秋阅读 454评论 0 0
  • 有些时候我们对屏幕的横竖还是很有要求的,比如QQ你不会希望他是横屏的,但是你如果不设置属性就会横屏(当你打开横屏...
    _天马_阅读 329评论 0 0
  • 坐上回西安的动车上,寻思着今天写些什么,思前想后,只好把压箱底的看家本领拿出来和大家分享了,烤肉。从小我就...
    Tom_说阅读 628评论 6 1

友情链接更多精彩内容