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. 移动端网页乱象怎么破?搜狗搜索正在为行业建言
  2. 10远程连接连接不上华为云_从云手机到云游戏,5G会在多大程度上改变我们的生活?...
  3. python列表反转的三种方式
  4. MySQL 笔记4 -- 数据完整性
  5. kafka高性能揭秘:顺序写和零拷贝
  6. 比特(bit)_二进制数
  7. [渝粤教育] 西南科技大学 经济数学1 在线考试复习资料
  8. 新年寄语 | 2018 以及 Oracle 18c 一个时代的开启
  9. Initializing Java Tooling 30% 停住不动了
  10. 琉璃男主成毅手机壁纸,你要么?
  11. SharedObject实例.
  12. axure 发布 主页_【最新实习发布!】滴滴后台/数据产品经理实习生
  13. 虹软科技Java人脸识别_java人脸识别 虹软ArcFace 2.0,java SDK使用、人脸识别-抽取人脸特征并做比对...
  14. python实战一个完整的项目-这4个Python实战项目,让你瞬间读懂Python!
  15. 微信小程序云开发数据导出为Excel下载并打开
  16. 【android】简易的登陆界面的xml设计——代码复用,节省时间
  17. 一文读懂区块链与大数据的关系
  18. Openssl下载网址
  19. 计算机基础题选择题,计算机基础知识题库选择题.doc
  20. node.js 最全命令行配置操作win10

热门文章

  1. Erlang解析JSON之Jiffy篇
  2. 嵌入式与STM32原理及电路分析(STM32一)
  3. 熬夜整理!200道Python数据分析习题+50个办公自动化案例!
  4. 银行自动化案例丨云扩RPA助潍坊银行深挖数字金融服务新动能
  5. 解决电脑出现 R6034 Runtime Error
  6. D3 中国地图json 数据
  7. 函数练习220216
  8. 华为交换机配置远程管理地址
  9. Java8 Stream List<Bean> 或List<Map> 转Map
  10. Android Studio,Error type 3 Error: Activity class {com.xxx/com.xxx.MainActivity} does not exist