遞推與遞歸 一 實(shí)驗(yàn)?zāi)康?/font>
1. 掌握用遞推方法建立問(wèn)題的求解模型 2. 掌握用遞歸方法建立問(wèn)題的求解模型 3. 掌握遞推與遞歸的程序編寫、調(diào)試與測(cè)試 二 實(shí)驗(yàn)設(shè)備 一臺(tái)PC機(jī),Visual C++ 6.0開(kāi)發(fā)環(huán)境 三 實(shí)驗(yàn)內(nèi)容 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. 錯(cuò)排問(wèn)題
l 題目描述: 大家常常感慨,要做好一件事情真的不容易,確實(shí),失敗比成功容易多了!
做好“一件”事情尚且不易,若想永遠(yuǎn)成功而總從不失敗,那更是難上加難了,就像花錢總是比掙錢容易的道理一樣。
話雖這樣說(shuō),我還是要告訴大家,要想失敗到一定程度也是不容易的。比如,我高中的時(shí)候,就有一個(gè)神奇的女生,在英語(yǔ)考試的時(shí)候,竟然把40個(gè)單項(xiàng)選擇題全部做錯(cuò)了!大家都學(xué)過(guò)概率論,應(yīng)該知道出現(xiàn)這種情況的概率,所以至今我都覺(jué)得這是一件神奇的事情。如果套用一句經(jīng)典的評(píng)語(yǔ),我們可以這樣總結(jié):一個(gè)人做錯(cuò)一道選擇題并不難,難的是全部做錯(cuò),一個(gè)不對(duì)。
不幸的是,這種小概率事件又發(fā)生了,而且就在我們身邊:
事情是這樣的——HDU有個(gè)網(wǎng)名叫做8006的男性同學(xué),結(jié)交網(wǎng)友無(wú)數(shù),最近該同學(xué)玩起了浪漫,同時(shí)給n個(gè)網(wǎng)友每人寫了一封信,這都沒(méi)什么,要命的是,他竟然把所有的信都裝錯(cuò)了信封!注意了,是全部裝錯(cuò)喲!
現(xiàn)在的問(wèn)題是:請(qǐng)大家?guī)涂蓱z的8006同學(xué)計(jì)算一下,一共有多少種可能的錯(cuò)誤方式呢?
l 輸入: 輸入數(shù)據(jù)包含多個(gè)多個(gè)測(cè)試實(shí)例,每個(gè)測(cè)試實(shí)例占用一行,每行包含一個(gè)正整數(shù)n(1<n<=20),n表示8006的網(wǎng)友的人數(shù)。 l 輸出: 對(duì)于每行輸入請(qǐng)輸出可能的錯(cuò)誤方式的數(shù)量,每個(gè)實(shí)例的輸出占用一行。 l 輸入樣例: 2 3 l 輸出樣例 1 2
3. 數(shù)字旋轉(zhuǎn)方陣 l 題目描述: 編程輸出如圖所示的數(shù)字旋轉(zhuǎn)方陣。 file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml9532\wps6.png l 輸入: 輸入數(shù)據(jù)有若干行,每行有一個(gè)整數(shù)(n < =30). l 輸出: 一個(gè)n*n的數(shù)字旋轉(zhuǎ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
四 實(shí)驗(yàn)結(jié)果 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++){//藍(lán)色方向
- 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++來(lái)調(diào)整下一次遞歸時(shí)的起點(diǎn)
- j++; //第一圈的起點(diǎn)為圖中的1位置,第二圈的起點(diǎn)為圖中21位置,第三次在33
- xuanzhuan(n-2); //下次循環(huán)時(shí)邊長(zhǎng)-2
- }
- int main()
- {
- i=j=1;
- num=1;
- printf("請(qǐng)輸入旋轉(zhuǎn)數(shù)組的邊長(zhǎng)(1到10之間):\n");
- scanf("%d",&size);
- xuanzhuan(size);
- for(int i=1;i<=size;i++)//打印存儲(chǔ)好的數(shù)組
- {
- for(int j=1;j<=size;j++)
- printf("%3d ",a[ i][j]);
- printf("\n");
- }
- return 0;
- }
復(fù)制代碼
|