摘要: 先构建一个表达式树,构建方式:从右向左读字符串,如果该字符是大写字母,则构建右子树,接着构建左子树,用递归进行实现。接着层次遍历该树,把得到的结果逆序输出。 代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <queue> 5 #include <cctype> 6 using namespace std; 7 8 const int maxn = 10000+10; 9 10 struct Node11 {1 阅读全文
posted @ 2013-04-24 17:40 xiaobaibuhei 阅读(303) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》6.3.3的题,给出一个二叉树的先序遍历和中序遍历,求它的后续遍历。 书中代码对递归的运用,没得说!递归是一个神奇的东西,我现在只是知道它,但却永不好它,还需努力啊。 代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 30; 5 6 void build(int n, char *s1, char *s2, char *s) 7 { 8 if(n <= 0) return; 9 int p = strchr(s2, s1[0]) - s2.. 阅读全文
posted @ 2013-04-24 16:25 xiaobaibuhei 阅读(352) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》6.3.2的题,关于构建二叉树和层次遍历的问题。 书中的代码定义的节点结构用了一个have_value标记判断是否已有值,因为节点的值都是正整数,可以通过初始化为0来判断是否该节点已有值,另外,ans数组的n应在每次调用bfs函数时进行初始化。 第一次提交的时候PE了,原来是要去掉层次遍历结果的后缀空白符,可是我并没有在题目中找到相关的要求呀... 代码如下:View Code 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 5 const int maxn 阅读全文
posted @ 2013-04-24 15:49 xiaobaibuhei 阅读(400) 评论(0) 推荐(0)
摘要: 浮点数求幂,先把浮点数转化成整数,再按大数乘法计算,最后再考虑加上小数点,对小数点的位置和关于0的处理要考虑清楚。 代码如下:View Code 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 1000; 8 9 struct bign 10 { 11 int d[maxn]; 12 int len; 13 14 bign() 1... 阅读全文
posted @ 2013-04-19 20:09 xiaobaibuhei 阅读(208) 评论(0) 推荐(0)
摘要: 1.scanf函数 最常用的输入函数算是scanf函数了,要注意的就是它遇到空格、tab和回车就会停止读取,并且空格、tab和回车符仍存在于缓冲区中,如果下一个读取的是字符的话就得注意了。2.fgets函数 作用:读取完整的一行数据 用法:char buf[MAXN]; fgets(buf, MAXN, fin); 说明:这个函数一旦读到回车符'\n'就会停止读取,并把'\n'读到字符串中。此外,该函数只读取不超过MAXN-1个字符,然后再末尾添上结束符'\0',因此不会出现越界的情况。 实例:View Code 1 #include <s 阅读全文
posted @ 2013-04-18 18:58 xiaobaibuhei 阅读(314) 评论(0) 推荐(0)
摘要: 大数加法,代码如下:View Code 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 1000; 8 9 struct bign 10 { 11 int d[maxn]; 12 int len; 13 14 bign() 15 { 16 memset(d, 0, sizeof(d)); 17 ... 阅读全文
posted @ 2013-04-18 18:21 xiaobaibuhei 阅读(222) 评论(0) 推荐(0)
摘要: 大数加法,代码如下:View Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 1000; 8 9 struct bign 10 { 11 int d[maxn]; 12 int len; 13 14 bign() 15 { 16 memset(d, 0, sizeof(d)); 17 ... 阅读全文
posted @ 2013-04-18 18:10 xiaobaibuhei 阅读(532) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》5.2.2,关于大数的题,按照书上给的方法超时了,计算次数太多了,没有充分利用int,最后又在网上搜了一下,修改代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 1000; 5 int f[maxn]; 6 7 int main() 8 { 9 #ifdef LOCAL10 freopen("in", "r", stdin);11 #endif12 int n;13 while(scanf("%d& 阅读全文
posted @ 2013-04-18 16:21 xiaobaibuhei 阅读(224) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》5.1.3的题,给定一个字符串,输出它的最小周期,要注意的是最后一个数据后不要跟空行,不然会WA,代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 4 int main() 5 { 6 #ifdef LOCAL 7 freopen("in", "r", stdin); 8 #endif 9 int n;10 scanf("%d", &n);11 char s[100];12 while(n--)13 {14 scanf 阅读全文
posted @ 2013-04-18 14:37 xiaobaibuhei 阅读(1128) 评论(0) 推荐(0)
摘要: 《算法竞赛入门经典》5.1.1的题, 代码如下。View Code 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 char table[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; 6 7 int main() 8 { 9 #ifdef LOCAL10 freopen("in", "r", stdin);11 #endif12 int c;13 wh 阅读全文
posted @ 2013-04-18 13:40 xiaobaibuhei 阅读(135) 评论(0) 推荐(0)