Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

201903-2二十四点[20201213封笔题目]没写呢

2020.12.13
csp今天就考试完了,我用了三天的时间,倒着刷完了所有的第一题,正着刷第二题,就到了这里。暂时结束吧。

哎,发现现在的考试越来越难了。

今天的考试连第二个体都过不了。

我死了。

拿钱去奶CCF官网,以后报名可能真的就要好贵好贵了。

问题描述
试题编号: 201903-2
试题名称: 二十四点
时间限制: 1.0s
内存限制: 512.0MB
在这里插入图片描述
在这里插入图片描述

没通过的代码

暂时保存一下

#include<bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin>>n;
	while(n--) {
		stack<int> dig;
		stack<char> op;
		string s;
		cin>>s;
		int sum=0;
		for(int i=0; i<s.length(); i++) {
			//进符号栈
			if(s[i]=='+'||s[i]=='-'||s[i]=='x'||s[i]=='/') {
				//+ -
				if(s[i]=='+'||s[i]=='-') {
					if(!op.empty()&&!dig.empty()&&(op.top()=='/'||op.top()=='x')) {
						int ch=op.top();
						op.pop();
						int q=dig.top();
						dig.pop();
						int w=dig.top();
						dig.pop();
						int e;
						if(ch=='/')
							e=w/q;
						else if(ch=='x')
							e=w*q;
						dig.push(e);
					} else {
						op.push(s[i]);
					}
				}
				//x /
				else if(s[i]=='x'||s[i]=='/') {
					op.push(s[i]);
				}
			}
			//进数字栈
			else {
				op.push(s[i]);
			}
		}
		while(!op.empty()) {
			char q=op.top();
			op.pop();
			int w=dig.top();
			dig.pop();
			int e=dig.top();
			dig.pop();
			int r;
			if(q=='+')
				r=e+w;
			else if(q=='-')
				r=e-w;
			else if(q=='x')
				r=e*w;
			else if(q=='/')
				r=e/w;
			dig.push(r);
		}
		if(dig.top()==24)
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
	}
	return 0;
}

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: Arduino+nRF24L01多按键远程无线控制(接收端)

下一篇: 【金融市场基础知识】——金融市场体系

精华推荐