社区讨论
求教
P8815[CSP-J 2022] 逻辑表达式参与者 2已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhizmgxu
- 此快照首次捕获于
- 2025/11/03 18:18 4 个月前
- 此快照最后确认于
- 2025/11/03 18:18 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
string s;
stack<pair<char, int>>p;
stack<int>q;
int fuhao[256], cnt_and, cnt_or;
void duanlu(int &i){
if(s[i] == '|' && s[i - 1] == '0' || s[i] == '&' && s[i - 1] == '1')return 0;
}
void cal(stack<int> &q, stack<pair<char, int>> &p){
int x = p.top().second;
char t = p.top().first; p.pop();
int b = q.top(); q.pop();
int a = q.top(); q.pop();
switch(t){
case '&':
if(a == 0)q.push(0);
else if(b == 0)q.push(0);
else q.push(1);
break;
case '|':
if(a == 1)q.push(1);
else if(b == 1)q.push(1);
else q.push(0);
}
}
int main(){
fuhao['&'] = 2;
fuhao['|'] = 1;
cin >> s;
s.push_back(')');
p.push(make_pair('(', -1));
for(int i = 0; i < s.size() - 1; i++){
char &c = s[i];
if(isdigit(c)){
q.push((int)c - 48);
}
if(c == '&' || c == '|'){
while(p.top().first != '(' && fuhao[p.top().first] >= fuhao[c])
cal(q, p);
duanlu(i);
p.push(make_pair(c, i));
}
if(c == '(') p.push(make_pair(c, i));
else if(c == ')') {
while(p.top().first != '(')
cal(q, p);
p.pop();
}
}
cout << q.top() << '\n' << cnt_and << ' ' << cnt_or << '\n';
return 0;
}
duanlu 函数不会写,求完善回复
共 5 条回复,欢迎继续交流。
正在加载回复...