递推与递归 一 实验目的
1. 掌握用递推方法建立问题的求解模型 2. 掌握用递归方法建立问题的求解模型 3. 掌握递推与递归的程序编写、调试与测试 二 实验设备 一台PC机,Visual C++ 6.0开发环境 三 实验内容 1. Children’s Queue l 题目描述: There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children? l 输入: There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000) l 输出: For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs. l 输入样例: 1 2 3 l 输出样例 1 2 4
2. 错排问题
l 题目描述: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!
做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样。
话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的。比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情。如果套用一句经典的评语,我们可以这样总结:一个人做错一道选择题并不难,难的是全部做错,一个不对。
不幸的是,这种小概率事件又发生了,而且就在我们身边:
事情是这样的——HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟!
现在的问题是:请大家帮可怜的8006同学计算一下,一共有多少种可能的错误方式呢?
l 输入: 输入数据包含多个多个测试实例,每个测试实例占用一行,每行包含一个正整数n(1<n<=20),n表示8006的网友的人数。 l 输出: 对于每行输入请输出可能的错误方式的数量,每个实例的输出占用一行。 l 输入样例: 2 3 l 输出样例 1 2
3. 数字旋转方阵 l 题目描述: 编程输出如图所示的数字旋转方阵。 file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml9532\wps6.png l 输入: 输入数据有若干行,每行有一个整数(n < =30). l 输出: 一个n*n的数字旋转方阵 l 输入样例: 5 6 l 输出样例 file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml9532\wps2.png file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml9532\wps3.png
四 实验结果 1. 解题思路: 源程序:
- #include<iostream>
- using namespace std;
- int mat[1001][246];
- int main()
- {
- int i,j,k;
- int n,m;
- int flag,temp;
- int re,pr;
- mat[1][0]=1;
- mat[2][0]=2;
- mat[3][0]=4;
- mat[4][0]=7;
- for(i=5;i<=1001;i++)
- {
- temp=0;
- for(j=0;j<246;j++)
- {
- mat[ i][j]=mat[i-1][j]+mat[i-2][j]+mat[i-4][j]+temp;
- temp=0;
- if(mat[ i][j]>9)
- {
- temp=mat[ i][j]/10;
- mat[ i][j]=mat[ i][j]%10;
- }
- }
- }
- while(scanf("%d",&n)!=EOF)
- {
- flag=0;
- for(i=245;i>=0;i--)
- {
- if(!flag&&mat[n][ i]) flag=1;
- if(flag) printf("%d",mat[n][ i]);
- }
- printf("\n");
- }
- return 0;
- }
- 2. 解题思路:
- 源程序:#include<iostream>
- #include<string>
- using namespace std;
- string StringAdd(string a,string b){
- int maxlen=a.length()>b.length()?a.length()+1:b.length()+1,carry=0,i,result;
- string sum="";
- while(a.length()<maxlen)
- a="0"+a;
- while(b.length()<maxlen)
- b="0"+b;
- while(sum.length()<maxlen)
- sum="0"+sum;
- for(i=maxlen-1;i>0;i--){
- result=a[ i]+b[ i]-2*'0'+carry;
- sum[ i]=result%10+'0';
- carry=result/10;
- }
- sum[0]=carry+'0';
- while(sum[0]=='0' && sum.length()>1)
- sum.erase(0,1);
- return sum;
- }
- string StringAndNumberMultiply(string a,int n){
- if(n==0) return "0";
- string sum=a;
- n--;
- while(n--)
- sum=StringAdd(sum,a);
- return sum;
- }
- int main(){
- int i;
- string lib[21]={"0","0","1"};
- for(i=3;i<21;i++)
- lib[ i]=StringAndNumberMultiply(StringAdd(lib[i-1],lib[i-2]),i-1);
- while(cin>>i)
- cout<<lib[ i]<<endl;
- return 0;
- }
- 3. 解题思路:
- 源程序:#include<stdio.h>
- int a[11][11],i,j,num,size;
- int xuanzhuan(int n)
- {
- if(n==0)return 0;
- if(n==1)
- {
- a[ i][j]=num++;
- return 0;
- }
- for(int k=1;k<n;k++){//粉色方向
- a[ i][j]=num++;
- i++;
- }
- for(int k=1;k<n;k++){//蓝色方向
- a[ i][j]=num++;
- j++;
- }
- for(int k=1;k<n;k++){
- //红色方向
- a[ i][j]=num++;
- i--;
- }
- for(int k=1;k<n;k++){
- //黄色方向
- a[ i][j]=num++;
- j--;
- }
- i++; //注意用i++和j++来调整下一次递归时的起点
- j++; //第一圈的起点为图中的1位置,第二圈的起点为图中21位置,第三次在33
- xuanzhuan(n-2); //下次循环时边长-2
- }
- int main()
- {
- i=j=1;
- num=1;
- printf("请输入旋转数组的边长(1到10之间):\n");
- scanf("%d",&size);
- xuanzhuan(size);
- for(int i=1;i<=size;i++)//打印存储好的数组
- {
- for(int j=1;j<=size;j++)
- printf("%3d ",a[ i][j]);
- printf("\n");
- }
- return 0;
- }
复制代码
|