A
递推
#include <iostream>
int main()
{
long long a[90];
a[0] = 1;
a[1] = 2;
int n,i;
while(cin >> n && n)
{
for(i=2;i<n;i++) //递推到第“n”天;
{
a[i] = a[i-1] + a[i-2];
}
cout << a[n-1] << endl;
}
return 0;
}
递归
#include <stdio.h>
#include <stdlib.h>
long long a[90] = {0};
//要定义为longlong型,当“n”接近“90”时,结果便超出int的范围了;
long long f(int n)
{
if(a[n-1]!=0) return a[n-1]; //a[n-1]不为零,即先前已经计算出了结果;
else return a[n-1] = (f(n-1) + f(n-2));
}
int main()
{
a[0] = 1;
a[1] = 2;
int n;
long long sum;
while(scanf("%d",&n)&&n)
{
sum = f(n);
printf("%lld\n",sum);
}
return 0;
}
B
#include <iostream>
using namespace std;
void ff(int n, char x, char z, int y)
{
if(n == 1) printf("Move disk %d from %c to %c\n", y - x, x, y);
else
{
ff(n - 1, x, y, z);
printf("Move disk %d from %c to %c\n", y - x, x, y);
ff(n - 1, z, x, y);
}
}
int main()
{
int n;
cin >> n;
ff(n, 'A', 'B', 'C');
return 0;
}
C
#include <iostream>
using namespace std;
int main()
{
int f1, f2, m;
while(cin >> m && m)
{
f1 = 1;
for(int i = 1; i < m; i ++ )
{
f1 = f2 = (f1 + 1) * 2;
}
cout << f1 << endl;
}
return 0;
}
D
#include <iostream>
using namespace std;
typedef long long ll;
ll a[50]={0};
ll f(int n)
{
if(n<3||a[n]>0)
{
return a[n];
}
return a[n]=f(n-1)+f(n-2);
}
int main()
{
int n;
a[0]=1;
a[1]=1;
a[2]=2;
while(cin >> n)
{
cout << f(n) << endl;
}
return 0;
}
E
#include<stdio.h>
int main()
{
long long i,n,f[21] = {0, 0, 1, 2};
for(i = 4; i < 21; i++)
f[i] = (i - 1) * (f[i - 1] + f[i - 2]);
while(~scanf("%lld", &n))
printf("%lld\n", f[n]);
return 0;
}
F
#include <iostream>
using namespace std;
int f[43];
int main()
{
int n,m,i;
f[1]=0;
f[2]=1;
f[3]=2;
for(i=4;i<=42;++i)
f[i]=f[i-1]+f[i-2];
cin>>n;
while(n--)
{
cin>>m;
cout<<f[m]<<endl;
}
return 0;
}
//深搜及其耗时,分析,每增加一级都要从新算一遍以前的,
//这可定不被允许,简单递归,后用前,还是超时,分析,
//每一组数的输入都需要递归一遍,可能问题出在这儿,改 ,
// 在最开始把40个数循环完,放数组,输入编号,输出数组,
//成了!!!
#include<stdio.h>
int n,ans,flag,x,a[41];
int dai(int t)
{
if(t==2||t==1)
return 1;
else
{
return a[t]=dai(t-1)+dai(t-2);
}
}
int main()
{
int t;
scanf("%d",&t);
dai(40);
while(t--)
{
a[2]=1;a[1]=1;
scanf("%d",&x);
printf("%d\n",a[x]);
}
return 0;
}
#include[HTML_REMOVED]
void main()
{
int c, i,n;
long long nums[21],sum;
nums[0] = nums[1] = 0, nums[2] = 1;
for (i = 3; i <= 20; i)
nums[i] = (i - 1) * (nums[i - 1] + nums[i - 2]);
while (~scanf_s(“%d”, &c))
{
while (c–)
{
scanf_s(“%d”, &n);
sum = 2;
for (i = 3; i <= n; i)
sum *= i;
printf(“%.2f%%\n”, (nums[n] * 100.0) / sum);
}
}
}