其他分享
首页 > 其他分享> > CSP-201903-2-二十四点

CSP-201903-2-二十四点

作者:互联网

二十四点(传送门)

这道题比较绕,刚开始暴力破解,步骤有点多,后来有使用栈来解决,比较简单

输入:
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
输出:
Yes
No
No
Yes
Yes
No
No
No
Yes
Yes

满分代码1------------------------------暴力破解


#include <bits/stdc++.h>

using namespace std;

int n;
int result = 0;
bool judge[102];
string str;

int call(int a,int b,char c){
	int res;
	switch(c){
		case 'x':
			res=a*b;
			break;
		case '/':
			res=a/b;
			break;
		case '+':
			res=a+b;
			break;
		case '-':
			res=a-b;
			break;
	}
	return res;
}

bool cal(){
	char c[6];
	int coun = 0;
	int j = -1;
	c[1]=str[1];c[3]=str[3];c[5]=str[5];
	for(int i = 1; i < 6; i+=2){
		if(c[i]=='x' || c[i]=='/'){
			c[i-1] = '1';
			coun++;
		}else{
			c[i-1] = '0';
		}
	}
	if(coun == 3 || coun == 0){ //例1+2+3+4  1*2*3*4  1/2/3/4
		result = call(str[0]-'0',str[2]-'0',str[1]);
		result = call(result,str[4]-'0',str[3]);
		result = call(result,str[6]-'0',str[5]);
	}else{
		if(coun == 1){
			for(int i = 1; i < 6; i+=2){
				if(c[i-1] == '1'){
					result = call(str[i-1]-'0',str[i+1]-'0',str[i]);
					j = i;
				}
			}
			if(j == 1){			//例1*2+3-4
				result = call(result,str[4]-'0',str[3]);
				result = call(result,str[6]-'0',str[5]);
			}else if(j == 3){   //例1+2*3-4
				result = call(str[0]-'0',result,str[1]);
				result = call(result,str[6]-'0',str[5]);
			}else{              //例1+2+3*4
				int res = call(str[0]-'0',str[2]-'0',str[1]);
				result = call(res,result,str[3]);
			}
		}else{
			for(int i = 1; i < 6; i+=2){
				if(c[i-1] == '0'){
					j = i;
				}
			}
			if(j == 1){     //例 1+2*3/4
				result = call(str[2]-'0',str[4]-'0',str[3]);
				result = call(result,str[6]-'0',str[5]);
				result = call(str[0]-'0',result,str[1]);
			}else if(j == 3){       //例 1*2+3*4
				int res = call(str[0]-'0',str[2]-'0',str[1]);
				result = call(str[4]-'0',str[6]-'0',str[5]);
				result = call(res,result,str[3]);
			}else{              //例 1*2*3-4
				result = call(str[0]-'0',str[2]-'0',str[1]);
				result = call(result,str[4]-'0',str[3]);
				result = call(result,str[6]-'0',str[5]);
			}
		}
	}
	if(result == 24){
		return true;
	}
	return false;
}

int main(){
    cin >> n;
    for(int i = 0 ; i < n; i++){
		cin >> str;
		if(cal()){
			judge[i] = true;
		}
	}
	
	for(int i = 0; i < n; i++){
		if(judge[i]){
			cout << "Yes\n";
		}else{
			cout << "No\n";
		}
	}
	
    return 0;
}

满分代码2------------------------------使用栈实现-------------------------参考别人代码才知道使用栈

#include <iostream>
using namespace std;

int main(){
	int n, num[5], temp;//行数 - num[栈顶,--]
	char op[5]; 	//运算符
	num[0] = 0;		//初始化操作数栈
	op[0] = 0;		//初始化运算符栈
	cin>>n;
	int result[n];
	for(int i=0; i<n; ++i){
		for(int j=0; j<7; ++j){
			if(j%2 == 0){	//操作数进栈
				cin>>num[++num[0]];
				if (op[op[0]] == '-')	//如果运算符栈顶为 - 则将操作数转换 +(-x)
					num[num[0]] *= -1;
				else if (op[op[0]] == 'x'){	//需要x运算
					temp = num[num[0]] * num[num[0]-1];
					num[--num[0]] = temp;
					--op[0];
				}
				else if(op[op[0]] == '/'){	//需要/运算
					temp = num[num[0]-1] / num[num[0]];
					num[--num[0]] = temp;
					--op[0];
				}
			}
			else{			//运算符进栈
				cin>>op[++op[0]];
			}
		}
		while(op[0] > 0){	//将剩余+号运算完
				temp = num[num[0]] + num[num[0]-1];
				num[--num[0]] = temp;
				--op[0];
		}
		if(num[1] == 24)	//判定结果
			result[i] = 1;
		else
			result[i] = 0;
		num[0] = 0;			//初始化操作数栈
	}
	
	for(int i=0; i<n; ++i){	//打印结果
		if(result[i] == 1)
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
	return 0;

这里是题目O(∩_∩)O,欢迎大家留言,有空的话可以点个赞哦(#^ . ^#)

       试题编号: 201903-2
试题名称: 二十四点
时间限制: 1.0s
内存限制: 256.0MB
问题描述:

标签:二十四点,int,num,call,result,str,op,CSP,201903
来源: https://blog.csdn.net/weixin_44635198/article/details/104716267