递归之 Boolean Expressions

关于题目

描述

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:
Expression: ( V | V ) & F & ( F | V )

where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file.

输入

The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown.

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below.

输出

For each test expression, print “Expression ” followed by its sequence number, “: “, and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as that shown in the sample output shown below.

样例输入

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

样例输出

Expression 1: F
Expression 2: V
Expression 3: V
来源
México and Central America 2004

解题思想

主要是应用递归

开始也看了其他的解题方法,可以用栈解决,不过处理起来比较麻烦。布尔表达式本身就是一个递归的定义,项可以由表达式组成也可以由单个字符组成,表达式可以是 项 & 项 的形式,或者是 项 | 项 的形式,或者是 !项的形式

还有一点需要注意

本题中需注意对空格的处理,所以单独编写了读取函数(函数借鉴于别人)

代码

#include<iostream>
using namespace std;

bool f1();
bool f2();

char getc_()
{
    char c;
    while((c = cin.get()) == ' ');
    return c;
}
char peekc_()
{
    char c;
    while((c = cin.peek()) == ' ')
    {
        cin.get();
    }
    return c;
}

bool f2()
{
    char c = peekc_();
    if(c == '(')
    {
        getc_(); //拿走左括号
        bool d = f1();  
        getc_(); //拿走右括号 
        return d; 
    }
    else
    {
        getc_(); 
        if(c == '!')    return !f2();
        if(c == 'V')    return true;
        if(c == 'F')    return false;
    }
}

bool f1()
{
    char c = peekc_();
    if(c == '\n')   return false ;
    bool a = f2();
    while(true)
    {
        c = peekc_();
        if(c == '|')
        {   
            getc_();
            bool b = f2();
            a = a || b; 
        }
        else if(c == '&')
        {   
            getc_();
            bool b = f2();
            a = a && b;  
        }
        else    break;
    }
    return a;
}
int main()
{
    int n = 0;
    while(cin.peek() != EOF){
        bool f = f1();
        if(cin.peek() == '\n' || cin.peek() == EOF){
            cout<< "Expression " << ++n << ": " << (f?'V':'F') << endl;
            cin.get();
        }
    }
    return 0;
}
发布了48 篇原创文章 · 获赞 17 · 访问量 2万+
展开阅读全文

java递归 if() return返回到哪里?

05-06

学习归并排序时,遇到递归的思想。 测试输入 mergesortexample 单步调试到,if (hi<=lo) return;当hi=0,lo=0时,执行return,在我理解中,return就是退出方法了,为何会跳到 sort(a,mid+1,hi);而且此时,lo=0,hi=1? private static void sort(Comparable[] a,int lo,int hi){ //将数组a【lo hi】排序 if (hi<=lo) return; int mid=lo+(hi-lo)/2; sort(a,lo,mid); sort(a,mid+1,hi); merge(a,lo,mid,hi); } import java.util.Scanner; public class Merge { private static Comparable[] aux; public static void sort(Comparable[] a){ aux=new Comparable[a.length]; sort(a,0,a.length-1); } private static boolean less(Comparable v,Comparable w){ return v.compareTo(w)<0; } public static void merge(Comparable[] a,int lo,int mid,int hi){ //将a【lo mid】与a【mid+1,hi】归并 int i=lo; int j=mid+1; for(int k=lo;k<=hi;k++){ aux[k]=a[k]; } for(int k=lo;k<=hi;k++){ if(i>mid) a[k]=aux[j++]; else if(j>hi) a[k]=aux[i++]; else if(less(aux[j],aux[i])) a[k]=aux[j++]; else a[k]=aux[i++]; } } private static void sort(Comparable[] a,int lo,int hi){ //将数组a【lo hi】排序 if (hi<=lo) return; int mid=lo+(hi-lo)/2; sort(a,lo,mid); sort(a,mid+1,hi); merge(a,lo,mid,hi); } private static void show(Comparable[] a){ //在单行中打印数组 for(int i=0; i<a.length;i++) System.out.print(a[i]+""); System.out.println(); } public static boolean isSorted(Comparable[] a){ //测试数组是否有序 for(int i=1;i<a.length;i++) if(less(a[i],a[i-1])) return false; return true; } public static void main(String[] args){ //从标准输入读取字符串,将它们排序并输出 System.out.print("输入"); Scanner s = new Scanner(System.in); String line=s.nextLine(); System.out.println("输入的是"+line); char [] charArr =line.toCharArray(); String[] strArr = new String[charArr.length]; for(int i = 0; i < strArr.length; i++) { strArr[i] = String.valueOf(charArr[i]); } sort(strArr); assert isSorted(strArr); show(strArr); } } 问答

js递归算法,判断返回true或false

04-19

[ { "id": "510489e1acab613446b69034eada2943", "number": "1", "name": "模块(一)", "superclass": "0", "superclass_name": "无", "higrade": "", "lograde": "", "remark": "", "standard": "科学性和创新性" }, { "id": "b273cb4050abf959a823ff07ef95deec", "number": "1.1", "name": "管理方法", "superclass": "510489e1acab613446b69034eada2943", "superclass_name": "模块(一)", "higrade": "10", "lograde": "", "remark": "", "standard": "所应用的管理方法(技术)符合普遍性认知的科学规律,符合质量品牌管理发展趋势。" }, { "id": "35362a8ba4fbe04a9e23496806910dea", "number": "1.1.1", "name": "大萨达", "superclass": "b273cb4050abf959a823ff07ef95deec", "superclass_name": "管理方法", "higrade": "", "lograde": "", "remark": "", "standard": "" }, { "id": "1cf9f7ec5fb807d5477765c8ddb5b310", "number": "1.2", "name": "创新性经验", "superclass": "510489e1acab613446b69034eada2943", "superclass_name": "模块(一)", "higrade": "15", "lograde": "", "remark": "", "standard": "在管理方法(技术)本身或(和)应用过程中有创新性经验(成果)。" }, { "id": "bc230f233763864c200c465d24feb9ed", "number": "1.3", "name": "核心内容", "superclass": "510489e1acab613446b69034eada2943", "superclass_name": "模块(一)", "higrade": "10", "lograde": "", "remark": "", "standard": "能成功应用该管理方法(技术)的核心内容,且逻辑清晰,内容完整。" }] 上面的数据格式,我新插入一条数据,新插入如果higrade 不为空,且superclass是1.1.1,如果1.1.1的higrade为空,继续找1.1.1的上一级,就是一直向上找直到找到superclass为0为止,这样的递归算法怎么写 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览