问题:
--------------------------------------------------------------------------------
Time Limit:1s Memory limit:64M
--------------------------------------------------------------------------------
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>
int main()
{
int n,*data,p;
while (scanf("%d",&n)!=EOF)
{
data = (int *)malloc(sizeof(int)*n);
while(n--)
{
scanf("%d",&p);
if(*(data+p) == 1)
{
printf("%d\n",p);
break;
}
*(data + p) = 1;
}
free(data);
}
return 0;
}
执行时间总是2s,有哪位dx知道如何优化这段代码的,或者自己写一个高效率的实现代码出来的,
望高手指点啊!!!