【递归】【栈】先修课 计算概论(A)/函数递归练习(2)5:布尔表达式
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
-
输入一个布尔表达式,请你输出它的真假值。
比如:( V | V ) & F & ( F | V )
V表示true,F表示false,&表示与,|表示或,!表示非。
上式的结果是F - 输入
- 输入包含多行,每行一个布尔表达式,表达式中可以有空格,总长度不超过1000
- 输出
- 对每行输入,如果表达式为真,输出"V",否则出来"F"
- 样例输入
-
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V)) - 样例输出
-
F
V
V 递归和栈的基础应用。
代码丑飞……
Code:#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
string s;
int len;
bool work(int x)
{
char lastopt='&';
bool res=true;
bool sym=true;
for(int i=x;i<len;i++)
if(s[i]==')') return res;
else if(s[i]==' ') continue;
else if(s[i]=='!') sym=false;
else if(s[i]=='&') lastopt='&';
else if(s[i]=='|') lastopt='|';
else if(s[i]=='V')
{
bool tmp=true;
if(!sym) {tmp=false;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else if(s[i]=='F')
{
bool tmp=false;
if(!sym) {tmp=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else
{
bool tmp=work(i+);
if(!sym) {tmp^=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
int top=;
for(int j=i+;j<len;j++)
{
if(s[j]=='(') top++;
else if(s[j]==')') top--;
if(!top)
{
i=j;
break;
}
}
}
}
int main()
{
while()
{
s.clear();
getline(cin,s);
len=s.length();
if(!len) break;
char lastopt='&';
bool res=true;
bool sym=true;
for(int i=;i<len;i++)
if(s[i]==' ') continue;
else if(s[i]=='(')
{
bool tmp=work(i+);
if(!sym) {tmp^=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
int top=;
for(int j=i+;j<len;j++)
{
if(s[j]=='(') top++;
else if(s[j]==')') top--;
if(!top)
{
i=j;
break;
}
}
}
else if(s[i]=='!') sym=false;
else if(s[i]=='&') lastopt='&';
else if(s[i]=='|') lastopt='|';
else if(s[i]=='V')
{
bool tmp=true;
if(!sym) {tmp=false;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else if(s[i]=='F')
{
bool tmp=false;
if(!sym) {tmp=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
putchar( res ? 'V' : 'F' );puts("");
}
return ;
}
【递归】【栈】先修课 计算概论(A)/函数递归练习(2)5:布尔表达式的更多相关文章
- 【递归】先修课 计算概论(A) / 函数递归练习(3)2:分解因数
#include<cstdio> using namespace std; bool is_prime(int x) { ;i*i<=x;i++) ) return false; r ...
- Openjudge计算概论——数组逆序重放【递归练习】
/*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...
- 计算概论(A)/基础编程练习1(8题)/3:晶晶赴约会
#include<stdio.h> int main() { int w; scanf("%d", &w); || w==) { printf("%s ...
- OpenJudge计算概论-取石子游戏
OpenJudge计算概论-取石子游戏[函数递归练习] /*====================================================================== ...
- OpenJudge计算概论-计算书费
/*============================================== 计算书费 总时间限制: 1000ms 内存限制: 65536kB 描述 下面是一个图书的单价表: 计算 ...
- python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数
一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...
- 计算概论(A)/基础编程练习(数据成分)/3:整数的个数
#include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...
- 计算概论(A)/基础编程练习(数据成分)/2:奥运奖牌计数
#include<stdio.h> int main() { // n天的决赛项目 int n; scanf("%d",&n); ] = {}; while ( ...
- 计算概论(A)/基础编程练习(数据成分)/1:短信计费
#include<stdio.h> int main() { // 输入当月发送短信的总次数n和每次短信的字数words int n,words; scanf("%d" ...
随机推荐
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- NOIP2003 神经网络(bfs)
NOIP2003 神经网络 题目背景: 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应 ...
- iOS 控制台打印unicode 转中文汉字 UTF8String
今天查看代码数据结构,就在控台直接打印了,soGa,我看到了什么!!!! 于是乎想到了不对劲,不对呀,之前打印都是 UTF8String的呀,怎么会这样,百思不得其姐,看了一下封装的网络类,SoGa, ...
- Linux引导过程
早期时,启动一台计算机意味着要给计算机喂一条包含引导程序的纸带,或者手工使用前端面板地址/数据/控制开关来加载引导程序.尽管目前的计算机已经装备了很多工具来简化引导过程,但是这一切并没有对整个过程进行 ...
- hdu 2544 最短路 (dijkstra,floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找到两点间最短的距离值. 代码一:(dijkstra算法) #include < ...
- Selenium菜鸟手册
转自: http://www.iselenium.com/read.php?tid=458 首先声明我还是一只很菜的菜鸟,学习Selenium一个来月而已,发这个帖子是想利用我这块板砖引出真正的玉来, ...
- appium===报错Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.的解决办法
要解决的问题:appium在androidV7.0系统上运行时报错 Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.a ...
- Basic-Paxos协议日志同步应用
使用Basic-Paxos协议的日志同步与恢复 传统数据库保持服务持续可用通常采用1主N备, 既采取两种日志同步模式: Maximum Availability和Maximum Protection. ...
- 非旋转Treap——普通平衡树
扔板跑…… #include<bits/stdc++.h> #define N 100010 #define mp make_pair using namespace std; typed ...
- docker从零开始网络(一)概述
概述 预计阅读时间: 4分钟 Docker容器和服务如此强大的原因之一是您可以将它们连接在一起,或者将它们连接到非Docker工作负载.Docker容器和服务甚至不需要知道它们部署在Docker上,或 ...