- 博客(318)
- 收藏
- 关注
原创 HDU 1233 还是畅通工程 基础最小生成树
传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1233#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct Edge {public: int u;
2017-07-15 22:34:17 377
原创 HDU 1232 畅通工程 并查集水题
题意并查集算法最后得到的集合的个数减一就是结果传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1232#include <cstdio>#include <cstring>#include <iostream>using namespace std;class UnionFind {private: int * fa; int
2017-07-15 22:07:28 383
原创 HDU 1102 Constructing Roads 最小生成树
题意最小生成树模板题思路我把sort的cmp函数 e1.dis < e2.dis 写成 e1.dis - e2.dis RE了一下午,SAD传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1102#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#i
2017-07-15 21:57:46 335
原创 HDU 1856 More is better
题意求并查集中最大的一个集合的大小思路Union过后,使用Find找到每个节点的祖先统计即可。传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1856#include <cstdio>#include <cstring>#include <iostream>using namespace std;class UnionFind {private
2017-07-15 12:19:26 279
原创 HDU 1325 Is It A Tree?
题意判断一系列的输入是否是一棵树思路比HDU 1272多判断一个入度,如果入度为0的点不止一个,就肯定不是一颗树传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1325Code#include <cstdio>#include <cstring>#include <iostream>using namespace std;class UnionF
2017-07-15 11:56:30 409
原创 HDU 1272 小希的迷宫 并查集
题意给出一个图,问这个图是否是一棵树思路首先判断这个图有没有环,这个直接用并查集就可以判断,为此我改进了并查集的模板,然后就是判断这个图是不是一片森林,这个需要记录下有多少个点,如果有n个点,有n-1条边就是一棵树Code#include <cstdio>#include <cstring>#include <iostream>using namespace std;class UnionFin
2017-03-31 23:56:56 296
原创 HDU 1213 How Many Tables 并查集
题意n个点,m条边,无向图,求最后形成的集合有多少个。解法基础的并查集传送门 http://acm.hdu.edu.cn/showproblem.php?pid=1213Code#include <cstdio>#include <iostream>using namespace std;class UnionFind {private: int * fa; int siz
2017-03-31 11:44:49 273
原创 SICP 习题2.61~2.62 排序表示的adjoin和union-set函数
adjoin函数要注意的是怎样把x插入当前的序列,union-set的写法可以参考书上给出的intersection-set。my.scm文件将给出一些所有代码经常会用到的逻辑。譬如这次的先换行后打印my.scm(define (out x) (newline) (display x))2.62.scm(load "my.scm")(define (element-of-set? x set
2016-07-22 19:14:02 527
原创 SICP 习题2.60 允许元素重复,重构集合操作
element-of-set? 和intersection-set的复杂度急剧上升,而adjoin-set和union-set的复杂度下降了一个O(n)O(n)。在adjoin-set操作和union-set操作比较多,element-of-set?和intersection-set操作比较少的时候(define (element-of-set? x set) (cond ((null? set
2016-07-22 15:14:17 331
原创 SICP 习题2.59 union-set操作
union-set操作的想法就是找出set1中set2里面没有的,set2中没有的和set2放在一起,就是union-set(define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))
2016-07-22 14:49:17 344
原创 QT PySide 连连看小游戏
学了PySide,花了一天时间做出了以前很想写的一个游戏,然后写完主体的逻辑之后就不想再接着往下写了。在windows下写的6*6的方格,总共6种图像,网上找的6张gif图片,然后把大小用windows的画图变成50*50像素的图像,这是点击之前的图像。然后把gif图中白色的像素涂黑变成点击之后的图像(这个也是用windows画图解决的)。刚接触python,感觉格式会很好,但是self这个有点不爽
2016-07-16 21:50:00 1078
原创 SICP 习题2.53~2.55 equal
2.55 : ‘被替换成了(quote)(define (memq item x) (cond ((null? x) false) ((eq? item (car x)) x) (else (memq item (cdr x)))))(newline)(display (list 'a 'b 'c))(newline)(display (list (list
2016-07-01 19:09:18 454
原创 SICP 习题2.43 八皇后问题flatmap嵌套反了
问题在于(queen-cols (- k 1))被执行过多次了,在之前2.42的里面(queen-cols (-k 1))只执行了一次棋盘大小是NN时,运行时间是T∗NNT*N^N,因为在每一层(queen-cols (- k 1))都要被执行N次,一共有N层。
2016-07-01 00:38:53 429
原创 SICP 习题2.42 八皇后问题
想不到第一次写八皇后问题是用的scm写代码的途中遇到一个错误,用let错了,用define对了,应该是编译器的原因,和nil差不多。(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr
2016-07-01 00:24:58 590
原创 SICP 习题2.41 triple 三元组
非常朴素的想法,找出所有的三元组,然后判定三元组的和是否与s相等(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (enumerate-in
2016-06-29 21:38:55 514
原创 SICP 练习2.40 unique-pairs
我所做的只是把unique-pairs封装了一下(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (enumerate-interval l
2016-06-29 19:47:21 401
原创 SICP 习题2.39 reverse实现
(define reverse-list (lambda (s) (if (null? s) '() (append (reverse-list (cdr s)) (list (car s))))))(newline)(display (reverse-list '(1 2 3 4))) (newline)(display (reverse '(1 2 3
2016-06-24 18:40:07 390
原创 SICP 习题2.38 fold-left fold-right
要使fold-left和fold-right达到同样的结果,要求op对操作顺序无关,譬如+(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define
2016-06-24 17:39:59 320
原创 SICP 习题2.37 矩阵乘法的一些实现
(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (accumulate-n op init seqs) (if (null? (car
2016-06-24 17:24:36 453
原创 SICP 习题2.36 accumulate-n的实现
(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (accumulate-n op init seqs) (if (null? (car
2016-06-24 17:22:36 554
原创 SICP 习题2.35 count-leaves 用accumulate实现
这题开始我想着用emumerate-tree来做,但是这样做根本就不需要accumulate,后来上网看了别人的做法,我对于递归的理解实在是太浅显。(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initi
2016-06-24 16:41:56 516
原创 SICP 练习2.34 多项式求值(horner规则)
(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (horner-eval x coefficient-sequence) (accum
2016-06-24 16:13:50 552
原创 SICP 习题2.33 用accumulate完成一些基本的表操作
这个accumulate比第一章完成更强大(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (map p sequence) (accu
2016-06-24 16:05:34 590
原创 SICP 习题2.32 subsets 寻找子集
写代码的时候遇到一个奇葩的问题,(if (null? s) '() ...)开始我是这样写的,然后发现这样写了之后,map不认账,map rest的时候直接不处理空列表,然后就返回一个()了。(define (subsets s) (if (not (null? s)) (display (car s))) (if (null? s) '(nil) (let
2016-06-24 15:21:06 420
原创 SICP 练习2.31 tree-map
(define (tree-map proc tree) (map (lambda (sub-tree) (cond ((null? sub-tree) ()) ((not (pair? sub-tree)) (proc sub-tree)) (else (tree-map proc sub-tree))))
2016-06-24 14:57:54 278
原创 SICP 练习2.30 square-tree
普通递归写法(define (square-tree tree) (cond ((null? tree) ()) ((not (pair? tree)) (* tree tree)) (else (cons (square-tree (car tree)) (square-tree (cdr tree))))))(squar
2016-06-24 14:47:22 342
原创 SICP 习题2.29 二叉活动体
list版本(define (make-mobile left right) (list left right))(define (make-branch length stucture) (list length stucture))(define (left-branch x) (car x))(define (right-branch x) (car (cdr x)))(def
2016-06-24 14:20:12 412
原创 SICP 联系2.28 实现fringe
(define (fringe x) (cond ((null? (car x)) #f) ((pair? (car x)) (fringe (car x))) (else (out-elem (car x)))) (cond ((null? (cdr x)) #f) ((pair? (cdr x)) (fringe (cdr x))) (else (out-
2016-06-12 22:34:02 377
原创 SICP 联系2.27 实现deep-reverse
(define x (list (list 1 2) (list 3 4)))(define (deep-reverse s) (cond ((not (pair? s)) s) (else (cons (deep-reverse (cdr s)) (deep-reverse (car s))))))(deep-reverse x)
2016-06-12 22:25:02 346
原创 SICP 习题2.23 实现for-each
我还是用了前面那个非常讨巧的办法,两个if(define (for-each proc items) (if (not (null? items)) (proc (car items))) (if (not (null? items)) (for-each proc (cdr items))))(for-each (lambda (x) (newline) (display x)) '(57
2016-06-12 22:00:05 336
原创 SICP 习题2.22 square迭代式写法的失败
代码产生的结果是((((()1)2)3)4)((((()1)2)3)4)而我们需要的是(1(2(3(4))))(1(2(3(4))))(define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons
2016-06-12 21:54:10 341
原创 SICP 习题2.21 square-list
print-list的时候写了三个if,是个愚蠢的地方(define (map-a proc items) (if (null? items) '() (cons (proc (car items)) (map-a proc (cdr items)))))(define nl newline)(define print-list (lambda (s) (
2016-06-12 21:29:49 311
原创 SICP 习题2.20 same-parity
(define same-parity (lambda (x . y) (define iter (lambda (z res) (if (null? z) res (if (= (remainder (car z) 2) (remainder x 2)) (iter (cdr z) (cons res (car z)))
2016-06-12 20:06:56 410
原创 SICP 习题2.19 重写count-change 过程
(define (cc amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (else (+ (cc amount (except-first-denomination coin-values)) (cc (
2016-06-12 18:47:11 1052
原创 SICP 习题2.18 reverse-list
(define reverse-list (lambda (s) (if (null? (cdr s)) (car s) (cons (reverse-list (cdr s)) (car s)))))(reverse-list '(1 4 9 16 25))
2016-06-12 18:15:11 320
原创 SICP 习题2.17 last-pair 找出表的最后一个值
比较简单,判断(cdr s)是否为空即可(define last-pair (lambda (s) (if (null? (cdr s)) (car s) (last-pair (cdr s)))))(last-pair '(23 72 149 34))
2016-06-12 18:09:27 371
原创 SICP 练习2.13 乘积区间的误差
被乘区间(x1,y1)和(x2,y2)(x_1,y_1)和(x_2,y_2)题目中只考虑正数,所以乘积是(x1x2,y1y2)(x_1x_2,y_1y_2)一个区间的误差为y1−x12y1+x12=y1−x1y1+x1\frac{\frac{y_1-x_1}{2}}{\frac{y_1+x_1}{2}}=\frac{y_1-x_1}{y_1+x_1}y1−x1y1+x1+y2−x2y2+x2\fra
2016-06-11 21:36:56 458
原创 SICP 练习2.12 make-interval-percent
(define make-interval cons)(define lower-bound car)(define upper-bound cdr)(define print-interval (lambda (z) (newline) (display (lower-bound z)) (display " ~ ") (display (upper-bou
2016-06-11 21:11:22 410
原创 SICP 习题2.11 改写div-interval 分情况讨论
x正正正负负负y正正正负负负\begin{array}{c|c}x&y\\正正&正正\\正负&正负\\负负&负负\end{array}3*3总共有9种情况,分类讨论即可。div-interval的代码(define (div-interval x y) (let ((x1 (lower-bound x)) (y1 (upper-bound x)) (x2 (/ 1 (lower-
2016-06-11 20:56:36 382
原创 SICP 习题2.10 区间除法 被除区间横跨0的问题
横跨0会导致除法的结果是错的,因为x0\frac{x}{0}是没有结果的(define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y))))(define (mul-interval x y) (let ((p1
2016-06-11 01:15:22 409
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人