求助,在线等
  • 板块学术版
  • 楼主139______61
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/31 17:30
  • 上次更新2023/10/27 04:43:01
查看原帖
求助,在线等
629996
139______61楼主2022/10/31 17:30

CSP-J 第三题如何处理这个问题:

如果某处“短路”包含在更外层被“短路”的部分内则不被统计,如表达式 1|(0&1) 中,尽管 0&1 是一处“短路”,但由于外层的 1|(0&1) 本身就是一处“短路”,无需再计算 0&1 部分的值,因此不应当把这里的0&1 计入一处“短路”。

在线等!!!

#include <iostream>
#include <algorithm>
#include <stack>
#include <cmath>

using namespace std;

typedef long long ll;
char ch;
int s1 = 0, s2 = 0;
stack<ll> d;
stack<char> o; 

void Cacl(){
	char ch = o.top();
	o.pop();
	ll op1,op2,op;
	op2 = d.top(); d.pop();
	op1 = d.top(); d.pop();
	if(ch == '&') {
		op = op1 && op2;	
		if(op1 == 0)
			s1++;
	} 
	if(ch == '|') {
		op = op1 || op2;
		if(op1 == 1)
			s2++;
	}
	d.push(op);
}

int level(char ch){
	if(ch == '&') return 2;
	if(ch == '|') return 1;
	return 0; 
}

int main(){
	int num = 0;
	bool in = true;
	while(cin>>ch){
		if(ch >= '0' && ch <= '9'){
			num = num*10 + (ch-'0');
			in = true;
			continue;
		} 
		if(in == true){
			d.push(num);
			num = 0;
			in = false;
		}
		if(ch == '(')
			o.push(ch);
		else if(ch == ')'){
			while(!o.empty() && o.top()!='(')
				Cacl();
			o.pop();
		}
		else{
			while(!o.empty() && level(o.top()) >= level(ch))
				Cacl();
			o.push(ch);
		}
	}
	if(in == true)
		d.push(num);
	while(!o.empty())
		Cacl();
	cout<<d.top() << endl;
	cout << s1 << " " << s2;
	return 0;
}
2022/10/31 17:30
加载中...