文章分类 - Algorithm
摘要:一、递归计算#include using namespace std;/* 问题: 由非负组成的三角形,第一行只有一个数,除了最下行之外 * 每个数的左下方和右下方各有一个数,从第一行的数开始,每次可以 * 往左下或右下走一格,直到走到最下行,沿途的数相加,怎么走和最大? */const int N = 4;const int a[N][N] = {{1}, {3, 2}, {4, 10, 1}, {4, 3, 2, 20}};/* 把当前位置看成一个状态(i, j),定义状...
阅读全文
摘要:巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个,最后取光者得胜。显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少个,后取者都能够一次拿走剩余的物品,后者取胜。因此我们发现了如何取胜的法则:如果n=(m+1)r+s,(r为任意自然数,s≤m),那么先取者要拿走s个物品,如果后取者拿走k(≤m)个,那么先取者再拿走m+1-k个,结果剩下(m+1)(r-1)个,以后保持这样的取法,那么先取者肯定获胜。总之,要保持给对手留下(m+1)的倍数,就能最后获胜。这个游戏还可以有一种变相的玩法:两个人轮流报数,每次至少报一
阅读全文
摘要:对称加密体制,由用户指定自己的密钥key加密函数:M = P + key解密函数:P = M - key因此加密函数和解密函数互为逆函数只要将明文文件中的一个字节代入加密函数中进行运算,得到的结果即为一个字节的密文数据同理将密文文件中的一个字节代入解密函数中进行运算,得到的结果即为一个字节的明文数据#include #include int openSrcFile(char ** buffer) //读取文件{ FILE * myfile_src; char filename[20]; long file_size; printf("Please input the p...
阅读全文
摘要:康托展开的公式把一个整数X展开成如下形式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[2]*1!+a[1]*0! 其中,a为整数,并且0<=a[i]<i(1<=i<=n)康托展开的应用实例{1,2,3,4,...,n}表示1,2,3,...,n的排列如 {1,2,3} 按从小到大排列一共6个。123 132 213 231 312 321 。 代表的数字 1 2 3 4 5 6 也就是把10进制数与一个排列对应起来。 他们间的对应关系可由康托展开来找到。 如我想知道321是{1,2,3}中第几个大的数可以这样考虑
阅读全文
摘要:1 #include 2 const int MAX = 1<<20; 3 int main() 4 { 5 int n,m,p,q,l; 6 while(~scanf("%d%d",&n,&m),n||m) 7 { 8 int map[101][101], mark[101]={0}; 9 for(int i=1; i<=n; i++)10 {11 for(int j=1; j<=n; j++)12 {13 map[i][j] = MAX; //初始化每条...
阅读全文
摘要:chuanbindeng 的 素数判断算法关于素数的算法是信息学竞赛和程序设计竞赛中常考的数论知识,在这里我跟大家讲一下寻找一定范围内素数的几个算法。看了以后相信对大家一定有帮助。 正如大家都知道的那样,一个数 n 如果是合数,那么它的所有的因子不超过sqrt(n)--n的开方,那么我们可以用这个性质用最直观的方法来求出小于等于n的所有的素数。 1 num = 0; 2 3 for(i=2; isqrt(i) ) prime[num++] = i; //这个prime[]是int型,跟下面讲的不同。10 11 } 这就是最一般的求解n以内素数的算法。复杂度是o(n*sqrt...
阅读全文
摘要:得到的并非最优解 1 /* 2 迭代加深搜索解埃及分数 3 19/45=1/3 + 1/12 + 1/180 4 19/45=1/3 + 1/15 + 1/45 5 19/45=1/3 + 1/18 + 1/30 6 19/45=1/4 + 1/6 + 1/180 7 19/45=1/5 + 1/6 + 1/18 8 最后一种最好,因为1/18比1/180,1/45,1/30,1/180都大 9 */10 //按照分母递增的顺序来扩展,若扩展到d层时,11 //前d个分数之和为 now ,而第d个分数为 1 / maxdion,则接下来12 //至少还要( frac - now )...
阅读全文
摘要:用数组模拟的位运算,虽然模拟的很拙劣。。但是至少比普通搜索快1倍。 1 #include <cstdio> 2 #include <cstring> 3 #include <ctime> 4 #include <cstdlib> 5 const int SIZE = 12; 6 int sum=0,row[SIZE]={0},ld[SIZE]={0},rd[SIZE]={0}; 7 void L_m(int *a) 8 { 9 for(int i=0;i<SIZE-1;i++)10 a[i]=a[i+1];11 a[SIZE-1]=0;12
阅读全文
摘要:目前世界最快的N皇后算法,12皇后解25MS左右 1 #include <iostream> 2 #include <ctime> 3 using namespace std; 4 //sum用来记录皇后放置成功的不同布局数;upperlim用来标记所有列都已经放置好了皇后。 5 long sum, upperlim; 6 7 //试探算法从最右边的列开始。 8 void test(long row, long ld, long rd) 9 { 10 if (row != upperlim) 11 ...
阅读全文
摘要:1 #include 2 #include 3 #include 4 #include 5 int ans,N; 6 int map[16][16]; 7 bool isCorrect(int row,int col) 8 { 9 for(int i=0;i=0&&j>=0;i--,j--)16 if(map[i][j]==1)17 return false; 18 for(int i=row+1,j=col-1;i=0;i++,j--)19 if(map[i][j]==1)20 r...
阅读全文
摘要:比DFS快了4倍只用10MS就能得出结果。。 1 #include <cstdio> 2 #include <cstring> 3 #include <ctime> 4 const int XSIZE = 3; 5 const int SIZE = XSIZE * XSIZE; 6 const int MAX_C = SIZE * SIZE * 4; //最大列 7 const int MAX_R = SIZE * SIZE * SIZE; //最大行 8 const int MAX_SUDOKU = S...
阅读全文
摘要:50Ms左右 1 #include <cstdio> 2 #include <cstring> 3 #include <ctime> 4 #include <windows.h> 5 const int XSIZE = 4; //16*16的数独 6 const int _SIZE = XSIZE * XSIZE; 7 const int MAX_SUDOKU = _SIZE * _SIZE; //数独矩阵大小 8 const int MAX_C = _SIZE * _S...
阅读全文
摘要:这个模版是我从SuDoKu_DLX上面分离出来的,我也是刚接触DLX,总的来说只要能转换为精确覆盖问题的问题,都可以用DLX来解决,而且速度绝对是数一数二的。。。 1 const int SIZE = 16; 2 const int MAX_C = SIZE * SIZE * 4; //最大列 3 const int MAX_R = SIZE * SIZE * SIZE; //最大行 4 const int MAX_LINK = MAX_C * MAX_R; //链表最大范围 5 6 ...
阅读全文
摘要:网上的Dancing Links用L[R[x]] ← L[x], R[L[x]] ← R[x]来表示节点的删除用L[R[x]] ← x, R[L[x]] ← x来表示节点的复原搞的我一头雾水,其实就是修改双向链表的左右地址,自己写一个马上有一个深刻的理解了,不过表现的形式有点不一样。 1 #include 2 #include 3 const int MAX=10; 4 typedef struct node 5 { 6 int num; 7 struct node *left,*right; 8 }point; 9 point *head=(point*)malloc(...
阅读全文
摘要:昨天刚接触BFS算法,用的queue队列,算法的理解应该没什么问题,但是BFS和DFS在输出最短的路径上有点不一样,BFS如果要输出路径就必须要做记录,然后在逆向DFS,我用的是逆向链表记录路径,队列弹出推入的都是结构体地址。 1 /* 2 Name: 老鼠走迷宫(二) BFS 3 Copyright: 4 Author: 顾骏 5 Date: 23/05/13 22:14 6 Description: BFS 7 */ 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <queue> 11 ...
阅读全文
摘要:40Ms 1 #include <stdio.h> 2 #include <string.h> 3 #include <time.h> 4 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 5 int num[9][9]; 6 void Dfs(int,int); 7 bool Check(); //检测数独数组是否满足要求 8 bool Check_(int,int,int); //检测一行&一列&3*3小格子是否有相同的数 9 10 void input(void) 11 { 12 char str[1
阅读全文