美团4.23笔试第一题C++全A代码
思路就是用矩阵的大小分别减去奇数项和偶数项的某一个出现最多次数的值的出现的次数#include<stdio.h>
#include<map>
#include<set>
using namespace std;
int main(){
int x,y,len1,len2,max1,max2;
len1=len2=max1=max2=0;
int a[100],b[100];
map<int,int> mp1,mp2;
scanf("%d %d",&x,&y);
int bw[x][y];
for(int i=0;i<100;i++){
a[i]=0;
b[i]=0;
}
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
//int index;
scanf("%d",&bw[i][j]);
if((i+j)%2==0){
if(i==0&&j==0){
continue;
}
if(i==0&&j==j-1){
continue;
}
if(i==x-1&&j==0){
continue;
}
if(i==x-1&&j==j-1){
continue;
}
if(mp1.find(bw[i][j])==mp1.end()){
mp1[bw[i][j]]=1;
}else{
mp1[bw[i][j]]++;
}
}else{
if(mp2.find(bw[i][j])==mp2.end()){
mp2[bw[i][j]]=1;
}else{
mp2[bw[i][j]]++;
}
}
}
}
int s1,s2;
map<int,int>::iterator it1,it2;
it1=mp1.begin();
while(it1!=mp1.end()){
if(max1<(it1->second)){
max1=it1->second;
s1=it1->first;
}
it1++;
}
it2=mp2.begin();
while(it2!=mp2.end()){
if(max2<(it2->second)){
max2=it2->second;
s2=it2->first;
}
it2++;
}
if(s1==s2){
if(max1>max2){
max2=0;
it2=mp2.begin();
while(it2!=mp2.end()){
if(max2<(it2->second)&&it2->first!=s1){
max2=it2->second;
s2=it2->first;
}
it2++;
}
}else{
max1=0;
it1=mp1.begin();
while(it1!=mp1.end()){
if(max1<(it1->second)&&it1->first!=s2){
max1=it1->second;
s1=it1->first;
}
it1++;
}
}
}
printf("%d",x*y-max1-max2);
}
#笔试题目#