前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Be Unique

【PAT甲级】Be Unique

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:10:07
3340
发布2019-11-08 14:10:07
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/89045673

Problem Description:

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,10​4​​]. 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:

代码语言:javascript
复制
7 5 31 5 88 67 88 17

Sample Output 1:

代码语言:javascript
复制
31

Sample Input 2:

代码语言:javascript
复制
5 888 666 666 888 888

Sample Output 2:

代码语言:javascript
复制
None

解题思路:

题目简单来说就是一句话:“输出第一个只出现一次的数”,这肯定直接无脑map啊。然而在我对map进行无脑for-each来输出第一个只出现一次的数的时候我发现我错了,C++的map会自动根据key值来进行升序排列,它并不是像Java的LinkedHashMap一样能够按照输入的顺序来排列。这就很尴尬了,那只能够对输入的序列来进行遍历,然后输出第一个只出现一次的数。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int a[N];
    map<int,int> m;
    for(int i = 0; i < N; i++)
    {
        cin >> a[i];
        m[a[i]]++;        
    }
    bool flag = true;
    //不能这样直接无脑for-each,因为C++的map会自动根据key值来升序排列,它不像Java的LinkedHashMap一样是能按照输入的顺序排列的
    // for(auto it : m)
    // {
    //     if(it.second == 1)
    //     {
    //         cout << it.first << endl;
    //         flag = false;
    //         break;
    //     }
    // }
    for(int i = 0; i < N; i++)
    {
        if(m[a[i]] == 1)   //输出第一个只出现过一次的数
        {
            cout << a[i] << endl;
            flag = false;
            break;
        }
    }
    if(flag)
    {
        cout << "None" << endl;
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input 1:
  • Sample Output 1:
  • Sample Input 2:
  • Sample Output 2:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档