社区讨论

不是很理解

P1575正误问题参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo8zhfee
此快照首次捕获于
2023/10/28 03:05
2 年前
此快照最后确认于
2023/10/28 03:05
2 年前
查看原帖
为什么把程序的第 66 到 68 行(写有注释处)改为
CPP
for(i = 1; i <= n; i++) {
    if(ex[i] == "or") {
        if(i - 1 < 1 || i + 1 > n) check("");
        check(ex[i - 1]); check(ex[i + 1]);
    }
}
就可以通过?
我的思路很简单:处理 not,处理 and,处理 or
所以或者就是问为什么最后计算完 notand 之后,剩下的表达式(保证正确),在偶数位置上有可能不是 or
望解答,或者直接给出反例,谢谢。
代码如下:
CPP
/*Copyright (C) 2013-2024 LZE*/
#include<bits/stdc++.h>
#define fo(x) freopen(#x".in", "r", stdin); freopen(#x".out", "w", stdout);
#define INF 0x7fffffff
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef const long long cll;
const int N = 1000010;
const int M = 100010;
ll T, n, m;
string ex[260];
void check(const string &a, const bool &stop = false) {
    if(a != "true" && a != "false") {
        if(stop) while(true);
        cout << "error\n";
        exit(0);
    }
}
string Fun_And(const string &a, const string &b) {
    check(a); check(b);
    bool n1 = (a == "true" ? 1 : 0), n2 = (b == "true" ? 1 : 0);
    return n1 & n2 ? "true" : "false";
}
bool Fun_Or(const bool &a, const string &b) {
    check(b);
    bool n2 = (b == "true" ? 1 : 0);
    return a | n2;
}
int main() {
#ifdef LOCAL
    fo(debug_data)
#endif
    string tmp;
    for(n = 1; cin >> tmp; n++) {
        ex[n] = tmp;
        if(tmp == "not") {
            bool t = 0;
            while(tmp == "not") {
                t = !t;
                cin >> tmp;
            }
            check(tmp);
            if(tmp == "false") tmp = "true";
            else tmp = "false";
            if(!t) tmp = "true";
            else tmp = "false";
            ex[n] = tmp;
        }
    }
    n--;
    ll i, pos;
    for(i = 1, pos = 1; pos <= n; pos++, i++) {
        if(ex[pos] == "and") {
            if(pos - 1 < 1 || pos + 1 > n) check("");
            ex[i - 1] = Fun_And(ex[pos - 1], ex[pos + 1]);
            pos++; i--;
        }
    }
    n = --i;
    for(i = 1; i <= n; i += 2) {
        check(ex[i]);
    }
    for(i = 2; i <= n; i += 2) { // from here
        if(ex[i] != "or") check("");
    } // to here
    bool ans = 0;
    for(i = 1; i <= n; i += 2) {
        ans = Fun_Or(ans, ex[i]);
    }
    if(ans) cout << "true";
    else cout << "false";
    cout << endl;
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...