n个人报数找到只有一个报过且最小的数。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #define INF 0x7fffffff 8 #define LEN 1000100 9 using namespace std; 10 11 struct P{ 12 char name[50]; 13 int num; 14 }man[LEN]; 15 16 bool cmp(struct P a, struct P b){ 17 return a.num<b.num; 18 } 19 20 int main() 21 { 22 // freopen("in.txt", "r", stdin); 23 24 int T, n; 25 scanf("%d", &T); 26 while(T--){ 27 scanf("%d", &n); 28 for(int i=1; i<=n; i++){ 29 scanf("%s%d", man[i].name, &man[i].num); 30 } 31 sort(man+1, man+n+1, cmp); 32 int ans = -1, loc = man[1].num, cnt=1; 33 for(int i=2; i<=n; i++){ 34 if(man[i].num == loc){ 35 cnt++; 36 } 37 else { 38 if(cnt == 1){ 39 ans = i-1; 40 break; 41 }else{ 42 loc = man[i].num; 43 cnt = 1; 44 } 45 } 46 } 47 if(cnt == 1 && loc == man[n].num) ans = n; 48 if(ans!=-1)printf("%s ", man[ans].name); 49 else printf("Nobody wins. "); 50 } 51 return 0; 52 }