【JZOJ4840】【NOIP2016提高A组集训第4场11.1】小W砍大树

题目描述

这里写图片描述

数据范围

这里写图片描述

解法

模拟。

代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<iostream>
#define ll long long
using namespace std;
const char* fin="form.in";
const char* fout="form.out";
const int inf=0x7fffffff;
const int maxn=40007;
char ch;
bool stack[maxn];
int t,tot,y;
int main(){
    freopen(fin,"r",stdin);
    freopen(fout,"w",stdout);
    ch=getchar();
    while (1){
        y=0;
        while (ch!='(' && ch!=')' && ch!='F' && ch!='T'){
            ch=getchar();
            y++;
            if (y==10) return 0;
        }
        t++;
        tot=0;
        stack[tot]=false;
        while (ch=='(' || ch==')' || ch=='F' || ch=='T'){
            if (ch=='('){
                tot++;
                stack[tot]=(tot&1?true:false);
            }else if (ch==')'){
                if (tot&1) stack[tot-1]|=stack[tot];
                else stack[tot-1]&=stack[tot];
                tot--;
            }else if (ch=='F'){
                if (tot%2) stack[tot]=false;
            }else if (tot%2==0) stack[tot]=true;
            ch=getchar();
        }
        if (stack[0]) printf("%d. true
",t);
        else printf("%d. false
",t);
    }
    return 0;
}

启发

利用getchar()连续多次失效来判断文件输入结束。

原文地址:https://www.cnblogs.com/hiweibolu/p/6714858.html