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] != '\0'; i++) {
74             if (str[i] != ' ')
75                 equ[wei++] = str[i];
76         }
77         equ[wei] = '\0';
78         printf("Expression %d:", casec);
79         int forv = solve(0, wei - 1);
80         if (forv)printf(" V\n");
81         else printf(" F\n");
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|     file1file1file2

这个图说明: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
file4DATA 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\n", 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\n", (*(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:\nROOT\n", casec++);
49             solve(1, filec - 1, 0);
50             getline(cin, ch);
51             if (ch[0] == '#')
52                 break;
53             else {
54                 printf("\n");
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("\n");
44     }
45 }
46
47 int main()
48 {
49     int flag = 1;
50     while (cin >> n) {
51         if (!n)break;
52         if (!flag)
53             printf("\n");
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]='\0';
38             puts(trian[i] + 1);
39         }
40         puts("");
41     }
42     return 0;
43 }

View Code

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

转载于:https://www.cnblogs.com/yalphait/p/9038258.html

18.05.14 递归作业相关推荐

  1. 软考中高项学员:2016年3月14日作业

    软考中高项学员:2016年3月14日作业 第四章:项目管理一般知识 1.核心知识域有哪些.保障域有哪些?伴随域有哪些?过程域有哪些? 2.有效的项目管理要求项目管理团队,至少要使用哪六个方面知识? 3 ...

  2. Java SE 第八十八,八十九,九十讲 递归深度剖析 IO流深入详解,递归作业详解

    1.所谓递归(Recursion),就是方法调用自身,对于递归来说,一定有一个出口,让递归结束,只有这样才能保证不出现死循环. 2.作业:给定任意一个目录,以树形方式展现该目录中所有子目录和文件.另外 ...

  3. 人生之路1.18.05优化

    神殿地图扩大至40*100(原先是40*40),并增加技能"瞬移". 地图数组请自行改成41*101 原先是mm[41][41],改成mm[41][101]即可(开大一丢丢也可以) ...

  4. 神经网络贷款风险评估(base on keras and python ) 原创 2017年08月18日 14:35:17 标签: python / 神经网络 / keras 300 用我

    神经网络贷款风险评估(base on keras and python ) 原创 2017年08月18日 14:35:17 标签: python / 神经网络 / keras / 300 编辑 删除 ...

  5. AI公开课:18.05.16 周明博士(MSRA副院长)—北大AI第十一讲之《语言智能的进展》课堂笔记——你了解语言智能

    AI公开课:18.05.16 周明博士(MSRA副院长)-北大AI第十一讲之<语言智能的进展>课堂笔记--你了解语言智能 导读         周明博士,微软亚洲研究院副院长.国际计算语言 ...

  6. AI公开课:18.05.05 施尧耘(阿里云量子技术CS)—清华AI第四讲之《人工智能与量子计算》Quantum课堂笔记——带你了解量子计算

    AI公开课:18.05.05 施尧耘(阿里云量子技术CS)-清华AI第四讲之<人工智能与量子计算>Quantum课堂笔记--带你了解量子计算 导读 清华大学"人工智能前沿与产业趋 ...

  7. 最近任务-2012.05.14

    最近任务: 本周:3个英文短文,30新单词,3个C程序,5个新CSS属性. 2012.05.14(星期一)---2012.05.16(星期三) 1.复习网络各层概念 复习网络TCP/IP协议 2012 ...

  8. Uniswap 24h交易量约6.54亿美元涨18.05%

    智能交易平台Uniswap当前流动代币总价值约为31.8亿美元(+6.35%),24h交易量约为6.54亿美元(+18.05%).涨幅前三代币:PRQ(+42.08%).TRU(+32.32%).AR ...

  9. 【各版本通吃】【2023/05/14更新】通过网易云音乐分享链接找到分享用户主页

    2023/05/14正式更新,支持所有版本解析 简介 曾经网易云音乐的音乐分享链接携带了用户明文 id ,可以直接通过拼接网址得到用户主页. 前天发现官方为这个举措在安全层面下了功夫加固了数据,好歹终 ...

最新文章

  1. 全球研发开支排名:亚马逊第一,BATJ排不上号!
  2. 运维 + 数据 + AI=企业数字化难题的应对之法?
  3. BAT 批处理命令 - 获取时间并进行自定义年月日、时分秒格式实例演示
  4. 线程/协程/异步的编程模型(CPU利用率为核心)
  5. Gerrit评审报错[remote rejected] develop- refs/for/develop(no new changes)
  6. TensorFlow学习笔记(十)tf搭建神经网络可视化结果
  7. [css] 如何使用CSS3的属性设置模拟边框跟border效果一样?
  8. 表达式计算:后缀表达式求解 以及 中缀表达式转换为后缀表达式
  9. 父母延长退休,作为程序员的我光荣失业
  10. android图像处理(3) 底片效果
  11. 嵌套循环连接(Nested Loops), 合并联接(Merge), 哈希联接(Hash)的适用情况
  12. Eclipse alt+/语法不提示的解决方法
  13. 纯CSS3渐变色板配色代码
  14. 推荐几款压箱底的IDEA插件,撸码利器
  15. TeamFlowy——结合Teambition与Workflowy
  16. mysql数据库压缩_Mysql压缩解决方案
  17. 排列组合数学的相邻问题(插空法-捆绑法-隔板法)
  18. content=IE=Edge
  19. 邱跃鹏:互联网下半场,腾讯云要做信息能源发动机
  20. 服务器vmware私有云,方案建议-使用VMware架构搭建自己的私有云.pptx

热门文章

  1. linux内核c语言笔试,C语言之linux内核--BCD码转二进制与二进制转BCD码(笔试经典)...
  2. Springboot中数据库连接
  3. 调用Bytom Chrome插件钱包开发Dapp
  4. 【华为数据之道】作用于数据价值流的全链路元数据管理
  5. c/c++ 运行时: R6034
  6. python 语言变量命名规则的是_Python中变量命名规则有哪些
  7. 蓝桥杯—出栈次序 (JAVA)
  8. 第9节-偏差与方差、联合界定理和一致收敛定理
  9. simulink Stateflow使用
  10. 所谓”不用加号的加法运算“