A
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1018
#include <iostream>
using namespace std;
long long f[51]={1,2,3};
int n,i;
int main()
{
for (i=3;i<=50;++i)
f[i]=f[i-1]+f[i-2];
while (cin>>n)
{
cout<<f[n-1]<<endl;
}
return 0;
}
B
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1019
#include <iostream>
using namespace std;
long long f[40]={0,3,8};
int n,i;
int main()
{
for (i=3;i<40;++i)
f[i]=2*(f[i-1]+f[i-2]);
while (cin>>n)
{
cout<<f[n]<<endl;
}
return 0;
}
C
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2044
#include <iostream>
using namespace std;
int main()
{
int a,b,i;
long n,f[50];
f[1]=f[2]=1;
while(cin>>a>>b>>n)
{
if(a==0&&b==0&&n==0)break;
if(a==7&&b==7) cout<<"0"<<endl;
else if(a!=7&&b!=7)
{
for(i=3;i<=48;i++)
f[i%48]=(a*f[i-1]+b*f[i-2])%7;
cout<<f[n%48]<<endl;
}
}
}
D
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2058
#include <iostream>
using namespace std;
long long f[151]={0,0,1};
int n,i;
int main()
{
for (i=3;i<=150;i++)
{
f[i]=(i-1)*(f[i-1]+f[i-2]);
}
while (cin>>n)
{
cout<<f[n]<<endl;
}
return 0;
}
E//not mine idea...
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2067
#include <iostream>
#include <iomanip>
#include <string.h>
#include <math.h>
using namespace std;
struct num{int s[1000];int top;}a[260];
int main()
{
void deal(int x,int y,int z);
int i,j,n,m,s,t;
(a[1].s)[0]=1;
(a[2].s)[0]=3;
a[1].top=1;
a[2].top=1;
for(i=3;i<=250;i++)
{
deal(i-1,i-2,i);
}
while(cin>>n)
{
if(n==0){cout<<1<<endl;}
else
{
for(i=a[n].top-1;i>=0;i--){cout<<(a[n].s)[i];}
cout<<endl;
}
}
return 0;
}
void deal(int x,int y,int z)
{
int i,j,l1,l2,max;
int b[1000],c[1000],d[1000];
l1=a[x].top;
l2=a[y].top;memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
for(i=0;i<=l1-1;i++)
{
b[i]=(a[x].s)[i];
}
for(i=0;i<=l2-1;i++)
{
c[i]=2*((a[y].s)[i]);
}
if(l1>l2)
{max=l1;}
else
{max=l2;}
for(i=0;i<=max-1;i++)
{d[i]=b[i]+c[i];}
for(i=0;i<=max-1;i++)
{
if(d[i]>=10)
{
d[i+1]+=d[i]/10;
d[i]=d[i]%10;
if(i==max-1){max+=1;}
}
}
a[z].top=max;
for(i=0;i<=max-1;i++)
{
(a[z].s)[i]=d[i];
}
}
F
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1615
#include <iostream>
using namespace std;
long long f[51]={0,3,6,6};
int n,i;
int main()
{
for (i=4;i<=50;i++)
{
f[i]=f[i-1]+2*f[i-2];
}
while (cin>>n)
{
cout<<f[n]<<endl;
}
return 0;
}
上述代码是错误的,因为当n=3的时候,结果应该是6而不是12。我们在计算n=3的时候,是不存在ABA这样染色的,而后续计算的时候,我们计算了双色染色的情况,如果照此思路,n=3的时候应该正常输出6+3=9,但显然不是。n=3的时候不存在双色染色。而n=4的时候正确结果也应该是12,而不是18。后面的正确性也值得怀疑啊= =。。
所谓递归题或者递推题,目前遇到的水题大多都可用f(n)=a*f(n-1)+b*f(n-2)推出来,上述题仅有巴蜀之危需改换思路,书上有详细介绍。