久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2461|回復: 0
打印 上一主題 下一主題
收起左側

遞推與遞歸

[復制鏈接]
跳轉到指定樓層
樓主
ID:670425 發表于 2019-12-22 14:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
遞推與遞歸
實驗目的
        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 輸入:
輸入數據包含多個多個測試實例,每個測試實例占用一行,每行包含一個正整數n1<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. 解題思路:
源程序:
  1. #include<iostream>
  2. using namespace std;
  3. int mat[1001][246];
  4. int main()
  5. {
  6. int i,j,k;
  7. int n,m;
  8. int flag,temp;
  9. int re,pr;
  10.         mat[1][0]=1;
  11.   mat[2][0]=2;
  12.   mat[3][0]=4;
  13.   mat[4][0]=7;
  14.   for(i=5;i<=1001;i++)
  15.   {
  16.    temp=0;
  17.    for(j=0;j<246;j++)
  18.    {
  19.     mat[ i][j]=mat[i-1][j]+mat[i-2][j]+mat[i-4][j]+temp;
  20.     temp=0;
  21.     if(mat[ i][j]>9)
  22.     {
  23.      temp=mat[ i][j]/10;
  24.      mat[ i][j]=mat[ i][j]%10;
  25.     }
  26.    }

  27.   }

  28. while(scanf("%d",&n)!=EOF)
  29. {  
  30.   flag=0;
  31.   for(i=245;i>=0;i--)
  32.   {
  33.    if(!flag&&mat[n][ i]) flag=1;
  34.    if(flag) printf("%d",mat[n][ i]);
  35.   }
  36.   printf("\n");
  37. }

  38. return 0;
  39. }


  40. 2. 解題思路:
  41. 源程序:#include<iostream>
  42. #include<string>
  43. using namespace std;
  44. string StringAdd(string a,string b){
  45.     int maxlen=a.length()>b.length()?a.length()+1:b.length()+1,carry=0,i,result;  
  46.     string sum="";
  47.     while(a.length()<maxlen)
  48.         a="0"+a;
  49.     while(b.length()<maxlen)  
  50.         b="0"+b;
  51.     while(sum.length()<maxlen)
  52.         sum="0"+sum;
  53.     for(i=maxlen-1;i>0;i--){
  54.         result=a[ i]+b[ i]-2*'0'+carry;
  55.         sum[ i]=result%10+'0';
  56.         carry=result/10;
  57.     }
  58.     sum[0]=carry+'0';
  59.     while(sum[0]=='0' && sum.length()>1)  
  60.         sum.erase(0,1);
  61.     return sum;
  62. }

  63. string StringAndNumberMultiply(string a,int n){
  64.     if(n==0) return "0";
  65.     string sum=a;
  66.     n--;
  67.     while(n--)
  68.         sum=StringAdd(sum,a);
  69.     return sum;
  70. }

  71. int main(){
  72.     int i;
  73.     string lib[21]={"0","0","1"};
  74.     for(i=3;i<21;i++)
  75.         lib[ i]=StringAndNumberMultiply(StringAdd(lib[i-1],lib[i-2]),i-1);
  76.     while(cin>>i)
  77.         cout<<lib[ i]<<endl;
  78.     return 0;
  79. }



  80. 3. 解題思路:
  81. 源程序:#include<stdio.h>
  82. int a[11][11],i,j,num,size;
  83. int xuanzhuan(int n)
  84. {
  85.         if(n==0)return 0;
  86.         if(n==1)
  87.         {
  88.                 a[ i][j]=num++;
  89.                 return 0;
  90.         }               
  91.         for(int k=1;k<n;k++){//粉色方向
  92.                 a[ i][j]=num++;
  93.         i++;
  94.         }
  95.         for(int k=1;k<n;k++){//藍色方向
  96.                 a[ i][j]=num++;
  97.                 j++;
  98.         }
  99.         for(int k=1;k<n;k++){
  100.                 //紅色方向
  101.                 a[ i][j]=num++;
  102. i--;
  103.         }
  104.         for(int k=1;k<n;k++){
  105.                 //黃色方向
  106.                 a[ i][j]=num++;
  107.                 j--;
  108.         }
  109.         i++;                 //注意用i++和j++來調整下一次遞歸時的起點
  110.         j++;                  //第一圈的起點為圖中的1位置,第二圈的起點為圖中21位置,第三次在33
  111.         xuanzhuan(n-2);                        //下次循環時邊長-2
  112. }
  113. int main()
  114. {
  115.         i=j=1;
  116.         num=1;
  117.         printf("請輸入旋轉數組的邊長(1到10之間):\n");
  118.         scanf("%d",&size);
  119.         xuanzhuan(size);
  120.         for(int i=1;i<=size;i++)//打印存儲好的數組
  121.         {
  122.                 for(int j=1;j<=size;j++)
  123.                         printf("%3d ",a[ i][j]);
  124.                 printf("\n");
  125.         }
  126. return 0;
  127. }
復制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本一区二区三区四区 | 免费精品视频一区 | 特一级毛片| 国产高清亚洲 | 天天综合久久 | 激情欧美日韩一区二区 | 中文字幕在线第一页 | 久久9视频 | 精品在线一区 | 99免费看 | 国产精品久久av | 国产视频福利一区 | 亚洲一区二区三区在线视频 | 人人澡视频 | 在线日韩福利 | 亚洲视频在线一区 | 精品麻豆剧传媒av国产九九九 | 亚洲视频免费在线看 | 国产一区二区三区亚洲 | 欧美色综合一区二区三区 | 欧美激情久久久久久 | 国产色播av在线 | 日韩中文一区二区三区 | 中文字幕在线播放不卡 | 国产不卡一 | 欧美精品久久一区 | 91精品国产一区二区三区动漫 | 国产高清精品在线 | 成人黄色在线 | 91av免费版| 成人一区二区视频 | 全部免费毛片在线播放网站 | 久久亚洲一区 | 7777在线 | 亚洲视频免费在线观看 | 日韩美香港a一级毛片免费 国产综合av | 日本午夜精品一区二区三区 | 精品久久久999| 国产免费观看一区 | 国产精品69毛片高清亚洲 | 国产一区二区 |