华为OJ(扑克牌之24点游戏)
2015-08-19 15:38
507 查看
描述 | 计算24点是一种扑克牌益智游戏,随机抽出4张扑克牌,通过加(+),减(-),乘(*), 除(/)四种运算法则计算得到整数24,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写joker表示小王,大写JOKER表示大王: 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER本程序要求实现:输入4张牌,输出一个算式,算式的结果为24点。 详细说明: 1.运算只考虑加减乘除运算,没有阶乘等特殊运算符号,友情提醒,整数除法要当心; 2.牌面2~10对应的权值为2~10, J、Q、K、A权值分别为为11、12、13、1; 3.输入4张牌为字符串形式,以一个空格隔开,首尾无空格;如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算; 4.输出的算式格式为4张牌通过+-*/四个运算符相连,中间无空格,4张牌出现顺序任意,只要结果正确; 5.输出算式的运算顺序从左至右,不包含括号,如1+2+3*4的结果为246.如果存在多种算式都能计算得出24,只需输出一种即可,如果无法得出24,则输出“NONE”表示无解。 |
---|---|
知识点 | 字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入4张牌为字符串形式,以一个空格隔开,首尾无空格; |
输出 | 如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算; 输出的算式格式为4张牌通过+-*/四个运算符相连,中间无空格,4张牌出现顺序任意, 只要结果正确; 输出算式的运算顺序从左至右,不包含括号,如1+2+3*4的结果为24 如果存在多种算式都能计算得出24,只需输出一种即可,如果无法得出24, 则输出“NONE”表示无解。 |
样例输入 | A A A A |
样例输出 | NONE |
#include<iostream> #include<string> #include<sstream> using namespace std; int count_24_point(int,int,int,int); int cal(int,int,int,int,int,int,int); char m[4]={'+','-','*','/'}; string p[4]; int main() { int a[4],i=0; string s,word; getline(cin,s); istringstream is(s); while(is>>word) { if(word=="joker"||word=="JOKER") { cout<<"ERROR"; break; } else if(word=="J") a[i]=11; else if(word=="Q") a[i]=12; else if(word=="K") a[i]=13; else if(word=="A") a[i]=1; else a[i]=atoi(word.c_str()); p[i]=word; i++; } count_24_point(a[0],a[1],a[2],a[3]); //system("pause"); return 0; } int cal(int a,int b,int c,int d,int i,int j,int k) { int sum=0; if(i==0) sum=a+b; else if(i==1) sum=a-b; else if(i==2) sum=a*b; else if(i==3) { if(a%b==0) sum=a/b; else return 0; } if(j==0) sum+=c; else if(j==1) sum-=c; else if(j==2) sum*=c; else if(j==3) { if(sum%c==0) sum/=c; else return 0; } if(k==0) sum+=d; else if(k==1) sum-=d; else if(k==2) sum*=d; else if(k==3) { if(sum%d==0) sum/=d; else return 0; } return sum; } int count_24_point(int a,int b,int c,int d) { for(int i=0;i<4;i++) for(int j=0;j<4;j++) for(int k=0;k<4;k++) { if(cal(a,b,c,d,i,j,k)==24) { cout<<p[0]<<m[i]<<p[1]<<m[j]<<p[2]<<m[k]<<p[3]<<endl; return 1; } else if(cal(a,b,d,c,i,j,k)==24) { cout<<p[0]<<m[i]<<p[1]<<m[j]<<p[3]<<m[k]<<p[2]<<endl; return 1; } else if(cal(a,c,b,d,i,j,k)==24) { cout<<p[0]<<m[i]<<p[2]<<m[j]<<p[1]<<m[k]<<p[3]<<endl; return 1; } else if(cal(a,c,d,b,i,j,k)==24) { cout<<p[0]<<m[i]<<p[2]<<m[j]<<p[3]<<m[k]<<p[1]<<endl; return 1; } else if(cal(a,d,b,c,i,j,k)==24) { cout<<p[0]<<m[i]<<p[3]<<m[j]<<p[1]<<m[k]<<p[2]<<endl; return 1; } else if(cal(a,d,c,b,i,j,k)==24) { cout<<p[0]<<m[i]<<p[3]<<m[j]<<p[2]<<m[k]<<p[1]<<endl; return 1; } else if(cal(b,a,c,d,i,j,k)==24) { cout<<p[1]<<m[i]<<p[0]<<m[j]<<p[2]<<m[k]<<p[3]<<endl; return 1; } else if(cal(b,a,d,c,i,j,k)==24) { cout<<p[1]<<m[i]<<p[0]<<m[j]<<p[3]<<m[k]<<p[2]<<endl; return 1; } else if(cal(b,c,a,d,i,j,k)==24) { cout<<p[1]<<m[i]<<p[2]<<m[j]<<p[0]<<m[k]<<p[3]<<endl; return 1; } else if(cal(b,c,d,a,i,j,k)==24) { cout<<p[1]<<m[i]<<p[2]<<m[j]<<p[3]<<m[k]<<p[0]<<endl; return 1; } else if(cal(b,d,a,c,i,j,k)==24) { cout<<p[1]<<m[i]<<p[3]<<m[j]<<p[0]<<m[k]<<p[2]<<endl; return 1; } else if(cal(b,d,c,a,i,j,k)==24) { cout<<p[1]<<m[i]<<p[3]<<m[j]<<p[2]<<m[k]<<p[0]<<endl; return 1; } else if(cal(c,a,b,d,i,j,k)==24) { cout<<p[2]<<m[i]<<p[0]<<m[j]<<p[1]<<m[k]<<p[3]<<endl; return 1; } else if(cal(c,a,d,b,i,j,k)==24) { cout<<p[2]<<m[i]<<p[0]<<m[j]<<p[3]<<m[k]<<p[1]<<endl; return 1; } else if(cal(c,b,a,d,i,j,k)==24) { cout<<p[2]<<m[i]<<p[1]<<m[j]<<p[0]<<m[k]<<p[3]<<endl; return 1; } else if(cal(c,b,d,a,i,j,k)==24) { cout<<p[2]<<m[i]<<p[1]<<m[j]<<p[3]<<m[k]<<p[0]<<endl; return 1; } else if(cal(c,d,a,b,i,j,k)==24) { cout<<p[2]<<m[i]<<p[3]<<m[j]<<p[0]<<m[k]<<p[1]<<endl; return 1; } else if(cal(c,d,b,a,i,j,k)==24) { cout<<p[2]<<m[i]<<p[3]<<m[j]<<p[1]<<m[k]<<p[0]<<endl; return 1; } else if(cal(d,a,b,c,i,j,k)==24) { cout<<p[3]<<m[i]<<p[0]<<m[j]<<p[1]<<m[k]<<p[2]<<endl; return 1; } else if(cal(d,a,c,b,i,j,k)==24) { cout<<p[3]<<m[i]<<p[0]<<m[j]<<p[2]<<m[k]<<p[1]<<endl; return 1; } else if(cal(d,b,a,c,i,j,k)==24) { cout<<p[3]<<m[i]<<p[1]<<m[j]<<p[0]<<m[k]<<p[2]<<endl; return 1; } else if(cal(d,b,c,a,i,j,k)==24) { cout<<p[3]<<m[i]<<p[1]<<m[j]<<p[2]<<m[k]<<p[0]<<endl; return 1; } else if(cal(d,c,a,b,i,j,k)==24) { cout<<p[3]<<m[i]<<p[2]<<m[j]<<p[0]<<m[k]<<p[1]<<endl; return 1; } else if(cal(d,c,b,a,i,j,k)==24) { cout<<p[3]<<m[i]<<p[2]<<m[j]<<p[1]<<m[k]<<p[0]<<endl; return 1; } } cout<<"NONE"<<endl; return 0; }
看了高手写的代码,自愧不如,对比发现,下面的代码好像没有考虑a,b,c,d顺序的问题,只考虑了a,b,c,d的情况,另外对于STL中next_permutation是生成运算符的全排列,而穷举运算符顺序不同。
#include <iostream> #include <string> #include <algorithm> using namespace std; string poker[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; char C[12] = { '+', '+', '+', '-', '-', '-', '*', '*', '*', '/', '/', '/' }; int Calculate(int a, int b, char c) { switch (c) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return -1; //不会有这个选项 } } int Caculate24(int a, int b, int c, int d, char C1, char C2, char C3) { return Calculate(Calculate(Calculate(a, b, C1), c, C2), d, C3); } bool Count24(int a, int b, int c, int d) { sort(C, C + 12); do { if (Caculate24(a, b, c, d, C[0], C[1], C[2]) == 24) { //do right; return true; } } while (next_permutation(C, C + 12)); return false; } int main() { string str[4]; cin >> str[0] >> str[1] >> str[2] >> str[3]; int i, j; bool legal; string *p; for (i = 0; i < 4; i++) { legal = false; p=find(poker,poker+13,str[i]); if(p-poker!=13) legal=true; if (legal == false) { cout << "ERROR" << endl; return 0; } } int a, b, c, d; p = find(poker, poker + 13, str[0]); a = p - poker + 1; p = find(poker, poker + 13, str[1]); b = p - poker + 1; p = find(poker, poker + 13, str[2]); c = p - poker + 1; p = find(poker, poker + 13, str[3]); d = p - poker + 1; //现在a, b, c, d分别代表每一张牌的数值 int num[4]; num[0] = a, num[1] = b, num[2] = c, num[3] = d; do { if (Count24(num[0], num[1], num[2], num[3])) { cout << poker[num[0] - 1] << C[0] << poker[num[1] - 1] << C[1] << poker[num[2] - 1] << C[2] << poker[num[3] - 1] << endl; return 0; } } while (next_permutation(num, num+4)); cout << "NONE" << endl; return 0; }
相关文章推荐
- poj 3422 Kaka's Matrix Travels 最小费最大流
- iOS系统架构分层
- Mybatis的增删改查实例
- UVA10557- XYZZY(spfa+设置次数上限)
- JS简易模拟滚动条
- 编写高质量代码改善C#程序的157个建议——建议77: 正确停止线程
- poj 3422 Kaka's Matrix Travels 最小费最大流
- 进程&线程 同步异步&阻塞非阻塞
- js把预定义的html字符串转换为 HTML 实体 htmlspecialchars 输出html实体内容包括标签,而不自动转义标签,只显示内容,类似php的htmlspecialchars
- 论如何优雅的处理回文串 - 回文自动机详解
- BitMap设置图片透明度
- 桌面支持--扫描文件公章技巧
- 桌面支持-扫描打印发送错误报告
- 两个Activity间切换出现短暂白/黑屏的处理方法
- jQuery
- [leetcode] 263.Ugly Number
- Mybatis 相关知识(一)
- IPV4与IPV6 的 一点区别 (不深究、只记录备忘)
- Java的非访问控制符:static,final,abstract
- PHP 网页调用本地exe程序实例