复习版
#include <stdio.h>
#include <stdlib.h>
#define not !
#define and &&
#define or ||
#define ri register int
#define rep(inc, frm, to) for (ri inc = frm; inc < (to); ++inc)
#define rep2(inc, frm, to) for (ri inc = frm; inc > (to); --inc)
#define setbit(x, y) x |= 1 << y
#define clrbit(x, y) x &= ~(1 << y)
#define getbit(x, y) x >> y & 1
#define flipbit(x, y) x ^= 1 << y
#define show_bin(x) { rep2(i, 31, ~0) printf("%d%c", getbit(x, i), i ? 9 : 10); }
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define SWAP(a, b) { typeof(a) tmp = a; a = b; b = tmp; }
typedef long long int ll;
#define r() fast_read()
int is_digit(const char c) {
return c >= 48 and c <= 57;
}
ll fast_read(void) {
ll n = 0, sign = 1;
char c = getchar();
while (not is_digit(c)) {
if (c == '-') sign = ~0;
c = getchar();
}
while (is_digit(c)) {
n = (n << 1) + (n << 3) + (c ^ 48);
c = getchar();
}
return sign * n;
}
#define MAX_N 100010
int cnts[(int) 1E4 + 1], nums[MAX_N];
signed main(int argc, char const *argv[]) {
// freopen("test.in", "r", stdin);
int n = r();
rep(i, 0, n) {
*(nums + i) = r();
++*(cnts + *(nums + i));
}
rep(i, 0, n) {
if (*(cnts + *(nums + i)) == 1)
return printf("%d\n", *(nums + i)), ~~(0 ^ 0);
}
// fclose(stdin);
return puts("None"), ~~(0 ^ 0);
}
不使用HashMap的解法
#include <iostream>
using namespace std;
const int N = 1E5 + 100;
int n, a[N], cnts[10001];
int main(int argc, char const *argv[]) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
++cnts[a[i]];
}
for (int i = 0; i < n; ++i) {
if (cnts[a[i]] == 1) {
printf("%d\n", a[i]);
return 0;
}
}
puts("None");
return 0;
}
HashTable 解法
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
unordered_map<int, int> freq;
int main(void) {
int n;
cin >> n;
vector<int> nums;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
++freq[x];
nums.emplace_back(x);
}
int ans = -1;
for (const auto& x : nums) {
if (freq[x] == 1) {
ans = x;
break;
}
}
if (ans != -1) printf("%d\n", ans);
else puts("None");
}
真不错
标题不错