题目

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. 新概念英语三 新东方主讲Lesson1

    新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...

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

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

  3. Mac更新系统后提示xcrun error

    更新mac os后无法运行git,gcc等命令,出现missing xcrun错误. 这是因为更新系统后会卸载xcode,重新安装后会缺失xcode的命令行工具(command line tools) ...

  4. Pyqt5_QmainWindow

    QmainWindow Toolbar() QMenuBar() StatusBar() ******************************************************* ...

  5. redis配置文件.conf和常用配置

    1,配置文件在哪 2,Units单位 1 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 2 对大小写不敏感 3,INCLUDES包含 和我们的spring配置文件类似,可 ...

  6. Java8中的Lambda表达式

    Lambda是什么 Lambda表达式,也可称为闭包,是java8的新特性,作用是取代大部分内部类,优化java代码结构,让代码变得更加简洁紧凑. Lambda的基本语法 (expression)-& ...

  7. 用Linux感觉低效吗?来看看这几个技巧!

      Linux已经成为目前最火的操作系统之一,尽管现在的Linux用户很多,但很多使用Linux的同学发现,他们在Linux下的工作效率并不高,那么这是为什么呢?其实使用Linux也可以很舒适,通过一 ...

  8. [COCOS2DX-LUA]0-001.利用ClippingNode实现放大镜功能

    用过Iphone的都知道,Iphone在定位光标位置的时候会把内容进行放大,这样我们就能很快的把光标移动到指定的位置.那么在我们的Cocos2dX的游戏中,怎么可以实现这种功能呢.起先我就是想起了Ip ...

  9. Binlog2sql+CentOS7 离线安装

    Binlog2sql+CentOS7 离线安装 目录 Binlog2sql+CentOS7 离线安装 1. 环境 2. 下载 3.1 Pip 安装 3.2 PyMySQL/mysql-replicat ...

  10. fastjson漏洞利用备忘

    预备知识: 起rmi服务 用marshalsec-0.0.3-SNAPSHOT-all.jar起一个rmi服务. java -cp marshalsec-0.0.3-SNAPSHOT-all.jar ...