摘要: 题目大意:给出一个数n,求n^n的数根,数根即各位数字之和,如果这个和不是一位数,继续求这个数的各位数字之和,直到为一位数为止。因为ab*ab=(10*a+b)*(10*a+b)=100*a*a+10*2*a*b+b*b=a*a+2*a*b+b*b=(a+b)*(a+b)abc*abc=(100*a+10*b+c)*(100*a+10*b+c) =10000*a*a+2000*a*b+100*b*... 阅读全文
posted @ 2010-04-25 20:53 北海小龙 阅读(263) 评论(0) 推荐(0)
摘要: //求最长公共子串//思路:使用string中的相应方法//教训:特别要注意临界条件#include <string>#include <iostream>#include <stdio.h>#include <algorithm>//引入algorithm类 using namespace std;bool cmp(string a,string ... 阅读全文
posted @ 2010-04-25 20:53 北海小龙 阅读(243) 评论(0) 推荐(0)
摘要: //求最大卡片数//注意精度问题,否则题目会出错#include <iostream>using namespace std;int main(){//使用double类型,如果使用float类型精度不够,会出错double data;cin>>data;while(data!=0.00 && data>=0.01 && data<... 阅读全文
posted @ 2010-04-25 20:52 北海小龙 阅读(181) 评论(0) 推荐(0)
摘要: //重点理解#include <stdio.h>int min(int a,int b,int c, int d);int i;int j;int k;int l;int main(){int data[5843];data[1]=1;i=j=k=l=1;for(int t=2;t<5843;t++){data[t]=min(2*data[i],3*data[j],5*data[... 阅读全文
posted @ 2010-04-25 20:52 北海小龙 阅读(356) 评论(0) 推荐(0)
摘要: //快速排序算法#include <iostream>using namespace std;int Partition(int a[],int p,int r);void QuickSort(int a[],int p,int r);int main(){int case_num;cin>>case_num;for(int i=0;i<case_num;i++){i... 阅读全文
posted @ 2010-04-25 20:51 北海小龙 阅读(191) 评论(0) 推荐(0)
摘要: //思路:输出N个整数,输出出现次数大于(N+1)/2的数//不要想的太复杂了#include <iostream>using namespace std;int main(){int num,i,n,x,a[32768];while(scanf("%d",&num)!=EOF){//将a进行初始化memset(a,0,sizeof(a));for(i=0;i<num;i... 阅读全文
posted @ 2010-04-25 20:50 北海小龙 阅读(205) 评论(0) 推荐(0)
摘要: //输出i和j中的最大循环长度//注意细节:i和j的大小不确定#include <stdio.h>int main(){int i,j;while(scanf("%d%d",&i,&j)!=EOF){bool isswap = false;if(i>j){ int temp=i; i=j; j=temp; isswap=true;}int maxtimes=0;... 阅读全文
posted @ 2010-04-25 20:50 北海小龙 阅读(314) 评论(0) 推荐(0)
摘要: //使用双链表list实现#include <list>#include <stdio.h>#include <iostream>using namespace std;int main(){int step,mod;char good[20]="Good Choice";char bad[20] = "Bad Choice";while(cin>>... 阅读全文
posted @ 2010-04-25 20:49 北海小龙 阅读(202) 评论(0) 推荐(0)
摘要: //水题:但有值得借鉴的地方#include <stdio.h>int fun(int n);int main(){double sum[10];sum[0]=1;printf("n e\n");printf("- -----------\n");printf("0 %d\n",1);for(int n=1;n<10;n++){sum[n]=sum[n-1]+1.0/fun(n)... 阅读全文
posted @ 2010-04-25 20:48 北海小龙 阅读(262) 评论(0) 推荐(0)
摘要: //阿牛的EOF牛肉串//思路:递推求解 p[i] = 2*(p[i-2]+p[i-2])#include <iostream>using namespace std;int main(){int n;while(cin>>n){if(n==1)cout<<3<<endl;else if(n==2)cout<<8<<endl;... 阅读全文
posted @ 2010-04-25 20:47 北海小龙 阅读(220) 评论(0) 推荐(0)
摘要: //思路:主要使用错排公式//d[1] = 0 d[2] = 1 d[n] = (n-1)*(d[n-1]+d[n-2]) #include <iostream>using namespace std;__int64 fun(int n);int main(){int case_num;cin>>case_num;for(int i=0;i<case_num;i++)... 阅读全文
posted @ 2010-04-25 20:47 北海小龙 阅读(141) 评论(0) 推荐(0)
摘要: //考新郎//思路:在错排公式的基础上加入排列组合的因素#include <iostream>using namespace std;__int64 fun(int n);int main(){int case_num;cin>>case_num;for(int i=0;i<case_num;i++){int n,m;while(cin>>n>>... 阅读全文
posted @ 2010-04-25 20:47 北海小龙 阅读(181) 评论(0) 推荐(0)
摘要: //LELE的RPG难题//思路:使用递推求解 p[i] = p[i-1] + 2*p[i-2]//其中p[i-1]表示i个中第i-1个格与第1个格的颜色不同,p[i-2]表示第i-1个格和//第1个格的颜色相同但是第i-2个格肯定与i-1个格颜色不同,种类数为p[i-2],又//因为第i-1个格和第1个格颜色相同时,第i个格可以有其余两种颜色,所以乘以2.//注:从第四个开始满足这种规律#inc... 阅读全文
posted @ 2010-04-25 20:46 北海小龙 阅读(420) 评论(0) 推荐(0)
摘要: //思路:f(n) = f(n-1)+f(n-2) 使用递推求解//从图中也可以观察出来,第N张牌的排列可以又N-1张牌的排列再在末尾加上一张竖的牌。这样依然合法。也可以在N-2张合法排列的牌后面加上两张横着放的牌(如果竖着放就和上面一种重复了)。#include <iostream>#include <stdio.h>using namespace std;int mai... 阅读全文
posted @ 2010-04-25 20:46 北海小龙 阅读(292) 评论(0) 推荐(0)
摘要: #include <iostream>using namespace std;int mul(int a,int b);int main(){int a,b;int result;while(cin>>a>>b){if(a==0 && b==0)return 0;cout<<mul(a%1000,b)<<endl;}ret... 阅读全文
posted @ 2010-04-25 20:45 北海小龙 阅读(334) 评论(0) 推荐(0)
摘要: //超级楼梯//思路:使用递推求解 p[n] = p[n-1] + p[n-2]#include <iostream>using namespace std;int main(){int case_number;cin>>case_number;for(int i=0;i<case_number;i++){int m;cin>>m;if(m==2)cout... 阅读全文
posted @ 2010-04-25 20:45 北海小龙 阅读(188) 评论(0) 推荐(0)
摘要: // 一只小蜜蜂//思路:递推求解 p[i] = p[i-1] + p[i-2]//详细解题报告见word文档#include <iostream>using namespace std;int main(){int case_num;cin>>case_num;for(int i=0;i<case_num;i++){int a,b;cin>>a>&... 阅读全文
posted @ 2010-04-25 20:45 北海小龙 阅读(246) 评论(0) 推荐(0)
摘要: //计算直线的交点数//m条直线的交点方案数//=(m-r)条平行线与r条直线交叉的交点数 + r条直线本身的交点方案//=(m-r)*r+r条之间本身的交点方案数(1<=r<=m)#include <iostream>#include <vector>using namespace std;int main(){//使用多维数组实现,用到vector<i... 阅读全文
posted @ 2010-04-25 20:44 北海小龙 阅读(341) 评论(0) 推荐(0)
摘要: //蟠桃记//思路://天数 吃掉个数 剩余个数 吃掉与剩余的关系//0 0 F(0)//1 F(0)/2+1 F(1) = F(0)/2-1//2 F(1)/2+1 F(1) = F(1)/2-1//..............................................//n-2 F(n-3)/2+1 F(n-2) = F(n-3)/2-1//n-1 F(n-2)/2+1 ... 阅读全文
posted @ 2010-04-25 20:44 北海小龙 阅读(164) 评论(0) 推荐(0)
摘要: //母牛的故事//思路://total[0]代表0岁母牛//total[1]代表1岁母牛//total[2]代表2岁母牛//total[3]代表成年母牛 #include <iostream>using namespace std;int main(){int n;while(cin>>n && (n!=0)){int total[4]={0}; total... 阅读全文
posted @ 2010-04-25 20:44 北海小龙 阅读(278) 评论(0) 推荐(0)
摘要: n个平面最多分空间为几个部分的问题首先,可以通过直观想象1-3个平面最多分空间为几个部分。1个平面最多将空间分为2部分; 2个平面最多将空间分为4部分; 3个平面最多将空间分为8部分。若要第四个平面将空间分为最多部分,就要它与前三个平面都相交,且交线不重合。则第四个平面与前三个平面都相交,交线不重合,有三条交线,这三条交线都在第四个平面内,那么要想使这四个平面分空间为最多部分就要使这三条交线分一个... 阅读全文
posted @ 2010-04-25 20:43 北海小龙 阅读(277) 评论(0) 推荐(0)
摘要: //错排问题//思路:错排公式:d[n]= (n-1)*( d[n-1] + d[n-2])#include <iostream>#include <stdio.h>using namespace std;int main(){int n;while(cin>>n){//注意使用__int64,否则会溢出__int64 a=0,b=1;__int64 resul... 阅读全文
posted @ 2010-04-25 20:43 北海小龙 阅读(199) 评论(0) 推荐(0)
摘要: //求最小公倍数#include <iostream>#include <math.h>using namespace std;int big(int a,int b);int main(){int a,b;while(cin>>a>>b){int big_data = big(a,b);//两个数互素if(big_data == 1)cout<... 阅读全文
posted @ 2010-04-25 20:42 北海小龙 阅读(215) 评论(0) 推荐(0)
摘要: #include <stdio.h>#include <string>#include <iostream>using namespace std;int main(){int case_num;cin>>case_num;for(int i=0;i<case_num;i++){//注:不能使用char类型,否则会读取开头的空白符,使用stri... 阅读全文
posted @ 2010-04-25 20:42 北海小龙 阅读(241) 评论(0) 推荐(0)
摘要: #include <iostream>using namespace std;double return_result(double x1,double x2,double x3,double y1,double a);int main(){int case_num;double x1,y1;double x2,y2;double x3,y3;double a;double resul... 阅读全文
posted @ 2010-04-25 20:41 北海小龙 阅读(322) 评论(0) 推荐(0)
摘要: #include <iostream>using namespace std;int main(){int line;cin>>line;int i=0;//c++中动态数组的分配int *sum_p = new int[line];int p =line;for(;p>0;p--){int sum=0;int number;cin>>number;for... 阅读全文
posted @ 2010-04-25 20:41 北海小龙 阅读(268) 评论(0) 推荐(0)
摘要: #include <iostream>#include <stdio.h>#include <ctype.h>#include <string>//注意c++中string库和c中的string.h库不同using namespace std;int main(){ while(1){string str;getline(cin,str);if(st... 阅读全文
posted @ 2010-04-25 20:40 北海小龙 阅读(221) 评论(0) 推荐(0)
摘要: //求N^N的个位数//思路:只计算个位数的乘积,个位数最后形成一个循环#include <iostream>#include <string>#include <vector>using namespace std;int main(){int num;cin>>num;for(int i=0;i<num;i++){vector<int... 阅读全文
posted @ 2010-04-25 20:40 北海小龙 阅读(314) 评论(0) 推荐(0)
摘要: #include <iostream>#include <string>#include <vector>using namespace std;//注意使用结构体来表示牛奶struct struct_milk{string brand;double price;double volumn;int day;double day_price;};int main(... 阅读全文
posted @ 2010-04-25 20:40 北海小龙 阅读(319) 评论(0) 推荐(0)
摘要: //求阶乘的位数//方法:使用10的对数解决#include <iostream>#include <math.h>using namespace std;int main(){int count;cin>>count;while(count--){int number;cin>>number;double sum = 0;for(int i = 1... 阅读全文
posted @ 2010-04-25 20:39 北海小龙 阅读(262) 评论(0) 推荐(0)
摘要: //主要是解题的思路//f(0) = 7//f(1) = 11//因为f(n)要模3,所以我先将f(0)和f(1)模的3,所以现在是//f(0) = 7 % 3 = 1//f(1) =11 % 3 = 2//f(3) = 3 % 3 = 0//f(4) = 2 % 3 = 2//f(5) = 2 % 3 = 2//f(6) = 4 % 3 = 1//f(7) = 3 % 3 = 0//f(8) =... 阅读全文
posted @ 2010-04-25 20:39 北海小龙 阅读(420) 评论(0) 推荐(0)
摘要: #include <iostream>using namespace std;int main(){int number;cin>>number;//cout<<endl;注意此处一定不要加endl,否则会出现格式错误的警告。int n,m;for(int i=0;i<number;i++){int j = 0;cin>>n>>m;... 阅读全文
posted @ 2010-04-25 20:38 北海小龙 阅读(223) 评论(0) 推荐(0)
摘要: //思路:使用周期实现,周期的开始不一定从头开始#include <iostream>using namespace std;int main(){int a,b;long n;while(cin>>a>>b>>n && (a+b+n)!=0){int result[200] = {0,1,1};if(n<3){cout<... 阅读全文
posted @ 2010-04-25 20:37 北海小龙 阅读(296) 评论(0) 推荐(0)
摘要: //注意:尽量使用C++的string类型,而不要使用c字符串数组#include <iostream>#include <string>using namespace std;int digit_sum(int digit);int main(){string digit;cin>>digit;//这里读入的是string类型,不是int类型,便于求各个位数和... 阅读全文
posted @ 2010-04-25 20:37 北海小龙 阅读(305) 评论(0) 推荐(0)
摘要: #include <iostream>#include <vector>#include <string>using namespace std;//使用结构体实现typedef struct{string color;int time;}balloon;vector<balloon> balloons;void popular_color();vo... 阅读全文
posted @ 2010-04-25 20:36 北海小龙 阅读(130) 评论(0) 推荐(0)
摘要: //求最小公倍数//思路:先求两个数的最小公倍数,前两个数的最小公倍数与第三个数再求最小公倍数,求出最小公倍数后接着与第四个数相求。//求两个数的最小公倍数:利用辗转相除法#include <iostream>using namespace std;int gcd(int a,int b);int main(){ int row,col;cin>>row;for(int i... 阅读全文
posted @ 2010-04-25 20:35 北海小龙 阅读(406) 评论(0) 推荐(0)
摘要: //整数划分问题 思路:见《算法分析与设计》12页//注:使用递归会出现超时的情况,所以本程序将递归改成迭代的形式#include <iostream>using namespace std;int main(){int a[121][121]; //a[i][j]代表整数i最大数不超过jfor(int n=1;n<121;n++){for(int m=1;m<n+1;m+... 阅读全文
posted @ 2010-04-25 20:33 北海小龙 阅读(461) 评论(0) 推荐(0)
摘要: //Prim算法求最小生成树#include <iostream>using namespace std;#define arraysize 501int maxData = 0x7fffffff;//设定最大值int dis[arraysize][arraysize];bool final[arraysize];int d[arraysize];int N;void prim(){i... 阅读全文
posted @ 2010-04-25 20:31 北海小龙 阅读(149) 评论(0) 推荐(0)
摘要: //将边倒转后存放在两个数组中使用两次Dijkstra算法#include <iostream>#include <string.h>//memset的头文件using namespace std;#define arraysize 1001int maxData = 0x7fffffff;//最大整数int dis[arraysize][arraysize];int re... 阅读全文
posted @ 2010-04-25 20:30 北海小龙 阅读(221) 评论(0) 推荐(0)
摘要: //最小生成树Prim算法#include <iostream>#include <string.h>using namespace std;#define arraysize 101int maxData = 0x7fffffff;int dis[arraysize][arraysize];bool flag[arraysize];int d[arraysize];int... 阅读全文
posted @ 2010-04-25 20:30 北海小龙 阅读(125) 评论(0) 推荐(0)
摘要: //Dijkstra#include <iostream>#include <stdio.h>using namespace std;#define max 0x7fffffff//设置图中的两点的距离为无穷大#define arraysize 2001int dis[arraysize][arraysize];bool final[arraysize];int d[arr... 阅读全文
posted @ 2010-04-25 20:29 北海小龙 阅读(293) 评论(0) 推荐(0)
摘要: /*Dij的变形,这道题目的意思大致是,给你起点和终点,还有一些可用来中间过度的点,首先保证你能够跳跃到终点,同时还要保证你的跳跃跨度最小,也就是从起点到终点路径上的最大的那条边要尽量小,这个有点绕,所以我把这道题目改一下等价题:假设你在玩一个闯关游戏,目的是到达终点闯关,走哪条路无关紧要,你把那些点之间的路径看成是怪物,路径长度看成是怪物的能量值,如果你想击败怪物顺利闯关的话,你的能量值必须高于... 阅读全文
posted @ 2010-04-25 20:29 北海小龙 阅读(464) 评论(0) 推荐(0)
摘要: //思路:使用Dijkstra变形做的 #include <iostream>#include <stdio.h>using namespace std;int n,m;int num;int dis[1001][1001];//存储图的临界矩阵int d[1001];//存储最短距离bool flag[1001];//存储是否找到了最短路void Dij(){int ma... 阅读全文
posted @ 2010-04-25 20:28 北海小龙 阅读(351) 评论(0) 推荐(0)
摘要: //迷宫求解问题//思路:深度优先搜索#include <iostream>#include <math.h>using namespace std;#define max 8char map[max][max];int axisx[]={0,-1,0,1};//代表行上的偏移int axisy[]={-1,0,1,0};//代表列上的偏移int dx,dy;int t;i... 阅读全文
posted @ 2010-04-25 20:27 北海小龙 阅读(607) 评论(0) 推荐(0)
摘要: //数值型搜索题 关键点:缩小素数的范围#include <iostream>#include <stdio.h>#include <math.h>using namespace std;int prime[10000];int num = 0;void generate_prime(){for(int i=2;i<10000;++i){bool isPr... 阅读全文
posted @ 2010-04-25 20:26 北海小龙 阅读(211) 评论(0) 推荐(0)