hdu1564 找出特殊数(map模板)

Find your present!

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.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<=200, and n is odd) at first. Following that, n positive integers will be given in a line. 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
2

题意:rt
思路:特殊数必然是odd,所以可以用异或法,也可以记录数字出现的频率是否为1
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 2e2+10;

using namespace std;


int main()
{
    int n,a;
    map<int,int>hash;
    map<int,int >::iterator li;
    while(scanf("%d",&n)!=EOF&&n){
        hash.clear();
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            hash[a]++;
        }
        for(li=hash.begin();li!=hash.end();li++){
            if(li->second==1){
                printf("%d
",li->first);
                break;
            }
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}
map
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 2e2+10;

using namespace std;


int main()
{
    int n,a,t;
    //map<int,int>hash;
    //map<int,int >::iterator li;
    while(scanf("%d",&n)!=EOF&&n){
        //hash.clear();
        t = 0;
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            t ^= a;
        }
        /*for(li=hash.begin();li!=hash.end();li++){
            if(li->second==1){
                printf("%d
",li->first);
                break;
            }
        }*/
        cout<<t<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}
异或
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 2e2+10;

using namespace std;
int a[MAXN],b[MAXN];//b[]第一次出现的数字位置记录该数字出现的频率

int main()
{
    int n;
    int j;
    //map<int,int>hash;
    //map<int,int >::iterator li;
    while(scanf("%d",&n)!=EOF&&n){
        //hash.clear();
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            for(j=0;j<i;j++){
                if(a[i]==a[j]){
                    b[j]++;
                    break;
                }
            }
            if(j==i)
                b[j]++;
        }
        /*for(li=hash.begin();li!=hash.end();li++){
            if(li->second==1){
                printf("%d
",li->first);
                break;
            }
        }*/
        for(int i=0;i<n;i++){
            if(b[i]==1){
                cout<<a[i]<<endl;
                break;
            }
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}
特殊的方法
在一个谎言的国度,沉默就是英雄
原文地址:https://www.cnblogs.com/EdsonLin/p/5490843.html