码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【PAT甲级 - C++题解】1041 Be Unique


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1041 Be Unique (pintia.cn)
    🔑中文翻译:独一无二
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1041 Be Unique

    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 (≤105) 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
    
    • 1

    Sample Output 1:

    31
    
    • 1

    Sample Input 2:

    5 888 666 666 888 888
    
    • 1

    Sample Output 2:

    None
    
    • 1
    题意

    找出第一个只出现一次的数字。

    思路

    具体思路如下:

    1. 先将所有元素放入数组 a 中,并用哈希表 c 来记录每个元素出现的次数。
    2. 从头往后遍历 a 中元素,输出第一个在 c 中查找数值为 1 的元素,如果不存在则输出 None 。
    代码
    #include
    using namespace std;
    
    const int N = 100010;
    int a[N], c[N];
    int n;
    
    int main()
    {
        cin >> n;
    
        //记录每个数出现的次数
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            c[a[i]]++;
        }
    
        //找到最左边只出现一次的数字
        for (int i = 0; i < n; i++)
            if (c[a[i]] == 1)
            {
                cout << a[i] << endl;
                return 0;
            }
        puts("None");
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    IDEA点击RUN后后台到底执行了什么操作
    MXNet-图像分类(gluon版本)【附源码】
    HTTP 原理与CND原理
    相爱相杀六年,Elastic终与AWS就商标问题达成共识
    13、Efficient Geometry-aware 3D Generative Adversarial Networks
    Element UI + Vue 新增和编辑共用表单校验无法清除问题(已解决)
    深度强化学习中深度Q网络(Q-Learning+CNN)的讲解以及在Atari游戏中的实战(超详细 附源码)
    vmware本机安装虚拟机centos7.*
    R语言编写switch函数进行多分支选择、判断编程:使用switch函数在多种方案中进行选择
    el-upload 上传视频时,动态截取视频第一帧画面作为封面展示
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/128108487
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号