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.
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
Output
For each case, output an integer in a line, which is the card number of your present.
Sample Input
5 1 1 3 2 2 3 1 2 1 0
Sample Output
3 2use scanf to avoid Time Limit ExceededHintHint解题报告: 题意就是有一一些数字,个数是奇数个。you can assume that only one number appear odd times 这句话是关键,也就是说只有一个数字会出现奇数次,其他都只会出现偶数次。一看就是用异或很容易解决(不了解异或的请戳链接http://blog.csdn.net/scarlett_geng/article/details/47979435)我的想法是先用sort把所有的数排一下,奇数个位置的数与前面的异或值,不是0的话,则前面那个数一定是出现奇数次的数。下面是自己ac的代码:#include <cmath> #include <ctime> #include <cctype> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX / 10) #define clr(arr, val) memset(arr, val, sizeof(arr)) #define pb push_back #define sz(a) ((int)(a).size()) using namespace std; typedef set<int> si; typedef vector<int> vi; typedef map<int, int> mii; typedef pair<int, int> pii; typedef long long ll; const double esp = 1e-5; #define N 1000000+10 long long int a[N]; int main() { int n,i; long long int count; while(~scanf("%d",&n)&&n!=0) { for(i=0;i<n;i++) { scanf("%lld",&a[i]); } sort(a,a+n); count=a[0]; //printf("*****"); for(i=0;i<n;i++) { if(i!=0) count^=a[i]; if(i%2==1) { if(count!=0) break; } } printf("%lld\n",a[i-1]); } return 0; }
还有一个更简单的代码,这里一定要注意最初进行异或的值如sum或者count不要设成0或者1,一开始就是这个地方错了,要赋值为第一个数:#include <cmath> #include <ctime> #include <cctype> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX / 10) #define clr(arr, val) memset(arr, val, sizeof(arr)) #define pb push_back #define sz(a) ((int)(a).size()) using namespace std; typedef set<int> si; typedef vector<int> vi; typedef map<int, int> mii; typedef pair<int, int> pii; typedef long long ll; const double esp = 1e-5; #define N 50100 int main() { int i,n; long long int a; while(~scanf("%d",&n)&&n!=0) { scanf("%lld",&a); long long int sum=a; n--; while(n--) { scanf("%lld",&a); sum^=a; } printf("%lld\n",sum); } return 0; }
还有一种算法是a[i]!=a[i-1]&&a[i]!=a[i+1],这个很好理解,既不等于右边的,也不等于左边的,代码很简单,就不上了。