2021年度训练联盟热身训练赛第三场 A Circuit Math 后缀表达式

【2021年度训练联盟热身训练赛第三场】
A Circuit Math 后缀表达式+简单数字电路

输入电路元件(字母)的状态(T或F)及其连接方式(*表示与,+表示或,-表示非),输出最后的运算结果。

对输入从前往后处理,若遇到字母,则将其值(True或False)存入栈中;若遇到运算符,则取出栈顶的一个(非)或两个(与、或)元素进行运算,并将运算得到的结果存入栈中。最终栈中剩下的那个元素即为要求的答案。

在这里插入图片描述
在这里插入图片描述

样例输入:

4
T F T F
A B * C D + - +

样例输出:

F

AC代码:

#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
using namespace std;
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;
	char c;
	bool a[26];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>c;
		if(c=='T')
			a[i]=true;
		else
			a[i]=false;
	}
	cin.ignore(); 
	stringstream ss;
	string s;
	getline(cin,s);
	ss<<s;
	stack<bool> sta;
	while(ss>>c)
	{
		if(c>='A'&&c<='Z')
			sta.push(a[c-'A']);
		if(c=='*')
		{
			bool a=sta.top();
			sta.pop();
			bool b=sta.top();
			sta.pop();
			bool t=a&&b;
			sta.push(t);
		}
		if(c=='+')
		{
			bool a=sta.top();
			sta.pop();
			bool b=sta.top();
			sta.pop();
			bool t=a||b;
			sta.push(t);
		}
		if(c=='-')
		{
			bool a=sta.top();
			sta.pop();
			bool t=!a;
			sta.push(t);
		}
	}
	char t=sta.top();
	if(t==true)
		printf("T");
	else
		printf("F");
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

球王武磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值