【递归】【栈】先修课 计算概论(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" ...
随机推荐
- Grep basic and practice
定义:Grep (Globally search for the reqular expression and print out the line). 好处:Grep 在执行时不需要先调用编辑程序, ...
- SpringMVC——说说视图解析器
学习SpringMVC——说说视图解析器 各位前排的,后排的,都不要走,咱趁热打铁,就这一股劲我们今天来说说spring mvc的视图解析器(不要抢,都有位子~~~) 相信大家在昨天那篇如何获取请 ...
- Linux 下打包报错:enospc (no space left on device)
昨天打了个包,早上发现很多页面js加载404,一查原来打包的min文件夹下是空的,打包出错了 Error code: enospc du -sh * rm -f 文件名称 rm -rf 目录名称 前端 ...
- jquery遍历之后代
向下遍历dom树的jquery方法 children()方法返回被选元素的所有直接子元素,只会对向下一级对dom树进行遍历. 例子 代码: $(document).ready(function(){ ...
- C# 序列化理解 2(转)
一.概述 序列化是把对象转变成流.相反的过程就是反序列化. 哪些场合用到这项技术呢? 1. 把对象保存到本地,下次运行程序时恢复这个对象. 2. 把对象传送到网络的另一台终端上,然后在此终端还原这个对 ...
- MyBatis的SQL语句映射文件详解(三)----多参数传递的几种方式
1.单一基本类型参数(String,int等) 单一的基本类型参数,将对应语句中的parameterType的值与参数的类型相同.然后直接 用“#{参数名}” 来获取 java代码 //String类 ...
- bzoj1499: [NOI2005]瑰丽华尔兹&&codevs1748 单调队列优化dp
这道题 网上题解还是很多很好的 强烈推荐黄学长 码风真的好看 神犇传送门 学习学习 算是道单调队列优化dp的裸题吧 #include<cstdio> #include<cstring ...
- HDU 4320 Arcane Numbers 1 (数论)
A - Arcane Numbers 1 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 常见的makefile写法【转】
转自:http://blog.csdn.net/ghostyu/article/details/7755177 版权声明:本文为博主原创文章,未经博主允许不得转载. .目标名称,摆脱手动设置目标名称 ...
- 记一次CUDA编程任务
这个月6号开始,着手解决一个具有实际意义的计算任务.任务数据有9879896条,每条包含30个整数,任务是计算每两条数据之间的斯皮尔相关系数及其P值.原始数据只有500+MB,因此我并不认为这是个多么 ...