zoukankan      html  css  js  c++  java
  • 18.05.14 递归作业

    A: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

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <stack>
     5 using namespace std;
     6 const int maxn = 200;
     7 char str[maxn],equ[maxn];
     8 int casec = 0;
     9 
    10 void prac(stack<int>&num, stack<char>&mark) {
    11     int now = num.top();
    12     num.pop();
    13     num.push(!now);
    14     mark.pop();
    15 }
    16 void and0(stack<int>&num,stack<char>&mark) {
    17     int x1 = num.top(); num.pop();
    18     int x2 = num.top(); num.pop();
    19     int res = x1 & x2;
    20     num.push(res);
    21     mark.pop();
    22 }
    23 int solve(int s, int e) {
    24     stack<int> num;
    25     stack<char> mark;
    26     for (int i = s; i <= e; i++) {
    27         if (equ[i] == '(') {
    28             int count = 1, reach;
    29             for (int j = i + 1; j <= e; j++) {
    30                 if (equ[j] == '(')count++;
    31                 else if (equ[j] == ')')count--;
    32                 if (!count) { reach = j; break; }
    33             }
    34             num.push(solve(i + 1, reach - 1));
    35             i = reach;
    36         }
    37         else if (equ[i] == '!')
    38             mark.push('!');
    39         else if (equ[i] == '&') {
    40                 while (!mark.empty() && mark.top() == '!')
    41                     prac(num,mark);
    42             mark.push('&');
    43         }
    44         else if (equ[i] == '|') {
    45                 while (!mark.empty()&&mark.top() == '!')
    46                     prac(num, mark);
    47                 while (!mark.empty()&&mark.top() == '&')
    48                     and0(num, mark);
    49             mark.push('|');
    50         }
    51         else if (equ[i] == 'F')num.push(0);
    52         else if (equ[i] == 'V')num.push(1);
    53     }
    54     if (!mark.empty()) {
    55         while (!mark.empty() && mark.top() == '!')prac(num, mark);
    56         while (!mark.empty() && mark.top() == '&')and0(num, mark);
    57         while (!mark.empty() && mark.top() == '|') {
    58             int x1 = num.top(); num.pop();
    59             int x2 = num.top(); num.pop();
    60             int res = x1 | x2;
    61             num.push(res);
    62             mark.pop();
    63         }
    64     }
    65     int fi = num.top();
    66     num.pop();
    67     return fi;
    68 }
    69 void init() {
    70     while (cin.getline(str, maxn)) {
    71         casec++;
    72         int wei = 0;
    73         for (int i = 0; str[i] != ''; i++) {
    74             if (str[i] != ' ') 
    75                 equ[wei++] = str[i];
    76         }
    77         equ[wei] = '';
    78         printf("Expression %d:", casec);
    79         int forv = solve(0, wei - 1);
    80         if (forv)printf(" V
    ");
    81         else printf(" F
    ");
    82     }
    83 }
    84 
    85 int main()
    86 {
    87     init();
    88     return 0;
    89 }
    View Code

    老题重做 又是你

    B:文件结构“图”

    描述

    在计算机上看到文件系统的结构通常很有用。Microsoft Windows上面的"explorer"程序就是这样的一个例子。但是在有图形界面之前,没有图形化的表示方法的,那时候最好的方式是把目录和文件的结构显示成一个"图"的样子,而且使用缩排的形式来表示目录的结构。比如:

    ROOT
    | dir1
    | file1
    | file2
    | file3
    | dir2
    | dir3
    | file1
    file1
    file2

    这个图说明:ROOT目录包括三个子目录和两个文件。第一个子目录包含3个文件,第二个子目录是空的,第三个子目录包含一个文件。

    输入你的任务是写一个程序读取一些测试数据。每组测试数据表示一个计算机的文件结构。每组测试数据以'*'结尾,而所有合理的输入数据以'#'结尾。一组测试数据包括一些文件和目录的名字(虽然在输入中我们没有给出,但是我们总假设ROOT目录是最外层的目录)。在输入中,以']'表示一个目录的内容的结束。目录名字的第一个字母是'd',文件名字的第一个字母是'f'。文件名可能有扩展名也可能没有(比如fmyfile.dat和fmyfile)。文件和目录的名字中都不包括空格,长度都不超过30。一个目录下的子目录个数和文件个数之和不超过30。输出在显示一个目录中内容的时候,先显示其中的子目录(如果有的话),然后再显示文件(如果有的话)。文件要求按照名字的字母表的顺序显示(目录不用按照名字的字母表顺序显示,只需要按照目录出现的先后显示)。对每一组测试数据,我们要先输出"DATA SET x:",这里x是测试数据的编号(从1开始)。在两组测试数据之间要输出一个空行来隔开。

    你需要注意的是,我们使用一个'|'和5个空格来表示出缩排的层次。样例输入

    file1
    file2
    dir3
    dir2
    file1
    file2
    ]
    ]
    file4
    dir1
    ]
    file3
    *
    file2
    file1
    *
    #

    样例输出

    DATA SET 1:
    ROOT
    |     dir3
    |     |     dir2
    |     |     file1
    |     |     file2
    |     dir1
    file1
    file2
    file3
    file4
    
    DATA SET 2:
    ROOT
    file1
    file2
    

    提示一个目录和它的子目录处于不同的层次
    一个目录和它的里面的文件处于同一层次来源翻译自 Pacific Northwest 1998 的试题

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <stack>
     5 #include <queue>
     6 #include <set>
     7 using namespace std;
     8 const int maxn = 1000;
     9 const int maxlen = 35;
    10 string filename[maxn];
    11 
    12 void solve(int s,int e,int level) {
    13     set<string> que;
    14     for (int i = s; i <= e; i++) {
    15         if (filename[i][0] == 'f')
    16             que.insert(filename[i]);
    17         else if (filename[i][0] == 'd') {
    18             int count = 1;
    19             for (int k = 0; k <= level; k++)
    20                 printf("|     ");
    21             printf("%s
    ", filename[i].c_str());
    22             for (int j = i + 1; j <= e; j++) {
    23                 if (filename[j][0] == 'd')count++;
    24                 else if (filename[j] == "]") {
    25                     count--;
    26                     if (count == 0) {
    27                         if (i != j - 1) solve(i + 1, j - 1,level+1);
    28                         i = j ;
    29                         break;
    30                     }
    31                 }
    32             }
    33         }
    34     }
    35     while (!que.empty()) {
    36         for (int k = 0; k < level; k++)
    37             printf("|     ");
    38         printf("%s
    ", (*(que.begin())).c_str());
    39         que.erase(que.begin());
    40     }
    41 }
    42 void init() {
    43     string ch;
    44     int casec = 1, filec = 1;
    45     while (getline(cin,ch)) {
    46         if (ch[0] == '*')
    47         {
    48             printf("DATA SET %d:
    ROOT
    ", casec++);
    49             solve(1, filec - 1, 0);
    50             getline(cin, ch);
    51             if (ch[0] == '#')
    52                 break;
    53             else {
    54                 printf("
    ");
    55                 filec = 1;
    56                 filename[filec++]=ch;
    57             }
    58         }
    59         else
    60             filename[filec++]= ch;
    61     }
    62 }
    63 
    64 int main()
    65 {
    66     init();
    67     return 0;
    68 }
    View Code

    一开始Presentation Error

    是因为两个case之间有一行空

    C:The Sierpinski Fractal

    描述

    Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we'd obtain something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that's why it is called a fractal). By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1. 

    For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you'll need to grow the picture appropriately. Draw the smallest triangle (that is not divided any further) with two slashes, to backslashes and two underscores like this: 

     /
    /__

    To see how to draw larger triangles, take a look at the sample output.

    输入The input contains several testcases. Each is specified by an integer n. Input is terminated by n=0. Otherwise 1<=n<=10 indicates the recursion depth.输出For each test case draw an outline of the Sierpinski Triangle with a side's total length of 2ncharacters. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.样例输入

    3
    2
    1
    0
    

    样例输出

           /
          /__
         /  /
        /__/__
       /      /
      /__    /__
     /  /  /  /
    /__/__/__/__
    
       /
      /__
     /  /
    /__/__
    
     /
    /__
    

    提示


    The Sierpinski-Triangle up to recursion depth 7

    来源Ulm Local 2002

    这道……

    可能是因为在课上做的原因……大概是思维比较混乱……我因为数组开小了所以debug了有至少一个半小时……很坑的是因为我的数组只比应有的小一点点所以报错是WA……

    所以我一直以为是我写得太挫了(不过确实很挫,想都没想就很耿直地写了,结果跑了一百多毫秒),然后去网上找了答案,因为没改数组所以当然还是WA……就这么……一个多小时过去了……

    我甚至一个字一个字的跟人家的代码比较过去……到最后除了数组大小都和人家的代码一毛一样……

    最后才发现……是这个原因……

    就跟我考试的时候发生的情况一模一样,以为是这儿错了,然而并不是……而且错的太nc

    这个故事告诉我们,上课不要写代码。

    所以我有两份答案:

    我的巨慢巨长代码

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <memory.h>
     5 #include <queue>
     6 #include <math.h>
     7 using namespace std;
     8 const int maxn = 1025;
     9 char trian[maxn][2*maxn];
    10 int n;
    11 
    12 void solve(int s, int e, int bian) {
    13     int fen = (e - s + 1) / 2 + s - 1;//中间行
    14     int ban = (e - s + 1) / 2;//选定值的一半
    15     if (ban == 1)return;
    16     for (int j = bian + (ban + 2); j <= 3 * ban - 1 + bian; j++)
    17         trian[fen][j] = '_';
    18     for (int i = fen + 1; i <= e; i++) {
    19         int y1 = ban + i - fen + bian, y2 = 3 * ban - (i - fen) + 1 + bian;
    20         trian[i][y1] = '\', trian[i][y2] = '/';
    21     }
    22     solve(s, fen, bian + ban);
    23     solve(fen + 1, e, bian);
    24     solve(fen + 1, e, bian + ban * 2);
    25 }
    26 void init() {
    27     int bian = pow(2, n);
    28     for (int i = 1; i <= bian; i++)
    29         for (int j = 1; j <= bian + i; j++) {
    30             if (j == bian - i + 1)
    31                 trian[i][j] = '/';
    32             else if (j == bian + i)
    33                 trian[i][j] = 92;
    34             else
    35                 trian[i][j] = ' ';
    36         }
    37     for (int j = 2; j <= bian * 2 - 1; j++)trian[bian][j] = '_';
    38     solve(1, bian, 0);
    39     for (int i = 1; i <= bian; i++)
    40     {
    41         for (int j = 1; j <= bian + i; j++)
    42             printf("%c", trian[i][j]);
    43         printf("
    ");
    44     }
    45 }
    46 
    47 int main()
    48 {
    49     int flag = 1;
    50     while (cin >> n) {
    51         if (!n)break;
    52         if (!flag)
    53             printf("
    ");
    54         init();
    55         flag = 0;
    56     }
    57     return 0;
    58 }
    View Code

    大佬的超快超短代码

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <memory.h>
     5 #include <queue>
     6 #include <math.h>
     7 using namespace std;
     8 const int maxn =2050;
     9 char trian[maxn][maxn];
    10 
    11 void solve(int n,int x,int y) {
    12     if (n == 1) {
    13         trian[x][y] = trian[x + 1][y - 1] = '/';
    14         trian[x][y+1] = trian[x + 1][y + 2] = '\';
    15         trian[x+1][y] = trian[x + 1][y + 1] = '_';
    16         return;
    17     }
    18     int _n = 1 << (n - 1);
    19     solve(n - 1, x, y);
    20     solve(n - 1, x + _n, y - _n);
    21     solve(n - 1, x + _n, y + _n);
    22 }
    23 
    24 int main()
    25 {
    26     int n;
    27     while (~scanf("%d", &n)&&n) {
    28         int h = (1 << n);
    29         int w = (1 << (n + 1));
    30         for (int i = 1; i <= h; i++)
    31             for (int j = 1; j <= w; j++)
    32                 trian[i][j] = ' ';
    33         solve(n,1,1<<n);
    34         int k = (1 << n) + 1;
    35         for (int i = 1; i <= h; i++)
    36         {
    37             trian[i][k + i]='';
    38             puts(trian[i] + 1);
    39         }
    40         puts("");
    41     }
    42     return 0;
    43 }
    View Code

    今日份的许愿:这周的ddl能赶完。

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    git 使用
    使用Xcode 7 beta免费真机调试iOS应用程序
    Java类更改常量后编译不生效
    Spring AOP中pointcut expression表达式解析
    awk
    sed
    Servlet 单例多线程
    iOS 运行时添加属性和方法
    Lucene5学习之使用MMSeg4j分词器
    redis
  • 原文地址:https://www.cnblogs.com/yalphait/p/9038258.html
Copyright © 2011-2022 走看看