Duplicate Pair

Time Limit: 1000 mSec Memory Limit : 65536 KB

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 (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;
}

题意: 输入n个数 这n个数包括1到n-1 所以必然有一个数出现两次 找出这个数

本文链接:https://my.lmcjl.com/post/11522.html

展开阅读全文

4 评论

留下您的评论.