问题链接(https://codeforces.com/problemset/problem/1191/B)
问题描述
有数字1到9的3种类型s、p、m的麻将。其中有三种定义, koutsu是3个麻将完全相同(如1m、1m、1m),shuntsu是类型相同的有3个连续相邻数字的麻将(如1m、2m、3m),mentsu包含了前面两种定义,也就是koutsu、shuntsu都算是mentsu。
现手里有三个麻将,问最少还要添加多少个麻将能满足mentsud的条件。
问题分析
题意很简单,但是如果真要用条件判断去找结果会很麻烦,因为情况很多,容易漏,如果列错了也不好发现。
所以对于koutsu,我是用map去做,字符串对应其出现的次数,遍历(仅3个值),找它们的最大值maxn1;
对于shuntsu,我是在输入的时候,用数组去记录出现的牌的值记为1,没出现过的牌为0,然后遍历一遍(左中右的值相加),找到最大值maxn2。
再从maxn1和maxn2中找出最大的就好了。
注意shuntsu那部分,很容易会忘记考虑得到的牌是不连续相邻的,但可以加一个就实现shuntsu的情况,如:1s,3s,9s,再加一个2s就可以了。
程序如下
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
map<string,int> Q;
int i,j,maxn1,maxn2,count[3][15]={0};
string temp;
for(i=0;i<3;i++){
cin>>temp;
if(temp[1]=='s') count[0][temp[0]-'0']=1;
else if(temp[1]=='p')count[1][temp[0]-'0']=1;
else if(temp[1]=='m')count[2][temp[0]-'0']=1;
Q[temp]++;
}
map<string,int>::iterator p=Q.begin();
maxn1=0;
while(p!=Q.end()){
maxn1=max(maxn1,p->second);
p++;
}
maxn2=0;
for(i=0;i<3;i++){
for(j=1;j<=9;j++){
int temp=count[i][j+1]+count[i][j]+count[i][j-1];
maxn2=max(maxn2,temp);
}
}
cout<<(3-max(maxn1,maxn2))<<endl;
return 0;
}