题目描述
知识点
STL
我的实现
码前思考
题目中说了一定存在,那么每次判断就好了,水题!
代码实现
#include <iostream>
#include <map>
using namespace std;
int main() {
int m, n;
scanf("%d %d", &m, &n);
map<int, int> arr;
int half = m * n / 2;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int temp;
scanf("%d", &temp);
arr[temp]++;
if(arr[temp] > half) {
printf("%d", temp);
return 0;
}
}
}
return 0;
}
码后反思
感觉自己去年写的代码有点挫,多写了好多不必要的东西:
#include<stdio.h>
#include<map>
using namespace std;
int main(){
map<int,int> mp;
int m,n;
scanf("%d %d",&m,&n);
int maxKey;
int maxNum;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int now;
scanf("%d",&now);
if(mp.find(now) != mp.end()){
mp[now] += 1;
}else{
mp[now] = 1;
}
if(mp[now] > m*n/2){
maxKey = now;
break;
}
}
}
printf("%d",maxKey);
return 0;
}