PAT甲级--1041 Be Unique(20 分)【Hash】

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

解题思路:这题是用2个数组,第一个数组就是保存输入数据的顺序,第二个数组就是以输入数据为下标++统计,然后我们for循环一下,看一下有没有数量为1的,如果有就输出,并return 0,如果没有return 0,那它继续输出后面的语句,None,这题不要想复杂,这样做简单又快乐。

#include<bits/stdc++.h>
using namespace std;
int a[100010],cnt[100010];
int main(void)
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		cnt[a[i]]++;
	}
	for(int i=0;i<n;i++)
	{
		if(cnt[a[i]]==1) 
		{
			cout<<a[i]<<endl;
			return 0;
		}
	}
	printf("None\n");
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/82389882