打牌

来源:互联网 发布:pc使用mac虚拟机下载 编辑:程序博客网 时间:2024/04/27 16:35

问题 : 打牌

时间限制: 1 Sec  内存限制: 128 MB
提交: 2376  解决: 816
[提交][状态][讨论版]

题目描述

牌只有1到9,手里拿着已经排好序的牌a,对方出牌b,用程序判断手中牌是否能够压过对方出牌。 
规则:出牌牌型有5种   
[1]一张 如4 则5…9可压过 
[2]两张 如44 则55,66,77,…,99可压过 
[3]三张 如444 规则如[2] 
[4]四张 如4444 规则如[2] 
[5]五张 牌型只有12345 23456 34567 45678 56789五个,后面的比前面的均大。

输入

输入有多行,第一行代表手中的牌,长度不超过200个数字。接下来的每一行代表每次对方出的牌。

输出

输出有多行,代表手中的牌是否能压过对方出的牌,压过输出YES, 并列出所有可选项,可选项之间用空格分隔。 否则输出NO。

样例输入

176242345563673322234567

样例输出

YES 44 55 66 77YES 666NO


代码展示(注释为讲解)

#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){string card;while (cin >> card)//输入自己有什么牌,没有空格直接cin就好了 { string army;//设对手打出了什么牌 while (cin >> army){int point[10] = { 0 };//统计各个点数的牌 for (int i = 0; i<card.length(); i++)point[card[i] - '0']++;//string单个字符化为整数的用法,为对应的点数的牌数 if (army.length() == 1)//对应了对手出一张牌的情况 {int dianshu = army[0] - '0';//记录对手出的点数 bool yes = false;for (int i = dianshu + 1; i<10; i++)//从对手出的点数+1开始寻找 {if (point[i]>0)//判定能否找到至少有一种出牌方案 {yes = true;break;}}if (yes == false)//没有出牌方案时 {cout << "NO" << endl;continue;}else//有出牌方案时 {cout << "YES";for (int i = dianshu + 1; i<10; i++){while (point[i]>0){cout << " " << i;break;//我试过把全部都列出来,但是WA,我想如果你有4张3,但是输出一个3就好了 }}cout << endl;}}if (army.length() == 2)//和第一种情况类似 {int dianshu = army[0] - '0'; bool yes = false;for (int i = dianshu + 1; i<10; i++) {if (point[i] >= 2)//这里的if判断语句改成了>=3 {yes = true;break;}}if (yes == false){cout << "NO" << endl;continue;}else{cout << "YES";for (int i = dianshu + 1; i<10; i++){while (point[i] >= 2){cout << " " << i << i;break;}}cout << endl;}}if (army.length() == 3)//和第一种情况类似 {int dianshu = army[0] - '0';bool yes = false;for (int i = dianshu + 1; i<10; i++){if (point[i] >= 3){yes = true;break;}}if (yes == false){cout << "NO" << endl;continue;}else{cout << "YES";for (int i = dianshu + 1; i<10; i++){while (point[i] >= 3){cout << " " << i << i << i;break;}}cout << endl;}}if (army.length() == 4)//和第一种情况类似 {int dianshu = army[0] - '0';bool yes = false;for (int i = dianshu + 1; i<10; i++){if (point[i] >= 4){yes = true;break;}}if (yes == false){cout << "NO" << endl;continue;}else{cout << "YES";for (int i = dianshu + 1; i<10; i++){while (point[i] >= 4){cout << " " << i << i << i << i;break;}}cout << endl;}}if (army.length() == 5){int dianshu = army[0] - '0';//记录对手出的顺子的第一张牌 bool yes = false;for (int i = dianshu + 1; i<10; i++)//从对手出的顺子的第一张牌+1开始找 {if (point[i]>0 && point[i + 1]>0 && point[i + 2]>0 && point[i + 3]>0 && point[i + 4]>0)//应该很好理解这个if语句 {yes = true;break;}}if (yes == false){cout << "NO" << endl;continue;}else{cout << "YES";for (int i = dianshu + 1; i<6; i++){while (point[i]>0 && point[i + 1]>0 && point[i + 2]>0 && point[i + 3]>0 && point[i + 4]>0){cout << " " << i << i + 1 << i + 2 << i + 3 << i + 4;//输出顺子 break;}}cout << endl;}}}}}



原创粉丝点击