原题链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=11
find your present (2) |
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 9566 Accepted Submission(s): 2664 |
Problem Description In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.
|
Input The input file will consist of several cases.
|
Output
|
Sample Input 5 1 1 3 2 2 3 1 2 1 0
|
Sample Output 3 2 Hint Hintuse scanf to avoid Time Limit Exceeded
|
Author 8600
|
Source HDU 2007-Spring Programming Contest - Warm Up (1)
|
Recommend 8600
|
解法:两两异或找不同即可
源代码:
#include<stdio.h>
int main()
{
long long int n,a,temp;
while(~scanf("%lld",&n))
{
if(n==0)
return 0;
scanf("%lld",&a);
for(int i=0;i<n-1;i++)
{
scanf("%lld",&temp);
a = a^temp;
}
printf("%lld\n",a);
}
return 0;
}
作为小白一开始是用全朴素方法写的,结果超时。这个原理是利用逻辑异或的规律,如果两个数相等异或结果为0,两数不等时异或结果为1(同为0,异为1)
关于例题可以参考这个https://blog.csdn.net/gloriaied/article/details/23126875