CCF201903-2 二十四点

CCF资格认证试题201903-2 二十四点

题目
在这里插入图片描述
此题需要注意题目中的乘法输入为“x”,若看成“*”,只能的50分,QAQ~~。
方法一:
模拟:对每一个符号位都进行分析,先处理乘除,在处理加减。

//ccf 201903-2 二十四点
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	int n;
	cin>>n;
	while(n--){
		string s;
		int f1=0,f2=0; 
		//f1=1则第一个符号为“X”或者“/”
		//f2=1则第二个符号为“X”或者“/” 
		cin>>s;
		int n1=s[0]-'0';  //第一个数 
		int n2=s[2]-'0';  //第二个数 
		int n3=s[4]-'0';  //第三个数 
		int n4=s[6]-'0';  //第四个数 
		
		//s[1]处理第一个符号 
		if(s[1]=='x'){   //乘法8 
			int ans=(s[0]-'0')*(s[2]-'0');
			n1=0;
			s[1]='+';	//修改第一个符号为+ 
			n2=ans;	     
			f1=1;    	//标记 
		}
		if(s[1]=='/'){  //除法 
			int ans=(s[0]-'0')/(s[2]-'0');
			n1=0;
			s[1]='+';
			n2=ans;
			f1=1;
		}
		
		//s[3]处理第二个符号
		if(s[3]=='x'){
			int ans=0;
			if(f1==1)ans=n2*(s[4]-'0');  
			else ans=(s[2]-'0')*(s[4]-'0');
			n2=0;
			if(s[1]=='-')s[3]='-';
				else s[3]='+';
			n3=ans;
			f2=1;
		}
		if(s[3]=='/'){
			int ans=0;
			if(f1==1)ans=n2/(s[4]-'0');
			else ans=(s[2]-'0')/(s[4]-'0');
			n2=0;
			if(s[1]=='-')s[3]='-';
				else s[3]='+';
			n3=ans;
			f2=1;
		}
		
		//s[5]处理第三个符号
		if(s[5]=='x'){
			int ans=0;
			if(f2==1)ans=n3*(s[6]-'0');
			else ans=(s[4]-'0')*(s[6]-'0');
			n3=0;
			if(s[3]=='-')s[5]='-';
				else s[5]='+';
			n4=ans;
		}
		if(s[5]=='/'){
			int ans=0;
			if(f2==1)ans=n3/(s[6]-'0');
			else ans=(s[4]-'0')/(s[6]-'0');
			n3=0;
			if(s[3]=='-')s[5]='-';
				else s[5]='+';
			n4=ans;
		}	
		int ans=n1;
		if(s[1]=='+')ans+=n2;
			else ans-=n2;
		if(s[3]=='+')ans+=n3;
			else ans-=n3;
		if(s[5]=='+')ans+=n4;
			else ans-=n4;	
		if(ans==24)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}

方法二:栈

#include<cstdio>
#include<iostream> 
#include<cstring>
#include<stack>
using namespace std;
int n;
char str[10];
stack<int> num;	//记录数字 
stack<char> sign; //记录符号 
int main(){
	cin>>n;
	while(n--){
		gets(str);
		while(!num.empty()) num.pop();  //初始化栈 
		while(!sign.empty()) sign.pop();
		int j=0;
		while(j<strlen(str)){
			if(str[j]>'0' && str[j]<='9'){
				num.push(str[j]-'0');
			} 
			//根据符号:减变加,优先处理乘除 
			else{
				if(str[j]=='+'){
					sign.push('+');
				}
				else if(str[j]=='-'){ 	//将减法转换成加法 
					num.push((str[j+1]-'0')*(-1));
					sign.push('+');
					j++;
				}
				else if(str[j]=='x'){ 	//直接计算乘法 
					int l=num.top();	//等于栈顶元素 
					num.pop();			//出栈 
					num.push(l*(str[j+1]-'0'));	//压栈
					j++;
				}
				else if(str[j]=='/'){ 	//直接计算除法 
					int l=num.top();	//等于栈顶元素
					num.pop();			//出栈
					num.push(l/(str[j+1]-'0'));	//压栈 
					j++;
				}
			}
			j++;
		}
		while(!sign.empty()){	//计算剩余的加法 
			int r=num.top();	
			num.pop();
			int l=num.top();
			num.pop();
			sign.pop();
			num.push(l+r);
		}
		int ans=num.top();
		if(ans==24)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值