Be Unique

问题 C: Be Unique (20)
时间限制: 1 Sec 内存限制: 32 MB
提交: 386 解决: 182
[提交][状态][讨论版][命题人:外部导入]
题目描述
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.
输入
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.
输出
For each test case, print the winning number in a line. If there is no winner, print “None” instead.
样例输入
7 5 31 5 88 67 88 17
5 888 666 666 888 888
样例输出
31
None
自己代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	static int a[1000000];
	while (scanf("%d", &n) != EOF) {
		static int  count[1000000] = { 0 };
		for (int i = 0; i < 1000000; i++) {
			count[i] = 0;
		}
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
			count[a[i]]++;
		}
		//然后再去找是否唯一
		int jishu = 0;
		for (int i = 0; i < n; i++,jishu++) {
			if (count[a[i]] - 1 == 0) {
				printf("%d\n", a[i]);  break;
			}
		}
		if (jishu == n)printf("None\n");
	}
	return 0;
}
注意:首先一点,直接定义int a[100000]会有内存不足的错误,而加上static即可。在32位系统上,理论上内存访问空间最大也就是从0到0xffffffff(10进制4294967295),数组a的大小是100000×100000×4,已经超过4字节整型所能表达的范围。函数内部定义的数组是存放在栈中的,而一般栈的空间都不大,你定义的数组太大了,使得栈的容量不足,所以不能运行了。建议采用动态申请的方式定义这个数组,
还有一点static int a[100000]={0}好像初始化不同。
这就是static变量的一个性质:初始化只有一次,但是可以多次赋值。

猜你喜欢

转载自blog.csdn.net/qq_35966478/article/details/86655834