摘要: 1 int dp[N]; 2 void OneZeroPack( int v , int c , int w ){//01背包 3 for( int i = v ; i >= c ; i-- ) 4 dp[i] = max( dp[i] , dp[i-c] +w ) ; 5 } 6 void CompletePack( int v , int c , int w ){//完全背包 7 for( int i = c ; i <= v ; i++ ) 8 dp[i] = max( dp[i] , dp[i-c] +w ) ;... 阅读全文
posted @ 2013-04-11 18:03 码代码的猿猿 阅读(227) 评论(0) 推荐(0) 编辑
摘要: 动态规划 的 代表作:其实,DP就是把递归写的程序 改成带备忘记录的,或者至底而上的来避免重复计算同一子问题而已。。。。。 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int a[9]={9999,1,0,0,1,0,1,0,1}; 8 int b[10]={-9999,0,1,0,1,1,0,1,1,0}; 9 10 int i,j;11 12 char s[9][10];//记录移动方向13 int c[9][10];//记录从a[i]b[j]的最长子串的... 阅读全文
posted @ 2013-04-11 18:00 码代码的猿猿 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 计算组合数最大的困难在于数据的溢出,对于大于150的整数n求阶乘很容易超出double类型的范围,那么当C(n,m)中的n=200时,直接用组合公式计算基本就无望了。另外一个难点就是效率。 对于第一个数据溢出的问题,可以这样解决。因为组合数公式为: C(n,m) = n!/(m!(n-m)!) 为了避免直接计算n的阶乘,对公式两边取对数,于是得到: ln(C(n,m)) = ln(n!)-ln(m!)-ln((n-m)!) 进一步化简得到:这样我们就把连乘转换为了连加,因为ln(n)总是很小的,所以上式很难出现数据溢出。 为了解决第二个效率的问题,我们对上式再做一步化简。上式已经把连乘法变成. 阅读全文
posted @ 2013-04-11 17:58 码代码的猿猿 阅读(1960) 评论(0) 推荐(0) 编辑
摘要: 1 #include <iostream> 2 #include <string.h> 3 //#include <fstream> 4 5 using namespace std; 6 7 static int ans=0; 8 9 void swap(int& a,int& b) 10 { 11 int t; 12 t=a; 13 a=b; 14 b=t; 15 } 16 17 void swap(char*a ,char*b ) 18 { 19 char s[55]; 20 strcpy(s,a); 21 ... 阅读全文
posted @ 2013-04-11 17:55 码代码的猿猿 阅读(308) 评论(0) 推荐(0) 编辑
摘要: next_permutationprev_permutation在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组数据的全排列.但是怎么用,原理如何,我做了简单的剖析.首先查看stl中相关信息.函数原型:template<class BidirectionalIterator> bool next_permutation( BidirectionalIterator_First, BidirectionalIterator_Last );template<class BidirectionalIterator, class Bina 阅读全文
posted @ 2013-04-11 17:54 码代码的猿猿 阅读(251) 评论(0) 推荐(0) 编辑
摘要: 算法是计算机科学领域最重要的基石之一,但却受到了国内一些程序员的冷落。许多学生看到一些公司在招聘时要求的编程语言五花八门,就产生了一种误解,认为学计算机就是学各种编程语言,或者认为,学习最新的语言、技术、标准就是最好的铺路方法。其实,大家被这些公司误导了。编程语言虽然该学,但是学习计算机算法和理论更重要,因为计算机语言和开发平台日新月异,但万变不离其宗的是那些算法和理论,例如数据结构、算法、编译原理、计算机体系结构、关系型数据库原理等等。在“开复学生网”上,有位同学生动地把这些基础课程比拟为“内功”,把新的语言、技术、标准比拟为“外功”。整天赶时髦的人最后只懂得招式,没有功力,是不可能成为高手 阅读全文
posted @ 2013-04-11 17:51 码代码的猿猿 阅读(165) 评论(0) 推荐(0) 编辑
摘要: 若某数x分别被d1、、…、dn除得的余数为r1、r2、…、rn,则可表示为下式:x=R1r1+R2r2+…+Rnrn+RD其中R1是d2、d3、…、dn的公倍数,而且被d1除,余数为1;R1 、R2…、Rn是d1、d2、…、dn-1的公倍数,而且被dn除,余数为1;D是d1、d2、…、的最小公倍数;R是任意整数,可根据实际需要决定;且d1、、…、必须互质,以保证每个Ri(i=1,2,…,n)都能求得.该问题可以用初等数论中的同余方程组的求解问题。利用同余的符号,可以将上述问题转化为下面的同余方程组:x≡2(mod 3);x≡3(mod 5);x≡2(mod 7);不难看出上述同余方程组的解并不 阅读全文
posted @ 2013-04-11 17:45 码代码的猿猿 阅读(471) 评论(0) 推荐(0) 编辑
摘要: 1 //最终版 2 #include <iostream> 3 #include <cmath> 4 using namespace std; 5 double ans2(char* c) 6 { 7 double a,b; 8 double sum; 9 char t; 10 a=c[2]-48;b=c[4]-48; 11 t=c[3]; 12 // cout<<"a: "<<a<<"b: "<<b<<"t: "<<t<<en 阅读全文
posted @ 2013-04-11 17:39 码代码的猿猿 阅读(1112) 评论(0) 推荐(2) 编辑
摘要: 1 int** func(int w,int h) 2 { 3 int** p=new int *[w]; 4 for(int i=0;i<w;i++) 5 { 6 p[i]=new int[h]; 7 } 8 return p; 9 }10 11 //记得要DELETE 阅读全文
posted @ 2013-04-11 17:38 码代码的猿猿 阅读(192) 评论(0) 推荐(0) 编辑
摘要: 1 #include<stdio.h>2 #define N 6003 int main(void)4 {5 int a[N][N];6 printf("\nthe array is %d\n",N);7 return 0;8 }当N不是太大时还可以,一旦N大于600,就会出现警告,甚至错误,错误如下:Compiling...test.cD:\test.c(5):warningC4101:'a':unreferencedlocalvariableLinking...错误大概就是说,可能是数组建的太大了???开到main函数外面,开在里面会因为数组 阅读全文
posted @ 2013-04-11 17:36 码代码的猿猿 阅读(427) 评论(0) 推荐(0) 编辑
摘要: 找规律的题,对每一个数字对应一个三维坐标(X,Y,Z)X::表示在第几层Y:从左边的边数在第几个Z::从右边的边数在第几个对倒三角要特殊处理(Z减一)如:1(1,1,1) 2(2,2,1) 3(2,1,1)距离就是坐标差点绝对值。 1 #include <iostream> 2 #include <cmath> 3 4 using namespace std; 5 6 struct Note 7 { 8 int x; 9 int y;10 int z;11 };12 13 int main()14 {15 Note a,b;16 int m,n;... 阅读全文
posted @ 2013-04-11 12:07 码代码的猿猿 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int cmp(const void *a,const void *b) 7 { 8 return *(int*)a-*(int*)b; 9 }10 11 int cn(int x)12 {13 int c=0;14 for(;x;x=x>>1)15 {16 if(x&1)17 c++;18 }19 return c;20 }21 22 int a[110];23 int b... 阅读全文
posted @ 2013-04-11 12:03 码代码的猿猿 阅读(161) 评论(0) 推荐(0) 编辑
摘要: MatrixTime Limit:2000MSMemory Limit:30000KTotal Submissions:3096Accepted:1612DescriptionGiven an n*n matrix A, whose entries Ai,j are integer numbers ( 0 <= i < n, 0 <= j < n ). An operation SHIFT at row i ( 0 <= i < n ) will move the integers in the row one position ri... 阅读全文
posted @ 2013-04-11 12:02 码代码的猿猿 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 教课书一样的代码,一遍AC:Fire NetTime Limit : 2000/1000ms (Java/Other)Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 15Accepted Submission(s) : 6Problem DescriptionSuppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each represen 阅读全文
posted @ 2013-04-11 11:59 码代码的猿猿 阅读(336) 评论(1) 推荐(0) 编辑
摘要: 非常经典的搜索题,很多剪枝技巧在里面!! 4 #include <iostream> 5 #include <algorithm> 6 #include <string.h> 7 8 using namespace std; 9 10 int cmp(const void *a ,const void *b)11 {12 return *(int*)b-*(int*)a;13 }14 15 int a[70];16 int n;17 int vis[70];18 int sum;19 20 int ko(int unused,int left, int ta 阅读全文
posted @ 2013-04-11 11:57 码代码的猿猿 阅读(191) 评论(0) 推荐(0) 编辑
摘要: 1 #include <iostream> 2 3 using namespace std; 4 5 int n,k; 6 char m[9][9]; 7 int a[9]; 8 9 void dfs(int cur,int k,int& tot)10 {11 if(k<0) return;12 if(cur==n)13 {14 if(k==0)15 tot++;16 return ;17 }18 for(int i=0;i<n;i++)19 {20 if(m[cu... 阅读全文
posted @ 2013-04-11 11:54 码代码的猿猿 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 找规律的: 1 #include <iostream> 2 #include <cmath> 3 4 using namespace std; 5 6 int main() 7 { 8 int a,b; 9 while(cin>>a>>b)10 {11 b=b%4;//最多就4种情况12 a=a%10;13 switch(b)14 {15 case 1:16 cout<<a%10; break;17 case 2:18 cout<<a*a%10; break;19... 阅读全文
posted @ 2013-04-11 11:53 码代码的猿猿 阅读(184) 评论(0) 推荐(0) 编辑
摘要: Problem Description猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么。每猜一个数,计算机都会告诉玩家猜对几个数字,其中有几个数字在正确的位置上。比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。Input输入数据 阅读全文
posted @ 2013-04-11 11:51 码代码的猿猿 阅读(403) 评论(0) 推荐(0) 编辑
摘要: 最基本的DFSLake CountingTime Limit : 2000/1000ms (Java/Other)Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 10Accepted Submission(s) : 3Problem DescriptionDue to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 & 阅读全文
posted @ 2013-04-11 11:50 码代码的猿猿 阅读(297) 评论(0) 推荐(0) 编辑
摘要: Prime PathTime Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 7 Accepted Submission(s) : 3Problem DescriptionThe ministers of the cabinet were quit... 阅读全文
posted @ 2013-04-11 06:45 码代码的猿猿 阅读(165) 评论(0) 推荐(0) 编辑