2011年5月6日

ACM PKU 1008 http://acm.pku.cn/JudgeOnline/problem?id=1008

摘要: 解法 一:#include <iostream>#include <stdio.h> #include <string.h> using namespace std;char hMonthName[19][8]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac& 阅读全文

posted @ 2011-05-06 19:57 _Clarence 阅读(138) 评论(0) 推荐(0)

ACM PKU 2328 http://acm.pku.cn/JudgeOnline/problem?id=2328

摘要: #include<iostream>#include<string>using namespace std;int main(){char abc[20];int n , max[100] , min[100] , i = 0 , j = 0 , k, cnt = 0 ,number = 0;while(cin>>n){ number++; if(number == 1 && n == 0) return 0; getchar(); gets(abc); if( !strcmp("too high",abc) ) max[ 阅读全文

posted @ 2011-05-06 19:56 _Clarence 阅读(132) 评论(0) 推荐(0)

ACM PKU 1009 http://acm.pku.cn/JudgeOnline/problem?id=1009

摘要: #include <stdio.h>#include <stdlib.h>int stick[64];int used[64];int n, m, len;void init ( int n ){ for ( int i=1; i<n; i++ ) { used[i] = 0; } used[0] = 1;}int cmp ( const void *a, const void *b ){ return *( int * )b - *( int * )a;}int dfs ( int start, int no, int cur, int last ){ int 阅读全文

posted @ 2011-05-06 19:55 _Clarence 阅读(175) 评论(0) 推荐(0)

ACM2304 http://acm.pku.cn/JudgeOnline/problem?id=2304

摘要: #include<iostream>using namespace std;int main(){int a , b , c , d , sum;while(cin>>a>>b>>c>>d ){ sum = 0; if(!a && !b && !c && !d) return 0; sum += 1080; if( a > b) sum += (a - b) * 9; else sum += 360 - (b - a) * 9 ; if(b < c ) sum += (c - 阅读全文

posted @ 2011-05-06 19:54 _Clarence 阅读(127) 评论(0) 推荐(0)

素数测试+大数的因素分解 的概率算法

