水题,找出出现次数最多的数字,若多个输出Nobody
//#include <bits/stdc++.h> //using namespace std; #include <cstdio> //stdio.h #include <cstring> //string.h #include <algorithm> const int N = 1e3 + 5; struct Hash { int num, cnt; bool operator < (const Hash &r) const { return cnt < r.cnt; } }h[N]; //bool cmp(const Hash &a, const Hash &b) { //更规范的写法 bool cmp(Hash a, Hash b) { return a.cnt < b.cnt; //按照计数cnt从小到大排 } int main(void) { int T; scanf ("%d", &T); while (T--) { int n; scanf ("%d", &n); memset (h+1, 0, sizeof (h)); for (int x, i=1; i<=n; ++i) { scanf ("%d", &x); h[x].num = x; h[x].cnt++; } //std::sort (h+1, h+1+1000, cmp); //cmp比较函数写在结构体外面 std::sort (h+1, h+1+1000); //结构体重载运算符 '<' if (h[1000].cnt == h[999].cnt) puts ("Nobody"); else printf ("%d ", h[1000].num); } return 0; }