题目描述
题目描述
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
输出例子:
31
大意
买彩票,在1-9999中选择一个数字,如果你的数字只有你选了,而且你是第一个这样的人,输出这个数字。
思路
- 理论上是一道水题,但是我做的时候,发现实情并不简单。
- 第一个问题是容易超时,最初我开的是二维数组,把输入和此输入的个数放在两行,每次输入的时候判断一次是否是表里已经有的数字,这个方法可以过九个点,最后一个测试点怎么也过不了。
- 现在正确的解题思路是:把所有输入放在一个一维数组中,在输出的时候用一个双重循环判断是否独一无二。时间复杂度还是n²,但是能通过了。
- 如下代码:
int a = 10;
char b[a]; //编译不报错
//char b[a] = “jfkd”; //报错,使用变量定义长度时,不可在定义时同时进行初始化赋值,需要在之后进行赋值
原因:在C中,使用变量来定义数组长度是,这个数组可以定义,却不能同时进行初始化赋值,需要在之后赋值。
代码
#include<cstdio>
using namespace std;
int main(){
int N;
scanf("%d",&N);
int num[N];
for(int i=0;i<N;++i){
scanf("%d",&num[i]);
}
for(int i=0;i<N;++i){
int flag=num[i];
int amount=0;
for(int j=0;j<N;++j){
if(num[j]==flag)++amount;
}
if(amount==1){
printf("%d",flag);
return 0;
}
}
printf("%s","None");
return 0;
}