摘要: 素数在密码届被广泛使用。如何判断一个素是素数,迄今为止最有效的方法是筛数法,做个表(打表法)即素数的倍数一定是合数。筛选法的效率很高,但是遇到大素数就无能为力了。米勒罗宾算法是一个相当著名的判断是否是素数的算法,能够很大概率的判断一个数是否是素数(接近100%,在一定范围内能达到100%)。在工程方面被广泛应用。米勒罗宾算法的核心是课上提到的费马小定理:即如果一个数n 它的 a^(n-1)%n !=1 (0<a<n) 则它一定是合数。如果 a^(n-1)%n ==1 (0<a<n) 则它可能是合数可能是素数。概率算法的概率就在这个 a上体现。/*1 随机取一个 a2 如 阅读全文

posted @ 2011-05-06 19:53 _Clarence 阅读(856) 评论(1) 推荐(0)

ACM 2260 http://acm.pku.cn/JudgeOnline/problem?id=2260

摘要: #include <iostream>using namespace std;int main (){ int n;bool a[100][100];for(int i=0;i<100;i++){ cin >>n; if (n==0) break; int o=0,q=0,p=0,x=0,y=0; for (int j=0;j<n;j++) for (int k=0;k<n;k++) { cin >>a[j][k]; } for (int l=0;l<n;l++) { int sum1=0; for (int m=0;m<n;m 阅读全文

posted @ 2011-05-06 19:53 _Clarence 阅读(97) 评论(0) 推荐(0)

ACM 1050 To the Max http://acm.pku.cn/JudgeOnline/problem?id=1050

摘要: 这里我重新整理了一下最大子矩阵的算法;基本思路是将二维数组转化为一维数组,多次调用最大子段和函数,通过比较获得最大值;我画一个图吧;每次将二维数组的每列和转为一维数组;调用一维数组最大最大子段和问题;用temp存储最大值就欧了!!!总之一句话:会最大字段和这个问题就很容易看懂!我也不多写了,代码贴一下:#include <iostream>using namespace std;int array[105][105],n,s[105];int maxSum(int n,int *a){int sum=a[0],b=0;for(int i=0;i!=n;++i){ if (b> 阅读全文

posted @ 2011-05-06 19:52 _Clarence 阅读(165) 评论(0) 推荐(0)

ACM1503 http://acm.pku.cn/JudgeOnline/problem?id=1503

摘要: #include <iostream>using namespace std;int main (){char a[200][200]={'0'};int b[200]={0};int c[200]={0};int mm=0;int j=0;int i=0;for (;;i++){cin>>a[i];b[i]=strlen(a[i])-1;if (b[i]>mm) mm=b[i]; if (a[i][0]=='0'&&b[i]==0)break;}int carry =0;for (int p=mm;p>=0; 阅读全文

posted @ 2011-05-06 19:51 _Clarence 阅读(98) 评论(0) 推荐(0)

C++/MFC 试题 (2)

摘要: 三.简答题(64分)1.请简述C、C++、VC、MFC在概念上的区别(4分)2.请写一个函数重载的简单例子(4分)3.用什么函数开启新进程、线程。(4分)4.SendMessage和PostMessage有什么区别(4分)5.WaitForSingleObject有何作用;m_pThrd的类型是CWinThread*时,WaitForSingleObject(m_pThrd->m_hThread, INFINITE);有何作用。(4分)6. __stdcall、__cdecl、__pascal在什么方面有所不同。(4分)7.请把下述代码加上异常处理。(6分)int MyWriteFile 阅读全文

posted @ 2011-05-06 19:50 _Clarence 阅读(238) 评论(0) 推荐(0)

C++/MFC 试题 (1)

摘要: 一.填空题(26分)1.WIN32平台下,sizeof(short) = __2__,sizeof(int) = __4__,sizeof(long) = __4__。(3分)2.请给出如下程序的结果(2分)int a = 3;int b = a << 3;a = __3__,b = __24__。3.请给出如下程序的结果(2分)int aaa = 0x01;htonl(aaa) = _16777216___。//这题运行显示的是16777216,我觉得可能是随机值4.请给出如下程序的结果(2分)#define MAX_NUM 100+200int nTemp = MAX_NUM* 阅读全文

posted @ 2011-05-06 19:50 _Clarence 阅读(391) 评论(0) 推荐(0)

ACM 1740 A New Stone Game http://acm.pku.cn/JudgeOnline/problem?id=1740

摘要: 题目大意:有N堆石头,每堆石头数目在1到100之间,最多有10堆.两人分别取走石头.取石头的规则是:每次只能从1堆中取,每次取走至少1个.取过后还可以把这堆的石头任意分配到其它堆上(这些堆必须有石头,废话呵呵),当然也可以不分配.问给定这些石头堆的情况,两人轮流取,谁先取完谁胜利,问是先取的胜利还是后取的胜利.双方最优策略.我就直接说结论了:首先讨论石头堆两堆两堆相等的情况,例如x,x,y,y,z,z.6堆的情况.在这种情况下先取的必输,很简单,先取的那人怎么取后取的那人就怎么取(如果对方把石头分配到一堆上,你就分配到与之对应的堆上),总之保持这个相等的均势不变,这样到最后,后取的人就将取走最 阅读全文

posted @ 2011-05-06 19:49 _Clarence 阅读(157) 评论(0) 推荐(1)

ACM.2234 Matches Game http://acm.pku.cn/JudgeOnline/problem?id=2234

摘要: #include <iostream>using namespace std;int main (){ int n=0; while (cin>>n) { int m=0,l=0; for (int i=0;i<n;i++) { cin >>l; m^=l; } cout <<(m?"Yes":"No")<<endl; } return 0;} 阅读全文

posted @ 2011-05-06 19:47 _Clarence 阅读(146) 评论(0) 推荐(0)

无穷下降法·数的按位异或与随机博弈(1)

摘要: 无穷下降法是著名的业余数学家费马提出的,也正是因为这个方法他才敢在书页边上声称自己已经找到了一个巧妙的方法证明费马猜想(x^n+y^n=z^n在n>2时没有正整数解,x^y表示x的y次方)。意外的是这个方法并没有想象中的那样神奇,它仅仅可以证明对于某些特殊的n才能成立(譬如说4),思路是:假设存在一组解(x0,y0,z0),那么由这一组解可以得到一组新的解(x1,y1,z1),并且满足z0>z1,这个过程可以重复进行,然而这是不可能的,这样必然使得zi“下降”到0,所以原方程没有正整数解。这个方法应该和数学归纳法齐名的,然而它太类似反证法了:上述证明可以写成假设一组解(x0,y0, 阅读全文

posted @ 2011-05-06 19:47 _Clarence 阅读(884) 评论(0) 推荐(0)

ACM 2348 Euclid's Game http://acm.pku.cn/JudgeOnline/problem?id=2348

摘要: 方法:若整除先者胜,若不然,GCD,看每次的商是否为1,若为1,i++,继续,判断他的奇偶性;证明,若整除,显然,不然,若遇到m/n>1时,其中1、2的时候包含所有胜负,则做该判断的时候的人必胜我最后更新的代码如下:#include <iostream>using namespace std;int main (){int n=0,m=0;while (cin>>m>>n){ if(m==0&&n==0) break; int i=0; int temp=0; if (m<n) { temp=m; m=n; n=temp; } i 阅读全文

posted @ 2011-05-06 19:46 _Clarence 阅读(153) 评论(0) 推荐(0)

ACM 2083 Game of Connections http://acm.pku.cn/JudgeOnline/problem?id=2084

摘要: 很郁闷的一道题,看通过率本以为很简单,其实就是个数学题,求Catalan数,关于Catalan数,现在做下说明:令h(1)=1,catalan数满足递归式:h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2)该递推关系的解为:h(n+1)=c(2n-2,n-1)/n (n=1,2,3,...)我并不关心其解是怎么求出来的,我只想知道怎么用catalan数分析问题。我总结了一下,最典型的三类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了)1.括号化问题。矩阵链乘: P=a1×a2&# 阅读全文

posted @ 2011-05-06 19:44 _Clarence 阅读(278) 评论(0) 推荐(0)

Bode Plot pku ACM http://acm.pku.cn/JudgeOnline/problem?id=1045

摘要: 今天做了一件很缺德的事,用别人给出的公式做了这个1045,一道纯数学公式推导居然还看了DISCUSS,真的不能原谅自己:公式推导:V2=iR=CR d/dt(Vs*cos(wt)-Vr*cos(wt+b))=Vrcos(wt+b) CRw(sin(wt+b)-sin(wt))=Vr*cos(wt+b) 令t=0化简得: tan(b)=1/(CRw)-------------(1) 令wt+b=0化简得:Vr=CRwVs*sin(b)------------(2) 由(1),(2)得: Vr=CRwVs/sqrt(1+(CRw)^2))#include <iostream>#incl 阅读全文

posted @ 2011-05-06 19:43 _Clarence 阅读(212) 评论(0) 推荐(0)

Deck ACM pku http://acm.pku.cn/JudgeOnline/problem?id=1607

摘要: 哈哈,这道题我玩得最阴毒,你出题毒我会更毒,让你知道什么叫道高一尺,魔高一丈!不就是要我控制格式嘛,我连你的空格我都给复制下来,看你还敢不敢说我Presentation Error!其题目大意就是要我们计算超出边缘的总长度,没有难度,主要注意小数点位数和它要求的格式就欧了!#include <iostream>#include <iomanip>using namespace std;int main (){int n;cout<<"Cards Overhang"<<endl;while (scanf("%d&quo 阅读全文

posted @ 2011-05-06 19:42 _Clarence 阅读(181) 评论(0) 推荐(0)

Yeehaa! ACM pku http://acm.pku.cn/JudgeOnline/problem?id=1799

摘要: 面队1799这样的题只有一个字可以描述它:水!我的代码如下:#include <iostream>#include <cmath>using namespace std;const double p=3.1415926535897932384626;int main (){ int m;cin >>m;for (int i=1;i<=m;i++){ double R=0; int n=0; cout <<"Scenario #"<<i<<":"<<endl; cin 阅读全文

posted @ 2011-05-06 19:41 _Clarence 阅读(204) 评论(0) 推荐(0)

Intersecting Lines ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=1269

摘要: 自己的问题自己解决了,凡事还得靠自己啊!#include <iostream>using namespace std;int main (){int n;cin >>n;double x1,x2,x3,x4,y1,y2,y3,y4;double a[10][8];for (int i=0;i<n;i++) for (int k=0;k<8;k++){ cin>>a[i][k];}cout<<"INTERSECTING LINES OUTPUT"<<endl;for (int m=0;m<n;m++ 阅读全文

posted @ 2011-05-06 19:40 _Clarence 阅读(107) 评论(0) 推荐(0)

Hansel and Grethel ACM pku http://acm.pku.cn/JudgeOnline/problem?id=1254

摘要: 1254是一道非常简单的关于平面直线相交问题的计算,只要注意一点:斜率并不是所给出的指南针上的度数而应该是(90-degree);解决了此问题后所有的问题都迎刃而解了我的代码如下:#include <iostream>#include <cmath>using namespace std;const double PI=3.1415926535897932384626;int main (){int n;cin >>n;for (int i=1;i<=n;i++){ double x0,x1,y0,y1,degree0,degree1,X=0,Y=0; 阅读全文

posted @ 2011-05-06 19:40 _Clarence 阅读(141) 评论(0) 推荐(0)

Rotten Ropes PKU ACM http://acm.pku.cn/JudgeOnline/problem?id=2291

摘要: 基础题,没有什么好说的;我的代码如下:#include <iostream>#include <algorithm>using namespace std;int main (){int t;int a[1000]={0};cin >>t;for (int i=0;i<t;i++){ int n,sum=0,sun=0; cin >>n; int j; for (j=0;j<n;j++) { cin >>a[j]; } sort (a,a+n); for (j;j>=0;j--) { sum=a[n-j]*j; if 阅读全文

posted @ 2011-05-06 19:39 _Clarence 阅读(119) 评论(0) 推荐(0)

The Circumference of the Circle ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=2242

摘要: 公式推导过程如下:设任意一个三角形的三边为a, b, c, 边a所对的角为A. 所求外接圆半径为R根据任意三角形面积公式, 得到 bcsinA/2 = S再根据正弦定理, 得到 2R= a/sin A;两式一除, 得到 (bc/2) / (2R) = S/a整理, 就得到 R = abc/(4S)代码如下:#include<stdio.h>#include<math.h>#define pi 3.141592653589793int main(){ double a,b,c;double x1,x2,x3,y1,y2,y3;double sinA,cosA,r;whil 阅读全文

posted @ 2011-05-06 19:38 _Clarence 阅读(121) 评论(0) 推荐(0)

Parliament ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=1032

摘要: 我们首先先撇开“每组的人数不同”这一约束条件,我们发现如10可以拆成5+5,它的乘积最大,而5又可拆成2+3;2*3>5;故我们发现每次的每一个数都拆成两个相等或相差1的两个数乘积最大。而最大的当然是一直拆到出现2;我们在回来考虑“每组的人数不同”这一约束条件;每组的人数不能相等,但我们又要使数拆成最小的部分(最好拆到出现2),比如说2.3.4~~,那我们大可从2加起,2+3+4+~~~一直加到接近所要求的数;如26我们可以用个循环得到2+3+4+5+6,然后让它停下,此时还剩余6(26-2-3-4-5-6)没用完;那我们就从加数序列的最大那个数开始倒序一次加一,这样过后加数序列就变成了 阅读全文

posted @ 2011-05-06 19:37 _Clarence 阅读(122) 评论(0) 推荐(0)

寻找和最大的最长子串

摘要: 问题:给你一个整型数组,其中的元素有正有负也有0,要求你找到一个连续的子串,子串的所有元素之和是所有的连续子串中最大的,而且是非负的。(应该讲清楚了吧?^_^)解决方法:最简单而又最高效的方法应该就是《编程珠玑》上介绍的扫描法了吧。其基本思想是假设以【0~(i-1)】为下标区间的子串中的最大子向量的元素之和,以及以下标 i-1 为截止元素的最大子向量之和已经求出,则当扫描到下标为 i 的元素时,这两个最大和应该做如何改变?具体操作可以这样:通过一个变量保存遍历到当前位置为止所找到的最大连续子串的元素之和,假设为max通过另一个变量temp用来保存以当前位置上的元素作为最后一个元素的连续子串的元 阅读全文

posted @ 2011-05-06 19:36 _Clarence 阅读(402) 评论(0) 推荐(0)

Biorhythms ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=1006

摘要: 本题可以参考Wikipedia上线性同余同方程的解法,方法很简单手算一会就出来了。也可以参考中国同余定理;就这么多,下面我展示我的代码:#include <iostream>using namespace std;int main (){int p,e,i,d;for (int n=1;;n++){ cin >>p>>e>>i>>d; int sum,sum1,sum2,sum3; if (p==-1&&e==-1&&i==-1&&d==-1) break; sum1=p; sum2=e 阅读全文

posted @ 2011-05-06 19:36 _Clarence 阅读(114) 评论(0) 推荐(0)

Blocks ACM PKU http://acm.pku.cn/JudgeOnline/problem?id=2363

摘要: 本以为这道题有多难,开始别人说它恶心我还不信,现在我把它拿下了;原来是把它想得太复杂了,既然是水题我也没有什么好说的了,只要注意一下当输入0时应该输出6就行了,下面展示这个恶心题的我的水代码:#include <iostream>using namespace std;int main (){int n;cin>>n;for (int q=0;q<n;q++){ int m=0,sum=999999; cin >>m; if (m==0) { sum=m; continue; }for (int i=1;i<n;i++){ for (int j= 阅读全文

posted @ 2011-05-06 19:34 _Clarence 阅读(183) 评论(0) 推荐(0)

pku acm 1046 Color Me Less http://acm.pku.edu.cn/JudgeOnline/problem?id=1046

摘要: #include <iostream>#include <cmath>using namespace std;int main (){double aa[16][3];for (int i=0;i<16;i++){ cin>>aa[i][0]>>aa[i][1]>>aa[i][2];} double a,b,c;while ((cin >>a>>b>>c)&&(a!=-1&&b!=-1&&c!=-1)){ int sub=0; double Dm 阅读全文

posted @ 2011-05-06 19:33 _Clarence 阅读(142) 评论(0) 推荐(0)

ACM PKU Temple of Dune 2335 http://acm.pku.cn/JudgeOnline/problem?id=2335

摘要: #include <iostream>#include <cmath>using namespace std;const double pi=3.1415926535897932384626;double angle (double x0,double y0,double x1,double y1,double x2,double y2){double t1,X1,X2,Y1,Y2,temp;X1=x2-x0;Y1=y2-y0;X2=x1-x0;Y2=y1-y0;temp=(X1*X2+Y1*Y2)/(sqrt(X1*X1+Y1*Y1)*sqrt(X2*X2+Y2*Y2 阅读全文

posted @ 2011-05-06 19:32 _Clarence 阅读(156) 评论(0) 推荐(0)

ACM PKU 取石子游戏 http://acm.pku.edu.cn/JudgeOnline/problem?id=1067

摘要: P=黄金分割率[自然数列/P]就可以了.划分的概念很简单,假如A,B为无理数,而且1/A+1/B=1;那么[A*自然数列]和[B*自然数列]是两个数列,这两个数列合成了自然数列而且没有重复.#include <iostream>#include <cmath>using namespace std;int main (){ int n,m; int num,temp ; while ( scanf ( "%d%d", &n, &m ) != EOF ) { if ( n > m ) { temp = n; n = m; m = 阅读全文

posted @ 2011-05-06 19:31 _Clarence 阅读(115) 评论(0) 推荐(0)

Bash Game

摘要: (一)巴什博弈(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取光者得胜。很容易想到当n%(m+1)=0时,先取必胜,第一次先拿走n%(m+1),以后每个回合到保持两人拿走的物品总和为m+1即可。这个游戏还可以有一种变相的玩法:两个人轮流报数,每次至少报一个,最多报十个,谁能报到100者胜。 阅读全文

posted @ 2011-05-06 19:29 _Clarence 阅读(168) 评论(0) 推荐(0)

Wythoff Game

摘要: 威佐夫博弈(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜。如果甲面对(0,0),那么甲已经输了,这种局势我们称为奇异局势。前几个奇异局势是:(0,0)、(1,2)、(3,5)、(4,7)、(6,10).可以看出,a0=b0=0,ak是未在前面出现过的最小自然数,而 bk=ak+k. 那么任给一个局势(a,b),怎样判断它是不是奇异局势呢?我们有如下公式: ak =[k(1+√5)/2],bk= ak + k (k=0,1,2,...,n 方括号表示取整函数)奇妙的是其中出现了黄金分割数(1+√5)/ 阅读全文

posted @ 2011-05-06 19:27 _Clarence 阅读(109) 评论(0) 推荐(0)

ACM PKU 3318 Matrix Multiplication http://acm.pku.edu.cn/JudgeOnline/problem?id=3318

摘要: #include <iostream>using namespace std;int main (){ int n;int a[500][500],b[500][500],c[500][500];cin>>n;for (int j1=0;j1<n;j1++) for (int k1=0;k1<n;k1++) scanf("%d",&a[j1][k1]);for (int j2=0;j2<n;j2++) for (int k2=0;k2<n;k2++) scanf("%d",&b[j2][k2 阅读全文

posted @ 2011-05-06 19:26 _Clarence 阅读(150) 评论(0) 推荐(0)

Nimm Game

摘要: 尼姆博弈(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜。对于任何奇异局势(a,b,c),都有a^b^c=0.非奇异局势(a,b,c)(a<b<c)转换为奇异局势,只需将c变为a^b,即从c中减去 c-(a^b)即可。 阅读全文

posted @ 2011-05-06 19:26 _Clarence 阅读(133) 评论(0) 推荐(0)

PKU ACM Subsequence http://acm.pku.edu.cn/JudgeOnline/problem?id=3302

摘要: 这道题郁闷了我这么久,这么就这样过了呢?是在费解感觉以前做的没有什么不妥啊!现在加的条件好像也是可有可无啊!是在想不通,还是展示一下我的水代码吧!#include <iostream>using namespace std;bool yn(char a[],int n,char b[],int m){ int si=0,pi=0; if (n>m) return false; while (pi<m&&si<n) { if (a[si]==b[pi]) { si++; pi++; } else pi++; } if(si==n) return tr 阅读全文

posted @ 2011-05-06 19:23 _Clarence 阅读(150) 评论(0) 推荐(0)

PKU ACM 1604 Just the Facts http://acm.pku.edu.cn/JudgeOnline/problem?id=1604

摘要: 题目很水,代码也写得很面,没有什么好说的,基础题!#include <iostream>#include <iomanip>using namespace std;int main (){int aa,bb;while (scanf("%d",&bb)!=EOF){ aa=bb; long sum=0; long temp=1; while (aa!=1) { temp=aa*temp; if(temp%10==0) { temp=temp/10; } temp=temp%100; aa--; } while (temp%10==0) { t 阅读全文

posted @ 2011-05-06 19:22 _Clarence 阅读(143) 评论(0) 推荐(0)

ACM PKU 3318 Matrix Multiplication http://acm.pku.edu.cn/JudgeOnline/problem?id=3318

摘要: 矩阵乘法的计算这应该是一个比较节省时间的算法了吧,用一个n维数组左乘每个等式两边。改进后时间从O(n^3)变成了O(3*n^2)再加上改成了用C语言的输入函数scanf(),大大减少了输入时间。我的代码:#include<iostream>using namespace std;const int Max=501;int A[Max][Max]={0},B[Max][Max]={0},C[Max][Max]={0},AA[Max]={0},BB[Max]={0},DD[Max]={0},CC[Max]={0};int main (){int n;scanf("%d&quo 阅读全文

posted @ 2011-05-06 19:22 _Clarence 阅读(174) 评论(0) 推荐(0)

pku acm 2140 Herd Sums http://acm.pku.edu.cn/JudgeOnline/problem?id=2140

摘要: 2140代码短小精悍:#include<stdio.h>int main(){int cnt=0,i; long s;scanf("%ld",&s);for(i=1;(i+1)*i/2<=s;i++) if((s-(i-1)*i/2)%i==0) cnt++;printf("%d\n",cnt); return 0;} 阅读全文

posted @ 2011-05-06 19:21 _Clarence 阅读(116) 评论(0) 推荐(0)

PKU ACM Babelfish http://acm.pku.edu.cn/JudgeOnline/problem?id=2503

摘要: 这道题是用map容器解决的,对于map容器我一点都不熟悉,在队友的帮助下通过了n遍的修改才被ac,虽然代码很短,但是要解决实际问题确实要注意很多问题,特别是细节问题往往让我们在某一点上卡壳;通过这道题感觉学到了不少,希望与我同级别的菜鸟选手去做一做。下面我展示我的long long time(惭愧惭愧啊!)代码,并对它作注:#include <iostream>#include <map>#include <string> //用字符串整体输入using namespace std;int main(){map<string,string> my 阅读全文

posted @ 2011-05-06 19:20 _Clarence 阅读(230) 评论(0) 推荐(0)

PKU ACM 2485 Highways http://acm.pku.edu.cn/JudgeOnline/problem?id=2485

摘要: 一次性ac,算法-----prim,书中的讲解不太详细而且有错误,希望有困难的acmer看看我的文章prim;我的代码虽然说不是很精湛但也能勉强ac,有兴趣的可以看一下:#include <iostream>using namespace std;const int Max=65536;int min_edge (int a[],int n){int min=Max;int index;for (int i=0;i<n;i++){ if (a[i]!=0&&a[i]<min){min =a[i]; index=i;}}return index;}int 阅读全文

posted @ 2011-05-06 19:19 _Clarence 阅读(137) 评论(0) 推荐(0)

prim 算法

摘要: 最小生成树是数据结构中图的一种重要应用,它的要求是从一个带权无向完全图中选择n-1条边并使这个图仍然连通(也即得到了一棵生成树),同时还要考虑使树的权最小。为了得到最小生成树,人们设计了很多算法,最著名的有prim算法和kruskal算法。教材中介绍了prim算法,但是讲得不够详细,理解起来比较困难,为了帮助大家更好的理解这一算法,本文对书中的内容作了进一步的细化,希望能对大家有所帮助。假设V是图中顶点的集合,E是图中边的集合,TE为最小生成树中的边的集合,则prim算法通过以下步骤可以得到最小生成树:1:初始化:U={u 0},TE={ }。此步骤设立一个只有结点u 0的结点集U和一个空的边 阅读全文

posted @ 2011-05-06 19:18 _Clarence 阅读(367) 评论(0) 推荐(0)

PKU ACM 2418 Hardwood Species http://acm.pku.edu.cn/JudgeOnline/problem?id=2418

摘要: 这道题过的贼诡异,我依旧用map容器做的,在自己的电脑上根本运行不了,但是交上去就ac了,太神奇了,map容器的主要特点就是代码少但是所需的运行时间长,如果能很好的掌握map类的所有成员函数而利用它,对于很多问题的解决都会事半功倍,很多函数不懂,还得继续努力啊#include <iostream>#include <map>#include <string>using namespace std;int main(){map<string,int> mymap; //创建map类对象string str;map<string,int> 阅读全文

posted @ 2011-05-06 19:17 _Clarence 阅读(156) 评论(0) 推荐(0)

PKU ACM 1131 Octal Fractions http://acm.pku.edu.cn/JudgeOnline/problem?id=1131

摘要: 此题不用到高精度!跟踪double型数在计算机中的存储就很容易得到解题的方法!代码很简单:#include <iostream>#include <cmath>using namespace std;int main (){char str[65535]; while (scanf("%s",&str)!=EOF){ double sum=0; int n=strlen(str); for (int i=2;i<n;i++) { int temp=str[i]-'0'; sum+=(double)temp/pow(8.0 阅读全文

posted @ 2011-05-06 19:16 _Clarence 阅读(177) 评论(0) 推荐(0)

PKU ACM 1258 Agri-Net http://acm.pku.edu.cn/JudgeOnline/problem?id=1258

摘要: 熟悉了prim算法后这样的题目在十分钟内真的可以解决!最小生成树,prim真的很有用!#include <iostream>using namespace std;const int Max = 1000000;int edge_min (int a[],int n){int min =Max;int index=0;for (int i=0;i<n;i++){ if (a[i]<min&&a[i]!=0) { min = a[i]; index = i; }}return index;}int main (){ int n; while (scanf( 阅读全文

posted @ 2011-05-06 19:15 _Clarence 阅读(149) 评论(0) 推荐(0)

ACM PKU Catch That Cow 3278 http://acm.pku.edu.cn/JudgeOnline/problem?id=3278

摘要: 宽搜这道题我花了两天的时间写代码,只是本人比较笨,每次都wa或是re了,这道题我总共wa了11次,虽然对高手来说都只认为是没有什么好说的题,但我感觉自己把题做出来真的能学到很多东西,像宽搜一样使我对队列的知识有了更深的理解,人家都是0ms,AC的,我用了219ms,技不如人自当努力学习啊,第一个宽搜题还是要展示一下代码的嘛,呵呵#include <iostream>#include <queue>#include <cmath>//注意到0 1这组数据不能过所以我要用到绝对值函数;using namespace std;int n,k;struct Node 阅读全文

posted @ 2011-05-06 19:14 _Clarence 阅读(163) 评论(0) 推荐(0)

汇编语言小型程序设计代码

摘要: 1 STACK SEGMENT STACK 2 DB 128 DUP(0) 3 STACK ENDS 4 DATA SEGMENT 5 N = 90 6 A DB 0 7 BUF DB N DUP(' '),13,10,'$' 8 TEMPBUF DB 3,0,3 DUP(0),13,10,'$' 9 SCORE DB 30 DUP(0) 10 HSCORE DB 2 DUP(' '),13,10,'$' 11 HSCORESTR DB 'High score: $' 12 TITLESTR DB 阅读全文

posted @ 2011-05-06 19:13 _Clarence 阅读(382) 评论(0) 推荐(0)

我的汇编程序设计代码

摘要: 1 STACK SEGMENT STACK 2 DB 1 DUP(?) 3 STACK ENDS 4 DATA SEGMENT 5 STR1 DB 'Please input the scores of N(0<n<=30) students!',10,13,'$' 6 STR2 DB 'The largest score is:',10,13,'$' 7 SCORE DB 91,0,91 DUP (?) 8 BCD DB 30,0,30 DUP (?) 9 DATA ENDS10 CODE SEGMENT11 ASS 阅读全文

posted @ 2011-05-06 19:11 _Clarence 阅读(110) 评论(0) 推荐(0)

ACM PKU 1298 The Hardest Problem Ever http://acm.pku.edu.cn/JudgeOnline/problem?id=1298

摘要: 人有的时候真的很可鄙,可鄙就变成了可耻,可耻就喜欢做水题,我今天也可耻了一把,1298没什么好说的,水题一道:#include <iostream>#include <string>using namespace std;char S[] = "START";char E[] = "END";char final[] ="ENDOFINPUT";int main (){while (1){ char sentence[256]; gets(sentence); if(strcmp(final,sentence) 阅读全文

posted @ 2011-05-06 19:09 _Clarence 阅读(143) 评论(0) 推荐(0)

ACM PUK 1088 滑雪 http://acm.pku.edu.cn/JudgeOnline/problem?id=1088

摘要: 人家都说这是一道动态规划题,而我认为这不过是一道广搜题,递归就完全可以解决,而且还不需要用队列,代码很简单也很容易懂,说太多没用,大家都知道,我的代码:#include <iostream>using namespace std;int slope[100][100];int changdu[100][100];int R , C ;int fuc (int i,int j){int max = 0 ;if ( changdu[i][j] > 0 ) return changdu[i][j];if ( i-1>=0){if ( slope[i-1][j] > sl 阅读全文

posted @ 2011-05-06 19:09 _Clarence 阅读(144) 评论(0) 推荐(0)

最大子串和----经典动态规划

摘要: 假如对于子段:9 2 -16 2temp[i]表示以ai结尾的子段中的最大子段和。在已知temp[i]的情况下,求temp [i+1]的方法是:如果temp[i]>0 temp [i+1]= temp[i]+ai(继续在前一个子段上加上ai),否则temp[i+1]=ai(不加上前面的子段),也就是说状态转移方程:temp[i] = (temp[i-1]>0?temp[i-1]:0)+buf[i];int getMax(int buf[100],int n){int temp[101],max=n*(-127);memset(temp,0,4*(n+1));for(int i=1; 阅读全文

posted @ 2011-05-06 19:08 _Clarence 阅读(172) 评论(0) 推荐(0)

ACM PKU 1159 Palindrome http://acm.pku.edu.cn/JudgeOnline/problem?id=1159

摘要: 原问题为求回文数需要插入的最少字母,转化为求最长公共子序列!经典的DP问题,算法有固定的模板,动态规划+滚动数组解决问题:#include <iostream>using namespace std;char str[5001];char un_str[5001];int len[2][5001];int main (){int n;cin >> n;int i = 1;int j = n;while (i <= n){ char temp; cin >> temp; str[i] = temp; un_str[j] = temp; i++; j--; 阅读全文

posted @ 2011-05-06 19:07 _Clarence 阅读(120) 评论(0) 推荐(0)

ACM PKU 1080 Human Gene Functions http://acm.pku.edu.cn/JudgeOnline/problem?id=1080

摘要: 做了几天的一道题,一步一步向前推进,现在看着都想吐了,什么解题报告我也不想写了,就这样吧!只有继续加油!!!#include <iostream>using namespace std;char in_st[101];char in_ed[101];int gene[5][5] = {5,-1,-2,-1,-3, -1,5,-3,-2,-4, -2,-3,5,-2,-2, -1,-2,-2,5,-1, -3,-4,-2,-1,0};int change( char c ){if( c == 'A' ) return 0;if( c == 'C' ) 阅读全文

posted @ 2011-05-06 19:06 _Clarence 阅读(125) 评论(0) 推荐(0)

ACM PKU 2533 Longest Ordered Subsequence http://acm.pku.edu.cn/JudgeOnline/problem?id=2533

摘要: 动态规划,最长递增子序列(不是非递减子序列);做法很简单,直接按照模板做就行了,主要是要想明白其中的道理,用什么存取加入第 i 个长度后当前的最长递增长度。两层循环,i 表示对第 i 个长度进行考察,j 表示对于 longer[i] 进行递增长度确定;写个状态转移方程就都明白了!状态转移方程:longer[i] = max{num[j]+1;其中1<j < i};不知道对不对,总之就是这个意思吧!见笑了!!!#include <iostream>using namespace std;int num[10001];int longer[10001];int longes 阅读全文

posted @ 2011-05-06 19:05 _Clarence 阅读(102) 评论(0) 推荐(0)

ACM PKU 1163The Triangle http://acm.pku.edu.cn/JudgeOnline/problem?id=1163

摘要: dp求最长序列和问题,题目比较简单,短小精悍!代码:#include <iostream>using namespace std;int flag[101];int num[101][101];int main (){ int N; int i , j ; cin >> N;memset(flag, 0, sizeof(flag));for (i = 1; i<=N ; i++) for (j = 1; j <= i; j++) scanf ("%d",&num[i][j]); for (i = 1; i<=N ; i++) 阅读全文

posted @ 2011-05-06 19:05 _Clarence 阅读(188) 评论(0) 推荐(0)

ACM PKU 1651 Multiplication Puzzle http://acm.pku.edu.cn/JudgeOnline/problem?id=1651

摘要: 这是一个很经典的DP问题--矩阵连乘问题;题目大意是给定n个数,移除每个数的代价是其与左右两数的乘积,求移除剩两个数的需要花费的最小代价。我对题目进行解读主要是帮助那些对矩阵连乘问题不太熟悉了兄弟们,希望通过本菜鸟的解说,能帮助你们加速理解核心代码意思!状态转换方程:sign[i][j] = min{sign[i][k]+sign[k][j]+num[i]*num[k]*num[j];其中i<k<j};下面我对我的code作注(加粗字为核心代码):#include <iostream>using namespace std;const int MAX = 101;int 阅读全文

posted @ 2011-05-06 19:04 _Clarence 阅读(151) 评论(0) 推荐(0)

ACM PKU 1160 Post Office http://acm.pku.edu.cn/JudgeOnline/problem?id=1160

摘要: 来之不易呀!!!用opt[i][j]记录把前i个邮局建到前j个村庄中的最优解用cost[i][j]记录所有在i到j村庄中,建1个邮局的最小代价。显然邮局应该设到中点。让前i个邮局覆盖前j个村庄,第i+1个邮局覆盖第j+1至j+k个村庄(j+k<=n),则状态转移方程为opt[i+1][j+k]=min{opt[i][j]+cost[j+1][j+k];} (k+j<=n)#include <iostream>using namespace std;const int MAX = 3000000;long cost[301][301];long opt[301][301] 阅读全文

posted @ 2011-05-06 19:03 _Clarence 阅读(156) 评论(0) 推荐(0)

ACM PKU 1579 Function Run Fun http://acm.pku.edu.cn/JudgeOnline/problem?id=1579

摘要: 连我就能十分钟以内搞定的代码还有什么好说的呢!提醒一下,用递归是多么愚蠢,用记忆搜索才是王道,还有注意格式,最好直接粘原题!!!#include <iostream>using namespace std;int max = 21;int sign[21][21][21];void init(){int i, j, k; sign[0][0][0] = 1;for (i = 0;i < 21; i++ )for (j = 0;j < 21; j++ ){sign[0][i][j] = 1;sign[i][0][j] = 1;sign[i][j][0] = 1;} for 阅读全文

posted @ 2011-05-06 19:02 _Clarence 阅读(166) 评论(0) 推荐(0)

ACM PKU Testing the CATCHER http://acm.pku.edu.cn/JudgeOnline/problem?id=1887

摘要: 这个是最令我无奈的DP了,算法什么难度都没有,关键是初始化和数组下标问题,下面代码中我标记一下,希望大家能够引起注意:#include <iostream>using namespace std;static int flag = 1;int num[32768];int longer[32768];int init(int n){int i , longest = 1;for (i = 0 ; i <=32767 ; i++) longer[i] = 1;for (i = 2;i <= n; i++) for (int j = 1; j < i; j++) { 阅读全文

posted @ 2011-05-06 19:01 _Clarence 阅读(153) 评论(0) 推荐(0)

ACM PKU Lake Counting http://acm.pku.edu.cn/JudgeOnline/problem?id=2386

摘要: 虽然是深搜,但是用递归不用队列一样过了,太庆幸了,真是傻人有傻福啊!高兴,呵呵....#include <iostream>#include <queue>using namespace std;int temp = 0;int M, N;char rtg[101][101];int flag[101][101];bool situation(int m , int n){if ((flag[m][n]==0)&&(m > 0) && (m <= M ) && (n > 0) && (n 阅读全文

posted @ 2011-05-06 19:00 _Clarence 阅读(133) 评论(0) 推荐(0)

ACM PKU Camelot http://poj.org/problem?id=1178

摘要: #include <iostream>//#include <queue>using namespace std;// queue <code> myqueue;int flag[64][8][8];int shore[64];char encode[64];int count = 0;bool charge (int a, int b){if (a >= 0 && b >= 0 && a < 8 && b < 8) return true ;else return false;}voi 阅读全文

posted @ 2011-05-06 18:59 _Clarence 阅读(159) 评论(0) 推荐(0)

ACM PKU 1019 Number Sequence http://acm.pku.edu.cn/JudgeOnline/problem?id=1019

摘要: #include <iostream>#include <cmath>using namespace std;int Len(int n) // 求数字的长度函数;{ return log10(double(n)) + 1;}int oj (int i, int temp) //一个比较傻的打表;不为别的,就选择最省时的;{if (temp < 10) return temp;if (temp < 100){ if (i == 1) return temp/10; else return temp%10;}if (temp < 1000){ if (i 阅读全文

posted @ 2011-05-06 18:55 _Clarence 阅读(97) 评论(0) 推荐(0)

ACM PKU 1014 Dividing http://acm.pku.edu.cn/JudgeOnline/problem?id=1014

摘要: 0-1背包 + 完全背包 + 滚动数组解题:将完全背包进行二进制转换是第一次尝试;0-1背包DP用得不熟悉,有待提高;滚动数组不适应,以后应加强这方面的训练;查找错误时,思维混乱,一个简单的初始化调了一个晚上都没找到,思维能力需要培养;这是一个有价值的DP题目,代码展示一下,虽然不是0S A的;#include <iostream>using namespace std;static int event = 1;int sign ;int num[7];int sum = 0;int equ_val = 0;int dp[2][100002];int store[20002];bo 阅读全文

posted @ 2011-05-06 18:54 _Clarence 阅读(180) 评论(0) 推荐(0)

0-1背包 和 完全背包

摘要: 0-1背包状态转移方程:f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}“将前i件物品放入容量为v的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为f[i-1][v];如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-c[i]的背包中”,此时能获得的最大价值就是f[i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。优化空间复杂度后得到:for i=1..N for v=V..0 f[ 阅读全文

posted @ 2011-05-06 18:54 _Clarence 阅读(172) 评论(0) 推荐(0)

ACM PKU 2531 Network Saboteur http://acm.pku.edu.cn/JudgeOnline/problem?id=2531

摘要: 先找出在一个集合中的所有点再根据标记来求最大值!做了这么久,也实属无奈!!!#include <iostream>using namespace std;int Case;int num[21][21];bool flag[21];int Max = 0;int solve(int dp,int st){ int i, j, k;if (dp==0){ int sum=0; for (j = 0;j < Case; j++) { if (flag[j] == 1) { for (k = 0;k < Case; k++) { if (flag[k] != 1) sum + 阅读全文

posted @ 2011-05-06 18:53 _Clarence 阅读(123) 评论(0) 推荐(0)

ACM PKU 2636 Sudoku http://acm.pku.edu.cn/JudgeOnline/problem?id=2676

摘要: 数独问题:搜索时,注意下标;#include <iostream>using namespace std;int num[11][11];int Case;void input(){ memset(num,0,sizeof(num)); int i,j; for (i = 0;i <9; i++) for (j = 0;j < 9; j++) { char a; cin >> a; num[i][j] = a - '0'; }}void output(){ int i,j; for (i = 0;i < 9; i++) { for (j 阅读全文

posted @ 2011-05-06 18:52 _Clarence 阅读(135) 评论(0) 推荐(0)

ACM PKU 3291 Cousins http://acm.pku.edu.cn/JudgeOnline/problem?id=3291

摘要: DP + 贪心 搞定 :#include <iostream>using namespace std;char in_st[101], in_ed[101];int len[101][101];int shortest(int n,int m){ int i , j; for (i = 0;i<=n;i++) len[i][0] = 0; for (i = 0;i<=m;i++) len[0][i] = 0; for(i = 1; i <=n; i++) { for (j = 1; j <=m ;j++) { if (in_st[i - 1] == in_e 阅读全文

posted @ 2011-05-06 18:49 _Clarence 阅读(136) 评论(0) 推荐(0)

ACM PKU 1164 The Castle http://acm.pku.edu.cn/JudgeOnline/problem?id=1164

摘要: 建图比较难!建议开结构体数组!#include <iostream>#include<queue>using namespace std;int val[4] = {8,4,2,1};bool flag[4];struct room{ int m; int c; bool e;bool w; bool s;bool n;// room(){}; /*room(int a ,int b, bool x,bool y,bool z,bool t) { m = a; c = b; e = x; w = y; s = z; n = t; }*/};room num[50][50 阅读全文

posted @ 2011-05-06 18:48 _Clarence 阅读(130) 评论(0) 推荐(0)

ACM PKU 1111 Image Perimeters http://acm.pku.edu.cn/JudgeOnline/problem?id=1111

摘要: 广搜永远记住,下标不要错,COPY时一定要记得改下标:#include <iostream>#include <queue>using namespace std;char rec[21][21];bool flag[21][21];int h[8] = {1,1,1,0,0,-1,-1,-1}; int s[8] = {1,-1,0,-1,1,0,-1,1};int row,column,st0,st1;int sum = 0;struct node{ int r; int c; node(){}; node(int x , int y) { r = x ; c = 阅读全文

posted @ 2011-05-06 18:48 _Clarence 阅读(181) 评论(0) 推荐(0)

ACM PKU 3176 Cow Bowling http://acm.pku.edu.cn/JudgeOnline/problem?id=3176

摘要: 他奶奶的,我也做水题(DP水题)了,有必要鄙视一下:#include <iostream>using namespace std;int num [351][351];int sign[351];int Max = 0;int N;void solve (){ int i, j; for(i = N-1; i > 0; i--) for (j = 1; j <= i; j++) { if (sign[j] + num[i][j] > sign[j+1] + num[i][j] ) sign[j] += num[i][j]; else sign[j] = sign[ 阅读全文

posted @ 2011-05-06 18:46 _Clarence 阅读(97) 评论(0) 推荐(0)

ACM PKU 1191 棋盘分割 http://acm.pku.edu.cn/JudgeOnline/problem?id=1191

摘要: 棋盘分割可以说是DP问题的经典,在刘汝佳老师的《算法艺术与信息学竞赛》上给出的一道例题,分析公式,要使最后的西格玛最小只要使每个方格的分数的平方和最小就行了,因为平均值使一个定值; 思路:1,先化简均方差公式,可以看出,只需要让每个分割后的矩形的总分的平方和尽量小,即可使均方差最小。2,考虑左上角坐标为(x1,y1),右下角坐标为(x2,y2)的棋盘,设它的总和为value[x1,y1,x2,y2]切割k次以后得到k+1块矩形的总分平方和最小值为store[k,x1,y1,x2,y2],则它可以沿着横线切,也可以沿着竖线切,然后选一块继续切(递归)。。3,由1,2部可以得到状态转移方程:st. 阅读全文

posted @ 2011-05-06 18:46 _Clarence 阅读(206) 评论(0) 推荐(0)

ACM PKU 1141 Brackets Sequence http://acm.pku.edu.cn/JudgeOnline/problem?id=1141

摘要: 这家伙磨了我几天了,解题报告稍后上:#include <iostream>#include <string>using namespace std;const int Max = 999999;int flag[202][202],sign[202][202];char str[202];inline int min (int a,int b){return a > b ? b : a ;}int Find_Match(int len){memset(flag,0,sizeof(flag));int i, j, k, m;for (i = len; i > 阅读全文

posted @ 2011-05-06 18:44 _Clarence 阅读(138) 评论(0) 推荐(0)

ACM PKU 1276 Cash Machine http://acm.pku.edu.cn/JudgeOnline/problem?id=1276

摘要: 大家都过了,我也来凑热闹了!最简单的01背包(也可以说是完全背包)!我将它01化了,很简单;代码在下面,有什么不懂的可以来问我!欢迎大家提问:#include <iostream>using namespace std;int flag[1100000];int sign[1100000];inline int max(int a, int b){ return a > b ? a : b ; }int Dp (int cash, int index){ int i, j; memset(sign,0,sizeof(sign)); for (i = 0; i <= in 阅读全文

posted @ 2011-05-06 18:44 _Clarence 阅读(141) 评论(0) 推荐(0)

ACM PKU 1837 Balance http://acm.pku.edu.cn/JudgeOnline/problem?id=1837

摘要: 大牛说这个题目很简单,我认为难-----因为我没有想清楚;网上的代码很多,但是基本上都是Copy 来 又Copy去的;总之;看的是一个版本还看不明白;我把解题报告好好写写!第一:我们最终的目的是找能使天平平衡的方法数;这个只能做背包做了,用01背包不难想,最终的结果就是sign【G】【Mid】数组里面记录的次数;第二;想清楚为什么不会出现在加入同一个砝码时不会造成计算重叠;只要把一个子问题抽出来看就明白了;假设出现重叠,那么它的上以状态就已经平衡了,(即到天平中心的距离相等)此处只是借助当前砝码来权衡能不能达到中间位置,此砝码在实际中不起作用;第三;DP的状态转移方程:sign[i][k + 阅读全文

posted @ 2011-05-06 18:42 _Clarence 阅读(200) 评论(0) 推荐(0)

ACM PKU 1745 Divisibility http://acm.pku.edu.cn/JudgeOnline/problem?id=1745

摘要: 比较简单的DP题,就用到一个滚动数组;思路:此题只要求一个余数为0的情况就行了,所以数组不用开那么大,每次我都对当前输入的数据取余,然后再标记一下就可以了;所以数组范围在100之内就够了,这里需要注意的是负数的处理(计算机不识别负数的取余情况);#include <iostream>using namespace std;int N, K, data;int flag[101],flag1[101];void DP_flag(int data, int K){int i;for (i = 0; i < K; i++){ if (flag1[i] == 1) { flag[(i 阅读全文

posted @ 2011-05-06 18:41 _Clarence 阅读(146) 评论(0) 推荐(0)

ACM PKU 3356 AGTC http://acm.pku.edu.cn/JudgeOnline/problem?id=3356

摘要: 这里有个问题,为什么LCS用滚动数组不好使呢?希望看到的牛人能指点一二!!!#include <iostream>using namespace std;char str1[1001],str2[1001];int len[1001][1001];int max(int a,int b){return a > b ? a : b; }int dp(int l1,int l2){int i ,j;memset(len,0,sizeof(len));for(i=0;i<=l1;i++) len[i][0]=0;for(i=0;i<=l2;i++) len[0][i]= 阅读全文

posted @ 2011-05-06 18:39 _Clarence 阅读(114) 评论(0) 推荐(0)

ACM PKU 2663 Tri Tiling http://acm.pku.edu.cn/JudgeOnline/problem?id=2663

摘要: 这题开始没有思路,后来看了流牛的解题报告才写出来;递推公式我写一下:sign[i] = 1 (当i == 0 时,具体为什么我也没有具体的说明方法) 0 (i % 2 != 0) 根据图最先想到的一定是这个; 3*sign[i-2] + 2*(sign[i-4] + sign[i-6]+......+sign[2]); (这个就是要将拿来的这个往里 面插有重复的情况会有一种抵消!)#include <iostream>using namespace std;int sign[31];void init(){int i ;int temp = 0;sign[0] = 1;sign[2 阅读全文

posted @ 2011-05-06 18:38 _Clarence 阅读(181) 评论(0) 推荐(0)

ACM PKU 1664 放苹果 http://acm.pku.edu.cn/JudgeOnline/problem?id=1664

摘要: 解题思路:每种情况都可以归结为:1、所有盘子中最少的放 0 个苹果,那么剩余的 n-1 个盘子就可以随便放了;2、如果盘子中最少苹果数为 1 ,那么就相当于将 m-n 个苹果放到 n 个盘子里的结论是一样的;#include <iostream>using namespace std;int solve ( int m ,int n){if (m < 0) return 0;if (m == 0 || n==1) return 1;return solve(m - n, n) + solve ( m, n-1);}int main (){int t, m, n;cin > 阅读全文

posted @ 2011-05-06 18:37 _Clarence 阅读(169) 评论(0) 推荐(0)

全排列函数

摘要: void perm (int A[], int n, int k){if (k == n) print( A , n );else for (int i = k;i < n; i++) { swap(A[i],A[k]); perm (A,n,k+1); swap(A[i],A[k]); }}//下面有验证代码#include <fstream>#include <iostream>using namespace std;ofstream outfile ("out.txt");int A[100];void print ( int A[] , 阅读全文

posted @ 2011-05-06 18:36 _Clarence 阅读(102) 评论(0) 推荐(0)

棋盘覆盖问题求解

摘要: #include <iostream>#include <iomanip>using namespace std;int state = 1 ;int Graph[100][100];void chip_gram(int str , int stc , int enr , int enc , int t){if (t == 1) return ; int flag = state++;t = t / 2;if (str < enr+t && stc < enc+t) chip_gram(str , stc , enr , enc , t);e 阅读全文

posted @ 2011-05-06 18:35 _Clarence 阅读(153) 评论(0) 推荐(0)

ACM PKU 1002 487-3279 http://acm.pku.edu.cn/JudgeOnline/problem?id=1002

摘要: #include <iostream>using namespace std;int sign[10000000];void init(int n){while (n--){ char str[1024]; cin >> str; int sum = 0; for (int i = 0 ; i < strlen(str); i++) { int temp = 0; switch (str[i]) { case 'A': case 'B': case 'C': case '2': temp = 2 ; 阅读全文

posted @ 2011-05-06 18:35 _Clarence 阅读(138) 评论(1) 推荐(0)

ACM PKU 2559 Largest Rectangle in a Histogram http://acm.pku.edu.cn/JudgeOnline/problem?id=2559

摘要: 关于覆盖,用DP是一种很不错的解决方法,时效也很不错....这题目把图一画就很明确了,没有说明的必要了。#include <iostream>using namespace std;__int64 height[100001];int main (){int n;int left[100001];int right[100001];__int64 max=0,temp=0;while (scanf("%d",&n) && n){ int i=0; memset(height,0,sizeof(height)); right[0]=0; l 阅读全文

posted @ 2011-05-06 18:34 _Clarence 阅读(104) 评论(0) 推荐(0)

ACM PKU 2082 Terrible Sets http://acm.pku.edu.cn/JudgeOnline/problem?id=2082

摘要: 这是一道关于最大覆盖的问题,对于这种问题,用一个双向DP足以解决问题,双向DP在以前的文章中已经有使用过,但是我这次挑战了用堆栈做的,思路跟DP一样的,只是操作简单了罢了。时效也应该比DP强。思维误区:我不知道每次大于栈顶的元素是否要判定它的值与最大面积MAX_area之间的关系,所以对它的判断我在压栈时就开始了,多余了.....#include <iostream>#include <stack>using namespace std;#define MAX_SIZE 50001struct node{int w,h;}rec[MAX_SIZE];int main() 阅读全文

posted @ 2011-05-06 18:32 _Clarence 阅读(161) 评论(0) 推荐(0)

ACM PKU 1147 Binary codes http://acm.pku.edu.cn/JudgeOnline/problem?id=1147

摘要: 研究了n久,原来是题意理解有问题.,,这句话很诡异Thenrows of the matrix are sorted in alphabetical order, where ‘0’ is before ‘1’'经过研究,发现题意原来是这样的 : 1.所有行都是经过某一行的rotated versions 2.即上面这句话的理解:将所有的rotated versions 排序.... 3.由1和2,所以行的顺序和生成rotated versions 的顺序相比,是混乱的....注意到有一点是确定的,同一行中最后一个数和第一个数的关系.方法: 因为是经过按照row排序的,所以第一列肯定是 阅读全文

posted @ 2011-05-06 18:29 _Clarence 阅读(140) 评论(0) 推荐(0)

魔方阵的奇术解法

摘要: 好久没有写算法了,由于一些原因,现在我又回来了,我又开始学算法写算法了。 前几天上机考试做了一道奇数魔方阵的编程题,现在整理一下贴出来。思想到处都是,据说谭浩强老师的教材上有介绍,就当练手了。//---------------------------------------------------------------------------#include <vcl.h>#include <iostream>#include <iomanip>#pragma hdrstop#define MAXSIZE 30//--------------------- 阅读全文

posted @ 2011-05-06 13:48 _Clarence 阅读(250) 评论(0) 推荐(0)

导航