当前位置: 代码迷 >> 综合 >> Problem 1001 Duplicate Pair
  详细解决方案

Problem 1001 Duplicate Pair

热度:40   发布时间:2024-01-08 23:52:07.0

Problem Description
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
Input contains several cases.
Each case includes a number n (1< n<=10^6), which is followed by n integers.
The input is ended up with the end of file.

Output
Your must output the value for each case, one per line.

Sample Input
2
1 1
4
1 2 3 2

Sample Output
1
2

#include"stdio.h"
#include"string.h"
int main()
{int n;const int maxn=1e6;int ai[maxn+7];int bi[maxn+7];
while(scanf("%d",&n)==1)
{memset(bi,0,sizeof(bi));for(int i=0;i<n;i++){scanf("%d",&ai[i]);bi[ai[i]]++;}for(int i=1;i<n;i++){if(bi[i]==2){printf("%d\n",i);break;}}
}return 0;
}
  相关解决方案