• 【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

    Sample Output 1:

    31

    Sample Input 2:

    5 888 666 666 888 888

    Sample Output 2:

    None

    解题思路:

    题目要求的意思是找到给出数字中只出现过一次的数字,如果这种数字不止一个,则按照输入顺序,输出第一个只出现一次的数字。

    这种问题就用map将数字和出现次数对应起来就可以了。然后再循环输入的数字,如果出现次数为1就直接输出。

    易错点:

    1. 要输出第一个只出现一次的数字

    代码:

    1. #include
    2. using namespace std;
    3. int main(){
    4. int N;
    5. cin>>N;
    6. map<int,int> number;
    7. int a[N];
    8. for(int i=0;i
    9. cin>>a[i];
    10. number[a[i]]++;
    11. }
    12. for(int i=0;i!=N;i++){
    13. if(number[a[i]]==1){
    14. cout<
    15. return 0;
    16. }
    17. }
    18. cout<<"None";
    19. return 0;
    20. }

  • 相关阅读:
    Vue.js3学习篇--Vue组件的属性和方法
    Java 基础复习 Day 24
    Xss跨站脚本攻击
    leveldb安装
    矿山自动驾驶技术点分析
    Sentinel服务保护
    探索k8s集群的存储卷 emptyDir hostPath nfs
    cmake 代码版本管理
    【深度学习实验】前馈神经网络(六):自动求导
    SpringMvc之Json&全局异常处理
  • 原文地址:https://blog.csdn.net/weixin_55202895/article/details/126438428