蓝桥杯练习题K

来源:http://oj.mofriend.net/problem.php?cid=1203&pid=10

题目描述
An array of length n, with address from 1 to n inclusive(包含), contains entries from the set {1,2,…,n-1} and there’s exactly two elements with the same value. Your task is to find out the value.
输入

Input contains several cases. Each case includes a number n (1

输出
Your must output the value for each case, one per line

1
2
3
4
5
6
7
8
样例输入
2
1 1
4
1 2 3 2
样例输出
1
2

思路:
题目意思找到2个相同的数…超简单,就是英文的而已
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
int n;
int a[2048];
while (cin >> n) {
memset(a, 0, 2048);
for (size_t i = 0; i < n; i++) {
int b;
cin >> b;
a[b]++;
}
for (size_t i = 0; i < n; i++) {
if (a[i] == 2) {
cout << i << endl;
break;
}
}
}
return 0;
}