递推
SZ 斐波拉契数列
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
你应该很熟悉斐波拉契数列,对吗?下面是SZ斐波拉契数列:
n=1 F(n)=a
n=2 F(n)=b
n>2 and n is odd F(n)=F(n-1)+F(n-2)
n>2 and n is even F(n)=F(n-1)+F(n-2)+F(n-3)
这里a和b为常数,给出a,b和n,你的任务是计算F(n)。
n=1 F(n)=a
n=2 F(n)=b
n>2 and n is odd F(n)=F(n-1)+F(n-2)
n>2 and n is even F(n)=F(n-1)+F(n-2)+F(n-3)
这里a和b为常数,给出a,b和n,你的任务是计算F(n)。
Input
输入的第1行是一个正整数T(T<=10),表示数据的组数。每组数据包含3个正整数a,b和n(a<=10, b<=10, n<=30).
Output
对每组输入输出一个整数F(n).
Sample Input
2 1 2 3 1 3 6
Sample Output
3 24
#include<iostream>
using namespace std;
int a,b;
int fan(int n)
{
if(n==1) return a;
if(n==2) return b;
else
{
if(n&1) return fan(n-1)+fan(n-2);
else return fan(n-1)+fan(n-2)+fan(n-3);
}
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>a>>b>>n;
cout<<fan(n)<<endl;
}
return 0;
}
骨牌铺方格
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.
例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:
Input
输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50)。
Output
对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。
Sample Input
1 3 2Sample Output
#include<iostream>1 3 2 题目很水不过开始没考虑大数wrong了一次#include<stdio.h>
__int64 p[52];
int main()
{
__int64 i,t;
p[1]=1;
p[2]=2;
for(i=3;i<=51;i++)
{
p[i]=p[i-1]+p[i-2];
}
while(scanf("%I64d",&t)!=EOF)
{
printf("%I64d\n",p[t]);
}
return 0;
}Rabbit
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
The rabbits have powerful reproduction ability. One pair of adult rabbits can give birth to one pair of kid rabbits every month. And after m months, the kid rabbits can become adult rabbits.
As we all know, when m=2, the sequence of the number of pairs of rabbits in each month is called Fibonacci sequence. But when m<>2, the problem seems not so simple. You job is to calculate after d months, how many pairs of the rabbits are there if there is exactly one pair of adult rabbits initially. You may assume that none of the rabbits dies in this period.Input
The input may have multiple test cases. In each test case, there is one line having two integers m(1<=m<=10), d(1<=d<=100), m is the number of months after which kid rabbits can become adult rabbits, and d is the number of months after which you should calculate the number of pairs of rabbits. The input will be terminated by m=d=0.Output
You must print the number of pairs of rabbits after d months, one integer per line.Sample Input
2 3 3 5 1 100 0 0Sample Output
5 9 1267650600228229401496703205376简单的递推加大数处理#include<iostream>
#include<string>
using namespace std;
string lmx(string s1,string s2)
{
string smax=s2,smin=s1;
if(s1.length()>s2.length()) {smax=s1;smin=s2;}
int len1=smax.length(),len2=smin.length(),i,j;
for(i=len1-1,j=len2-1;j>=0;j--,i--)
{
smax[i]=smax[i]+smin[j]-'0';
}
for(i=len1-1;i>=1;i--)
{
if(smax[i]>'9')
{
smax[i]-=10;
smax[i-1]+=1;
}
}
if(smax[0]>'9') {smax[0]-=10;smax='1'+smax;}
return smax;
}
int main()
{
string fib[105];
int m,d,i;
while(cin>>m>>d)
{
if(m==0&&d==0) break;
fib[0]="1";
if(d<=m) for(i=1;i<=d;i++) fib[i]=lmx(fib[i-1],"1");
else
{
for(i=1;i<m;i++) fib[i]=lmx(fib[i-1],"1");
for(i=m;i<=d;i++)
{
fib[i]=lmx(fib[i-1],fib[i-m]);
}
}
cout<<fib[d]<<endl;
}
}
Train Problem II
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
Output
For each test case, you should output how many ways that all the trains can get out of the railway.
Sample Input
1 2 3 10Sample Output
Catlan数列的高精度应用1 2 5 16796
using namespace std;
int p[101][101];
int cnt[101];
int main()
{
int i,j,n,len=1,temp,r;
p[1][0]=1;
cnt[1]=1;
for(i=2;i<=100;i++)
{
for(j=0;j<len;j++)
{
p[i][j]=p[i-1][j]*(4*i-2);//相乘
}
for(j=0,r=0;j<len;j++)//相乘结果处理
{
temp=r+p[i][j];
p[i][j]=temp%10;
r=temp/10;
}
while(r)//进位判断
{
p[i][len++]=r%10;
r/=10;
}
for(j=len-1,r=0;j>=0;j--)//除法
{
temp=r*10+p[i][j];
p[i][j]=temp/(i+1);
r=temp%(i+1);
}
while(p[i][len-1]==0) len--;
cnt[i]=len;
}
while(cin>>n)
{
for(i=cnt[n]-1;i>=0;i--) cout<<p[n][i];
cout<<endl;
}
return 0;
}
为了明天所以选择坚定的执着今天。