题目

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:

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

题目解读

给出N个正整数,找出第一个只出现了一次的数字,比如 5 31 5 88 67 88 1731,67,17都只出现了一次,但是31是第一个,所以输出31;如果没有唯一的数字,输出 None

思路很简单:利用一个整型数组统计每个数字出现的次数,找出第一个次数为1的数字并输出。

因为这些数字本身在输入中是无序的,因此不能直接用数字做下标,次数做值,这样会导致结果错误,比如上面那个例子 5 31 5 88 67 88 17,若用数字本身做下标,17会排在前面,最后会输出17.

因此设计两个数组num[]保存出现按顺序的这些数字,count[]保存这些数字出现的次数,最后只需要这样遍历:

    // 判断第一个只出现了一次的数字
for(int i = 0; i < n; i++) {
if(count[num[i]] == 1) {
printf("%d", num[i]);
return 0;
}
}

num[]本身按顺序读取输入并存储保证了数字的有序性。

完整代码

#include <cstdio>
using namespace std; int num[100000], count[100000]; int main() {
int n;
scanf("%d", &n);
int x;
for(int i = 0; i < n; i++) {
// 当前数字
scanf("%d", &num[i]);
// 当前数字出现的次数
count[num[i]]++;
}
// 判断第一个只出现了一次的数字
for(int i = 0; i < n; i++) {
if(count[num[i]] == 1) {
printf("%d", num[i]);
return 0;
}
}
// 都重复,输出 None
printf("None");
return 0;
}

PAT 1041 Be Unique (20分)利用数组找出只出现一次的数字的更多相关文章

  1. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  2. PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

    1041 Be Unique (20 分)   Being unique is so important to people on Mars that even their lottery is de ...

  3. PAT Advanced 1041 Be Unique (20 分)

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  4. 【PAT甲级】1041 Be Unique (20 分)(多重集)

    题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". AAAAAccepte ...

  5. 1041 Be Unique (20分)(水)

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  6. PAT甲 1041. Be Unique (20) 2016-09-09 23:14 33人阅读 评论(0) 收藏

    1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being uniqu ...

  7. 一起来刷《剑指Offer》——不修改数组找出重复的数字(思路及Python实现)

    数组中重复的数字 在上一篇博客中<剑指Offer>-- 题目一:找出数组中重复的数字(Python多种方法实现)中,其实能发现这类题目的关键就是一边遍历数组一边查满足条件的元素. 然后我们 ...

  8. 【Java】 剑指offer(2) 不修改数组找出重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...

  9. 《剑指offer》第五十六题(数组中唯一只出现一次的数字)

    // 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...

随机推荐

  1. for循环中break和continue的区别

    break 会立即退出循环,强制执行循环后面的语句 默认只会终止紧邻的循环,如果要终止其他循环,需要给循环起名字 例如: name:for(var i = 0; i < 5; i++){ for ...

  2. class.getFields和class.getDeclareFields的区别

    class.getFields的定义 返回类提供的public域包括超类的共有变量; 注: 是public,我们平时定义变量一般用的private,如果用getFields是不会获得. class.g ...

  3. live2d web端加载moc3模型

    大佬博客链接:https://blog.csdn.net/weixin_44128558/article/details/104792345 照着大佬的博客做一下,可以先学会怎么生成bundle.js ...

  4. 9.4 Go 数据格式

    9.4 Go 数据格式 1.1. msgpack格式 官方msgpack官网用一句话总结: It’s like JSON. but fast and small. 简单来讲,它的数据格式与json类似 ...

  5. Django之from.Form内置字段

    from django import forms Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内 ...

  6. jsp 判断时间大小

    jsp 判断时间大小 <% SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date ...

  7. C:习题2

    C 语言中的数据类型主要有哪些? C 语言为什么要规定对所有用到的变量“先定义后使用”?这样做有什么好处? 1. 编译系统会根据定义为变量分配内存空间,分配空间的大小与数据类型有关 2. 系统可以根据 ...

  8. 王玉兰201771010128《面象对象程序设计(Java)》第九周学习总结

    第一部分:理论基础部分总结: 一:(1)异常:在程序的执行过程中所发生的异常事件,它中断指令的正常执行. 常见的几种错误:A:用户输入错误:B:设备错误;硬件出错:C:物理限制:磁盘满了,可用存储空间 ...

  9. vue项目中使用bpmn-番外篇(留言问题总结)

    前情提要 “vue项目中使用bpmn-xxxx”系列的七篇文章在上周已经更新完成,发表后,有小伙伴在使用时提出了一些文章中没有讲到的问题,此篇作为番外篇,将大家提出的共性问题解答一下,欢迎大家支持原创 ...

  10. JUC整理笔记三之测试工具jcstress

    并发测试工具Jcstress使用教程 Jcstress 全称 Java Concurrency Stress,是一种并发压力测试工具,可以帮助研究JVM.java类库和硬件中并发的正确性. Wiki地 ...