codeup Be Unique (20) (C++)

Title Description

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.

Entry

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.

Export

For each test case, print the winning number in a line. If there is no winner, print "None" instead.

Sample input  Copy

7 5 31 5 88 67 88 17
5 888 666 666 888 888

Sample output Copy

31
None

Timeout Code: habits of the drawbacks of using container. . Old thinking applied to simulate the process vessel, ignoring the results of the automatic ordering problem map; then use an analog structure, but ignoring the n <= 5 to 10 times, a timeout condition occurs. Finally, the simplest one-dimensional array to simulate, bucket sort collected directly, and then sequentially determine the number of one-dimensional array of elements is 1, is directly output, and then break; if still found traversed (i.e. ans = 0) , the output none. . . . (Obviously very simple ... but do not know about how the brain circuits may still own .... too much food ...) is out of the bottom of the code. . Very simple. . .

#include<bits/stdc++.h>
using namespace std;
struct num{
	int number;
	int times;
	num(){
		number = 0;
		times = 0;
	}
};
int main(){
	int n;
	while(scanf("%d",&n) != EOF){
		num m[n];
		int num;
		for(int i = 0; i < n; i++){
			scanf("%d",&num);
			for(int i = 0; i < n; i++){
				if(m[i].number == 0){
					m[i].number = num;
					m[i].times++;
					break;
				}else if(m[i].number == num){
					m[i].times++;
					break;
				}
			} 
		}
		int ans = 0;
		for(int i = 0; i < n; i++){
			if(m[i].times == 1){
				ans = m[i].number;
				break;
			}
		}
		if(ans == 0) printf("None\n");
		else printf("%d\n",ans);
	}
}

Out Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	while(scanf("%d",&n) != EOF){
		int flag[100010] = {0};
		int num[n];
		int res = 0;
		for(int i = 0; i < n; i++){
			scanf("%d",&num[i]);
			flag[num[i]]++;
		}
		int ans = 0;
		for(int i = 0; i < n; i++){
			if(flag[num[i]] == 1){
				ans = num[i];
				break;
			}
		}
		if(ans == 0) printf("None\n");
		else printf("%d\n",ans);
	}
}

 

Published 33 original articles · won praise 2 · Views 1616

Guess you like

Origin blog.csdn.net/qq_38969094/article/details/104336879