# PAT 1041 Be Unique (20分)利用数组找出只出现一次的数字

### 题目

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`

### 题目解读

```    // 判断第一个只出现了一次的数字
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;
}```

0 条评论

• ### PAT 1023 Have Fun with Numbers (20分) 字符数组解决大整数存储溢出

Notice that the number 123456789 is a 9-digit number consisting exactly the numb...

• ### PAT 1017 Queueing at Bank (25分) prioriry_queue

Suppose a bank has K windows open for service. There is a yellow line in front o...

• ### PAT 1021 Deepest Root (25分) 从测试点3超时到满分再到代码优化

A graph which is connected and acyclic can be considered a tree. The height of t...

• ### PAT（乙级）1019

分析：这个题目，没什么难度。但是我被超时问题困扰了一会儿，可能是scanf函数用的次数有点多，所以改了一下，直接通过了。

• ### 探索 | 神经网络到底是如何思考的？MIT精英们做了这么一个实验室来搞清楚

作者 | Larry Hardesty等 编译 | ziqi Zhang 没错！人工智能是很火，神经网络也很火，但你真的懂它吗？神经网络到底是怎么工作的？没有...

• ### LintCode-569.各位相加

相加的过程如下：3 + 8 = 11，1 + 1 = 2。因为 2 只剩下一个数字，所以返回 2。

• ### 通过反汇编理解函数调用机制（x86和ARM）

在分析上面的汇编程序之前，需要了解rbp、rsp为栈基址寄存器、栈顶寄存器，分别指向栈底和栈顶；edx、eax、esi、edi均为x86CPU上的通用寄存器，...

• ### Leetcode_342_Power of Four

http://blog.csdn.net/pistolove/article/details/52198328

• ### 各位相加，直到得到一个一位的整数

从1开始，结果依次是1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9····