题目描述 24点游戏的玩法是这样的:任取一幅牌中的 4张牌(不含大小王),每张牌上有数字(其中A 代表1,J 代表11,Q 代表 12,K代表13),你可以利用数学中的加、减、乘、除以及括号想办法得到24,每张牌只能用一次。例如有四张6,那么6+6+6+6=24,也可以6*6-6-6=24。但是有些牌是无法得到24的,比如两张 A 和两张2。
读入表达式树的先序遍历字符串, 这里的表达式树是来自24点游戏的真实场景,也就是对应四个数字(值在1到13之间)组成的表达式,问该表达式树能不能得到24?
输入 输入由多组测试数据组成。
每组数据包含一行字符串,即24点游戏的表达式树的先序遍历序列。 输出 对于每组数据,输出一行。如果不能得到24,输出“NO”。如果能得到24,按样例输出。 样例输入 Copy
6 # # 6 # # 6 # # 6 # # 6 # # 6 # # 6 # # 6 # # 1 # # 2 # # * 1 # # 2 # # 样例输出 Copy (((6+6)+6)+6)=24 (((6*6)-6)-6)=24 NO这是本憨憨的代码
#include <iostream> #include <stdlib.h> #include<stdio.h> #include<string.h> int flag; using namespace std; typedef struct node { char a[7]; struct node *l; struct node *r; }*tt; /*************/ void CreateBiTree(tt &t) { char b[5]; if(!(cin >> b)) exit(0); if(b[0]=='#') t=NULL; else{ t=(tt)malloc(sizeof(node)); strcpy(t->a,b); CreateBiTree(t->l); CreateBiTree(t->r); } } /************/ void pp(tt t) { if(t) { if(t->a[0]>='0'&&t->a[0]<='9') cout<<t->a; else { cout<<'('; pp(t->l); cout<<t->a; pp(t->r); cout<<')'; } } } /*****求值****/ double answer(tt t) { if(t) { if(t->a[0]>='0'&&t->a[0]<='9') { double sum=0; for(int i=0;t->a[i]!='\0';i++) { sum=sum*10+t->a[i]-'0'; } return sum; } else { while(t->a[0]) { if(t->a[0]=='+') { return (answer(t->l)+answer(t->r)); break; } else if(t->a[0]=='-') { return (answer(t->l)-answer(t->r)); break; } else if(t->a[0]=='*') { return (answer(t->l)*answer(t->r)); break; } else if(t->a[0]=='/') { if(answer(t->r)==0){ flag=1; return 0;} else{ return (answer(t->l)/answer(t->r));} break; } } } } } int main() { for(int i=0;;i++){ flag=0; struct node *t; CreateBiTree(t); /*if(t==NULL) break;*/ cout<<answer(t)<<' '<<flag; if(answer(t)==24&&flag==0){/*******是这里出了错,但憨憨不自知,求大佬指点*****/ pp(t); cout<<'='<<answer(t); } else cout<<'N'<<'O'; cout<<'\n'; } return 0; }然后然后就出现了这很离谱的画面,明明判断条件是一样的,一个进了if,一个进了else. emmmmm,奇怪的问题又增加了。。。 求大佬指点。 改进后的代码,我把flag删了后,直接赋值,不重复调用多次answer函数。
#include <iostream> #include <stdlib.h> #include<stdio.h> #include<string.h> using namespace std; typedef struct node { char a[7]; struct node *l; struct node *r; }*tt; /*************/ void CreateBiTree(tt &t) { char b[5]; if(!(cin >> b)) exit(0); if(b[0]=='#') t=NULL; else{ t=(tt)malloc(sizeof(node)); strcpy(t->a,b); CreateBiTree(t->l); CreateBiTree(t->r); } } /************/ void pp(tt t) { if(t) { if(t->a[0]>='0'&&t->a[0]<='9') cout<<t->a; else { cout<<'('; pp(t->l); cout<<t->a; pp(t->r); cout<<')'; } } } /*****求值****/ double answer(tt t) { if(t) { if(t->a[0]>='0'&&t->a[0]<='9') { double sum=0; for(int i=0;t->a[i]!='\0';i++) { sum=sum*10+t->a[i]-'0'; } return sum; } else { while(t->a[0]) { if(t->a[0]=='+') { return (answer(t->l)+answer(t->r)); break; } else if(t->a[0]=='-') { return (answer(t->l)-answer(t->r)); break; } else if(t->a[0]=='*') { return (answer(t->l)*answer(t->r)); break; } else if(t->a[0]=='/') { if(answer(t->r)==0){ return 0;} else{ return (answer(t->l)/answer(t->r));} break; } } } } } int main() { for(int i=0;;i++){ struct node *t; CreateBiTree(t); /*if(t==NULL) break;*/ double j=answer(t); if(j==24){ pp(t); cout<<'='<<j; } else cout<<'N'<<'O'; cout<<'\n'; } return 0; }在然而, 学习oj竟然过不了,直接原地裂开。