1、ACM 程序设计算法讲解-1-目录1.河内之塔 .32.Algorithm Gossip: 费式数列 .43.巴斯卡三角形 .54.Algorithm Gossip: 三色棋 .65.Algorithm Gossip: 老鼠走迷官(一) .86.Algorithm Gossip: 老鼠走迷官(二) .107.Algorithm Gossip: 骑士走棋盘 .118.Algorithm Gossip: 八皇后 .149.Algorithm Gossip: 八枚银币 .1610.Algorithm Gossip: 生命游戏 .1811.Algorithm Gossip: 字串核对 .2112.Al
2、gorithm Gossip: 双色、三色河内塔 .2313.Algorithm Gossip: 背包问题(Knapsack Problem) .2714.Algorithm Gossip: 蒙地卡罗法求 PI .3215.Algorithm Gossip: Eratosthenes 筛选求质数 .3316.Algorithm Gossip: 超长整数运算(大数运算) .3517.Algorithm Gossip: 长 PI .3718.Algorithm Gossip: 最大公因数、最小公倍数、因式分解 .4019.Algorithm Gossip: 完美数 .4320.Algorithm
3、Gossip: 阿姆斯壮数 .4621.Algorithm Gossip: 最大访客数 .4822.Algorithm Gossip: 中序式转后序式(前序式) .5023.Algorithm Gossip: 后序式的运算 .5324.Algorithm Gossip: 洗扑克牌(乱数排列) .5525.Algorithm Gossip: Craps 赌博游戏 5726.Algorithm Gossip: 约瑟夫问题(Josephus Problem) 5927.Algorithm Gossip: 排列组合 .6128.Algorithm Gossip: 格雷码(Gray Code) 6329
4、.Algorithm Gossip: 产生可能的集合 .6530.Algorithm Gossip: m 元素集合的 n 个元素子集 6831.Algorithm Gossip: 数字拆解 .7032.Algorithm Gossip: 得分排行 .7333.Algorithm Gossip: 选择、插入、气泡排序 .7534.Algorithm Gossip: Shell 排序法 - 改良的插入排序 7935.Algorithm Gossip: Shaker 排序法 - 改良的气泡排序 8236.排序法 - 改良的选择排序 8437.Algorithm Gossip: 快速排序法(一) .8
5、838.Algorithm Gossip: 快速排序法(二) .9039.Algorithm Gossip: 快速排序法(三) .92ACM 程序设计算法讲解-2-40.Algorithm Gossip: 合并排序法 .9541.Algorithm Gossip: 基数排序法 .9842.Algorithm Gossip: 循序搜寻法(使用卫兵) .10043.Algorithm Gossip: 二分搜寻法(搜寻原则的代表) .10244.Algorithm Gossip: 插补搜寻法 .10545.Algorithm Gossip: 费氏搜寻法 .10846.Algorithm Gossip
6、: 稀疏矩阵 .11247.Algorithm Gossip: 多维矩阵转一维矩阵 .11448.Algorithm Gossip: 上三角、下三角、对称矩阵 .11549.Algorithm Gossip: 奇数魔方阵 .11850.Algorithm Gossip: 4N 魔方阵 .11951.Algorithm Gossip: 2(2N+1) 魔方阵 121ACM 程序设计算法讲解-3-1.河内之塔说明河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市;1883年法国数学家 Edouard
7、Lucas曾提及这个故事,据说创世纪时Benares有一座波罗教塔,是由三支钻石棒( Pag)所支撑,开始时神在第一根棒上放置64个由上至下依由小至大排列的金盘(Disc) ,并命令僧侣将所有的金盘从第一根石棒移至第三根石棒,且搬运过程中遵守大盘子在小盘子之下的原则,若每日仅搬一个盘子,则当盘子全数搬运完毕之时,此塔将毁损,而也就是世界末日来临之时。解法如果柱子标为ABC,要由A搬至C ,在只有一个盘子时,就将它直接搬至C,当有两个盘子,就将B当作辅助柱。如果盘数超过 2个,将第三个以下的盘子遮起来,就很简单了,每次处理两个盘子,也就是:A-B、A -C、B-C这三个步骤,而被遮住的部份,其实
8、就是进入程式的递回处理。事实上,若有n个盘子,则移动完毕所需之次数为2n - 1,所以当盘数为64时,则所需次数为:2 64- 1 = 18446744073709551615为5.05390248594782e+16年,也就是约5000世纪,如果对这数字没什幺概念,就假设每秒钟搬一个盘子好了,也要约5850亿年左右。#include void hanoi(int n, char A, char B, char C) if(n = 1) printf(“Move sheet %d from %c to %cn“, n, A, C);else hanoi(n-1, A, C, B);printf
9、(“Move sheet %d from %c to %cn“, n, A, C);hanoi(n-1, B, A, C);int main() int n;printf(“请输入盘数:“);scanf(“%d“, hanoi(n, A, B, C);return 0; ACM 程序设计算法讲解-4-2.Algorithm Gossip: 费式数列说明Fibonacci为1200年代的欧洲数学家,在他的着作中曾经提到:若有一只免子每个月生一只小免子,一个月后小免子也开始生产。起初只有一只免子,一个月后就有两只免子,二个月后有三只免子,三个月后有五只免子(小免子投入生产)。 如果不太理解这个例子
10、的话,举个图就知道了,注意新生的小免子需一个月成长期才会投入生产,类似的道理也可以用于植物的生长,这就是Fibonacci数列,一般习惯称之为费氏数列,例如以下: 1、1 、2、3、5、8、13、21、34、55、89 解法依说明,我们可以将费氏数列定义为以下: fn = fn-1 + fn-2 if n 1fn = n if n = 0, 1 #include #include #define N 20 int main(void) int FibN = 0; int i; Fib0 = 0; Fib1 = 1; for(i = 2; i #define N 12long combi(int
11、 n, int r)int i;long p = 1;for(i = 1; i #include #include #define BLUE b #define WHITE w #define RED r #define SWAP(x, y) char temp; temp = colorx; colorx = colory; colory = temp; ACM 程序设计算法讲解-7-int main() char color = r, w, b, w, w, b, r, b, w, r, 0; int wFlag = 0;int bFlag = 0;int rFlag = strlen(c
12、olor) - 1;int i; for(i = 0; i #include int visit(int, int); int maze77 = 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 0, 2, 0, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2; int startI = 1, startJ = 1; / 入口int endI = 5, endJ = 5; / 出口int success = 0;int
13、main(void) int i, j; printf(“显示迷宫:n“); for(i = 0; i #include void visit(int, int);int maze99 = 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 0, 0, 0, 0, 0, 0, 0, 2,2, 0, 2, 2, 0, 2, 2, 0, 2,2, 0, 2, 0, 0, 2, 0, 0, 2,2, 0, 2, 0, 2, 0, 2, 0, 2,2, 0, 0, 0, 0, 0, 2, 0, 2,2, 2, 0, 2, 2, 0, 2, 2, 2,2, 0, 0, 0, 0, 0, 0, 0,
14、 2,2, 2, 2, 2, 2, 2, 2, 2, 2;int startI = 1, startJ = 1; / 入口int endI = 7, endJ = 7; / 出口int main(void) int i, j; printf(“显示迷宫:n“); for(i = 0; i int board88 = 0; int main(void) int startx, starty;int i, j;printf(“输入起始点:“);scanf(“%d %d“, if(travel(startx, starty) printf(“游历完成!n“);else printf(“游历失败!n“
15、);for(i = 0; i 7 | tmpj 7)continue;/ 如果这个方向可走,记录下来if(boardtmpitmpj = 0) nextil = tmpi;nextjl = tmpj;/ 可走的方向加一个l+;count = l;/ 如果可走的方向为0个,返回if(count = 0) return 0;else if(count = 1) / 只有一个可走的方向/ 所以直接是最少出路的方向min = 0;ACM 程序设计算法讲解-14-else / 找出下一个位置的出路数for(l = 0; l 7 | tmpj 7) continue;if(boardtmpitmpj =
16、0)existsl+;tmp = exists0;min = 0;/ 从可走的方向中寻找最少出路的方向for(l = 1; l #include #define N 8 int columnN+1; / 同栏是否有皇后,1表示有 int rup2*N+1; / 右上至左下是否有皇后 int lup2*N+1; / 左上至右下是否有皇后 int queenN+1 = 0; int num; / 解答编号 void backtrack(int); / 递回求解 int main(void) int i; num = 0; for(i = 1; i N) showAnswer(); else for
17、(j = 1; j #include #include void compare(int, int, int, int); void eightcoins(int); int main(void) int coins8 = 0; int i; srand(time(NULL); for(i = 0; i coinsk) printf(“n假币 %d 较重“, i+1); else printf(“n假币 %d 较轻“, j+1); ACM 程序设计算法讲解-18-void eightcoins(int coins) if(coins0+coins1+coins2 = coins3+coins4
18、+coins5) if(coins6 coins7) compare(coins, 6, 7, 0); else compare(coins, 7, 6, 0); else if(coins0+coins1+coins2 coins3+coins4+coins5) if(coins0+coins3 = coins1+coins4) compare(coins, 2, 5, 0); else if(coins0+coins3 coins1+coins4) compare(coins, 0, 4, 1); if(coins0+coins3 coins1+coins4) compare(coins,
19、 3, 1, 0); if(coins0+coins3 #include #include #define MAXROW 10 #define MAXCOL 25 #define DEAD 0 #define ALIVE 1 int mapMAXROWMAXCOL, newmapMAXROWMAXCOL; void init(); int neighbors(int, int);void outputMap();void copyMap();int main() int row, col; char ans; init();while(1) outputMap();for(row = 0; r
20、ow = MAXROW | c = MAXCOL) continue; if(maprc = ALIVE) count+; if(maprowcol = ALIVE) count-; return count; void outputMap() int row, col; printf(“nn%20cGame of life cell statusn“); for(row = 0; row #include #include void table(char*); / 建立前进表 int search(int, char*, char*); / 搜寻关键字 void substring(char
21、*, char*, int, int); / 取出子字串 int skip256; int main(void) char str_input80; char str_key80; char tmp80 = 0; int m, n, p; printf(“请输入字串:“); gets(str_input); printf(“请输入搜寻关键字:“); gets(str_key); m = strlen(str_input); / 计算字串长度 n = strlen(str_key); table(str_key); p = search(n-1, str_input, str_key); whi
22、le(p != -1) substring(str_input, tmp, p, m); printf(“%sn“, tmp); p = search(p+n+1, str_input, str_key); printf(“n“); return 0; void table(char *key) ACM 程序设计算法讲解-23-int k, n; n = strlen(key); for(k = 0; k void hanoi(int disks, char source, char temp, char target) if (disks = 1) printf(“move disk fro
23、m %c to %cn“, source, target);printf(“move disk from %c to %cn“, source, target); else hanoi(disks-1, source, target, temp);hanoi(1, source, temp, target);hanoi(disks-1, temp, source, target);void hanoi2colors(int disks) char source = A;char temp = B;char target = C;int i;for(i = disks / 2; i 1; i-)
24、 hanoi(i-1, source, temp, target);printf(“move disk from %c to %cn“, source, temp);printf(“move disk from %c to %cn“, source, temp);hanoi(i-1, target, temp, source);printf(“move disk from %c to %cn“, temp, target);printf(“move disk from %c to %cn“, source, temp);printf(“move disk from %c to %cn“, so
25、urce, target);int main() ACM 程序设计算法讲解-26-int n;printf(“请输入盘数:“);scanf(“%d“, hanoi2colors(n);return 0; 三色河内塔 C 实作 #include void hanoi(int disks, char source, char temp, char target) if (disks = 1) printf(“move disk from %c to %cn“, source, target);printf(“move disk from %c to %cn“, source, target);pr
26、intf(“move disk from %c to %cn“, source, target); else hanoi(disks-1, source, target, temp);hanoi(1, source, temp, target);hanoi(disks-1, temp, source, target);void hanoi3colors(int disks) char source = A;char temp = B;char target = C;int i;if(disks = 3) printf(“move disk from %c to %cn“, source, te
27、mp);printf(“move disk from %c to %cn“, source, temp);printf(“move disk from %c to %cn“, source, target);printf(“move disk from %c to %cn“, temp, target);printf(“move disk from %c to %cn“, temp, source);printf(“move disk from %c to %cn“, target, temp);else hanoi(disks/3-1, source, temp, target);print
28、f(“move disk from %c to %cn“, source, temp);printf(“move disk from %c to %cn“, source, temp);ACM 程序设计算法讲解-27-printf(“move disk from %c to %cn“, source, temp);hanoi(disks/3-1, target, temp, source);printf(“move disk from %c to %cn“, temp, target);printf(“move disk from %c to %cn“, temp, target);print
29、f(“move disk from %c to %cn“, temp, target);hanoi(disks/3-1, source, target, temp);printf(“move disk from %c to %cn“, target, source);printf(“move disk from %c to %cn“, target, source);hanoi(disks/3-1, temp, source, target);printf(“move disk from %c to %cn“, source, temp);for (i = disks / 3 - 1; i 0
30、; i-) if (i1) hanoi(i-1, target, source, temp);printf(“move disk from %c to %cn“,target, source);printf(“move disk from %c to %cn“,target, source);if (i1) hanoi(i-1, temp, source, target);printf(“move disk from %c to %cn“, source, temp);int main() int n;printf(“请输入盘数:“);scanf(“%d“, hanoi3colors(n);r
31、eturn 0; 13.Algorithm Gossip: 背包问题(Knapsack Problem)说明假设有一个背包的负重最多可达8公斤,而希望在背包中装入负重范围内可得之总价物ACM 程序设计算法讲解-28-品,假设是水果好了,水果的编号、单价与重量如下所示: 0 李子 4KG NT$4500 1 苹果 5KG NT$5700 2 橘子 2KG NT$2250 3 草莓 1KG NT$1100 4 甜瓜 6KG NT$6700 解法背包问题是关于最佳化的问题,要解最佳化问题可以使用动态规划 (Dynamic programming) ,从空集合开始,每增加一个元素就先求出该阶段的最佳解
32、,直到所有的元素加入至集合中,最后得到的就是最佳解。 以背包问题为例,我们使用两个阵列value与item ,value表示目前的最佳解所得之总价,item表示最后一个放至背包的水果,假设有负重量 18的背包8个,并对每个背包求其最佳解。 逐步将水果放入背包中,并求该阶段的最佳解:放入李子 背包负重 1 2 3 4 5 6 7 8 value 4500 4500 4500 4500 9000 item 放入苹果 背包负重 1 2 3 4 5 6 7 8 value 4500 5700 5700 5700 9000 item 1 1 1 放入橘子 背包负重 1 2 3 4 5 6 7 8 ACM
33、 程序设计算法讲解-29-value 2250 2250 4500 5700 6750 7950 9000 item 2 2 1 2 2 放入草莓 背包负重 1 2 3 4 5 6 7 8 value 1100 2250 3350 4500 5700 6800 7950 9050 item 3 2 3 1 3 2 3 放入甜瓜 背包负重 1 2 3 4 5 6 7 8 value 1100 2250 3350 4500 5700 6800 7950 9050 item 3 2 3 1 3 2 3 由最后一个表格,可以得知在背包负重8公斤时,最多可以装入9050元的水果,而最后一个装入的 水果是
34、3号,也就是草莓,装入了草莓,背包只能再放入7公斤(8-1)的水果,所以必须看背包负重7公斤时的最佳解,最后一个放入的是2号,也就 是橘子,现在背包剩下负重量5公斤(7-2) ,所以看负重5公斤的最佳解,最后放入的是 1号,也就是苹果,此时背包负重量剩下0公斤(5-5) ,无法 再放入水果,所以求出最佳解为放入草莓、橘子与苹果,而总价为9050元。实作C #include #include #define LIMIT 8 / 重量限制 #define N 5 / 物品种类 #define MIN 1 / 最小重量 struct body char name20; ACM 程序设计算法讲解-30
35、-int size; int price; ; typedef struct body object; int main(void) int itemLIMIT+1 = 0; int valueLIMIT+1 = 0; int newvalue, i, s, p; object a = “李子“, 4, 4500, “苹果“, 5, 5700, “橘子“, 2, 2250, “草莓“, 1, 1100, “甜瓜“, 6, 6700; for(i = 0; i values) / 找到阶段最佳解 values = newvalue; items = i; printf(“物品t价格n“); for(i = LIMIT; i = MIN; i = i - aitemi.size) printf(“%st%dn“, aitemi.name, aitemi.price); printf(“合计t%dn“, valueLIMIT); return 0; Java class Fruit private String name;