标签:
http://acm.hdu.edu.cn/showproblem.php?pid=2095
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.
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.
For each case, output an integer in a line, which is the card number of your present.
5
1 1 3 2 2
3
1 2 1
0
3
2
哈希
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 1000007; const int INF = 0x3f3f3f3f; int arr[N]; struct Hash_Set { int tot, num[N], head[N], next[N]; inline void init(int n) { tot = 0; rep(i, n + 1) head[i] = -1; } inline void insert(int val) { int u = val % N; num[tot] = u, next[tot] = head[u], head[u] = tot++; } inline bool find(int val) { int tot = 0, u = val % N; for(int i = head[u]; ~i; i = next[i]) { if(num[i] == val) tot++; if(tot >= 2) return true; } return false; } }hash; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int n; while(~scanf("%d", &n), n) { hash.init(n); rep(i, n) { scanf("%d", &arr[i]); hash.insert(arr[i]); } rep(i, n) { if(!hash.find(arr[i])) { printf("%d\n", arr[i]); break; } } } return 0; }
stl
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 1000007; const int INF = 0x3f3f3f3f; map<int, int> A; map<int, int>::iterator it; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int n, v; while(~scanf("%d", &n), n) { rep(i, n) { scanf("%d", &v); A[v]++; } for(it = A.begin(); it != A.end(); ++it) { if(it->second == 1) { printf("%d\n", it->first); break; } } A.clear(); } return 0; }
hdu 2095 find your present (2)
标签:
原文地址:http://www.cnblogs.com/GadyPu/p/4792779.html