




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、1、 設(shè)計題目 猴子吃桃子問題 有一群猴子摘了一堆桃子,他們每天都吃當(dāng)前桃子的一半且再多吃一個,到了第10天就只余下一個桃子。用多種方法實(shí)現(xiàn)求出原來這群猴子共摘了多少個桃子。二、運(yùn)行環(huán)境(軟、硬件環(huán)境)VC+6.0 PC電腦一臺3、 算法的需求分析 1) 采用數(shù)組數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)上述求解2) 采用鏈數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)上述求解3) 采用遞歸實(shí)現(xiàn)上述求解4) 如果采用4種方法者,適當(dāng)加分/用戶界面int Desk(int n) printf("*n");printf("| 歡迎進(jìn)入猴子吃桃子系統(tǒng) |n");print
2、f("| 1-數(shù)組法 2-鏈表法 3-遞歸法 4-二叉樹法 5-退出 |n");printf("*n"); printf("請輸入要選擇的方法: ");scanf("%d",&n);getchar();system("cls"); /刷新屏幕while(n<1 | n>5)printf("*輸入錯誤 ! 請重新輸入*n");scanf("%d",&n);return n;四、算法概要設(shè)計/采用鏈數(shù)據(jù)結(jié)構(gòu) (棧) 實(shí)現(xiàn)上述求解ty
3、pedef struct int *top; int *base;stack;/初始化一個棧stack Init(stack *s)s->base=(int *)malloc(STACK_SIZE*sizeof(int);if(s->base = NULL)printf("Init failed !n");exit(-1);s->top = s->base;return *s;/二叉樹創(chuàng)建一個大小為DAYS(由用給出)的二叉樹,二叉樹的左孩子節(jié)點(diǎn)存放當(dāng)天的桃子數(shù),右節(jié)點(diǎn)存放數(shù)字1,即為多吃的一個桃子。typedef struct TNodeint d
4、ata;struct TNode *lchild;struct TNode *rchild;tree;/創(chuàng)建一個二叉樹tree CreatTree(tree *T) /T 為指針類型 int n=0,i=0;tree *p,*pr,*T1;T=(tree *)malloc(sizeof(TNode);T1=T;T->data=1; /根節(jié)點(diǎn)內(nèi)的數(shù)據(jù)為第DAYS天的桃子數(shù)for(i=1; i<DAYS; i+)p=(tree *)malloc(sizeof(TNode);pr=(tree *)malloc(sizeof(TNode);pr->lchild=NULL;pr->
5、;rchild=NULL;p->data=0;pr->data=1;T1->lchild=p;T1->rchild=pr;T1=p;T1->lchild=NULL;T1->rchild=NULL;return *T; /返回T的地址/算法框架圖打印共摘的桃子數(shù)量判斷是否執(zhí)行完畢 進(jìn)入猴子吃桃子系統(tǒng)界面 開始 選擇要使用的方法 進(jìn)入該函數(shù)判斷是否執(zhí)行 完畢 繼續(xù)執(zhí)行 N Y N Y5、 算法詳細(xì)設(shè)計#include <stdio.h>#include <stdlib.h>#include "peach.h"/函數(shù)聲明
6、int Desk(int n);void peach_1(void);stack Init(stack *s);void Push(stack *s,int num);void Pop(stack *s,int &num);void peach_2(stack *s);void peach_3(int n,int i);tree CreatTree(tree *T);void calculate(tree *T);void peach_4(tree *T);int main()int data=0;int n=1,i=1;stack s;tree T;s=Init(&s);T=
7、CreatTree(&T);while(1)switch(Desk(n)case 1:peach_1();break;case 2: peach_2(&s);break;case 3:peach_3(n,i);break;case 4:peach_4(&T);break;case 5:exit(0);break;return 0;/頭文件代碼#define DAYS 10 /定義給定的天數(shù)#define STACK_SIZE 5 /棧的容量,實(shí)際只用了一個#define TRUE 1#define ERROR 0void peach_3(int n,int i);/用戶
8、界面int Desk(int n)printf("*n");printf("| 歡迎進(jìn)入猴子吃桃子系統(tǒng) |n");printf("| 1-數(shù)組法 2-鏈表法 3-遞歸法 4-二叉樹法 5-退出 |n");printf("*n");printf("請選擇要使用的方法: ");scanf("%d",&n);getchar();system("cls"); /刷新屏幕while(n<1 | n>5)printf("*輸入錯誤 ! 請
9、重新輸入*n");scanf("%d",&n);return n;/采用數(shù)組數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)上述求解void peach_1(void)int peach50=0;int i=0,j=0;peachDAYS-1=1; /最后一天的桃子數(shù)for(i=DAYS ; i>0; -i , j=i-1)peachj = 2*(peachi + 1);printf("* * * * * * * * * * * * * * * * * * * * *n");printf("* 數(shù) 組 法 * n");printf("*
10、 這群猴子共摘了 %d 個桃子 *n",peach0);printf("* * * * * * * * * * * * * * * * * * * * *n");/采用鏈數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)上述求解typedef structint *top;int *base;stack;/初始化一個棧stack Init(stack *s)s->base=(int *)malloc(STACK_SIZE*sizeof(int);if(s->base = NULL)printf("Init failed !n");exit(-1);s->top =
11、 s->base;return *s;/把當(dāng)天的桃子數(shù)進(jìn)棧void Push(stack *s,int num)*s->top+ = 2*(num + 1);/把前一天的桃子數(shù)出棧void Pop(stack *s,int &num) /&num位地址傳遞,可以直接對參數(shù)進(jìn)行修改num = *-s->top;void peach_2(stack *s)int i=0;int num=0;Push(s,1); /先把最后一天的桃子數(shù)進(jìn)棧i+;while(i < DAYS)Pop(s,num);Push(s,num);i+;printf("* *
12、* * * * * * * * * * * * * * * * * * *n");printf("* 鏈 表 法 * n");printf("* 這群猴子共摘了 %d 個桃子 *n",num);printf("* * * * * * * * * * * * * * * * * * * * *n");void peach_3(int n,int i)if(i =DAYS) /DAYS 為遞歸終止條件 由用戶給出printf("* * * * * * * * * * * * * * * * * * * * *n&quo
13、t;);printf("* 遞 歸 法 *n");printf("* 這群猴子共摘了 %d 個桃子 *n",n);printf("* * * * * * * * * * * * * * * * * * * * *n");elsen = 2*(n + 1); peach_3(n,i+1);/二叉樹typedef struct TNodeint data;struct TNode *lchild;struct TNode *rchild;tree;tree CreatTree(tree *T) /T 為指針類型 int n=0,i=0;t
14、ree *p,*pr,*T1;T=(tree *)malloc(sizeof(TNode);T1=T;T->data=1; /根節(jié)點(diǎn)內(nèi)的數(shù)據(jù)為第DAYS天的桃子數(shù)for(i=1; i<DAYS; i+)p=(tree *)malloc(sizeof(TNode);pr=(tree *)malloc(sizeof(TNode);pr->lchild=NULL;pr->rchild=NULL;p->data=0;pr->data=1;T1->lchild=p;T1->rchild=pr;T1=p;T1->lchild=NULL;T1->r
15、child=NULL;return *T; /返回T的地址/對二叉樹進(jìn)行賦值計算void calculate(tree *T)int i=0; tree *T1,*T2,*T3; /T1,T3分別為T2的左右孩子T2=T;for(i=0; i<DAYS-1; i+)T1=T2->lchild;T3=T2->rchild;T1->data=2*(T2->data + T3->data);T2=T1;/二叉樹遍歷最左下角的孩子節(jié)點(diǎn)void peach_4(tree *T)calculate(T);while(T->lchild != NULL)T=T-&g
16、t;lchild;printf("* * * * * * * * * * * * * * * * * * * * *n");printf("* 二 叉 樹 法 *n");printf("* 這群猴子共摘了 %d 個桃子 *n",T->data);printf("* * * * * * * * * * * * * * * * * * * * *n");六、算法的測試 開始函數(shù)功能 :1-數(shù)組法 2-鏈表法 3-遞歸法 4-二叉樹法 5-退出 選擇一個功能 執(zhí)行該功能/主函數(shù)#include <stdio.
17、h>#include <stdlib.h>#include "peach.h"/函數(shù)聲明int Desk(int n);void peach_1(void);stack Init(stack *s);void Push(stack *s,int num);void Pop(stack *s,int &num);void peach_2(stack *s);void peach_3(int n,int i);tree CreatTree(tree *T);void calculate(tree *T);void peach_4(tree *T);in
18、t main() int data=0; int n=1,i=1; stack s; tree T; s=Init(&s);T=CreatTree(&T); while(1) switch(Desk(n) case 1: peach_1(); break; case 2: peach_2(&s); break; case 3: peach_3(n,i); break; case 4: peach_4(&T); break; case 5: exit(0); break; return 0;/采用數(shù)組數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)上述求解void peach_1(void) int
19、 peach50=0; int i=0,j=0; peachDAYS-1=1; /最后一天的桃子數(shù) for(i=DAYS ; i>0; -i , j=i-1) peachj = 2*(peachi + 1); printf("* * * * * * * * * * * * * * * * * * * * *n"); printf("* 數(shù) 組 法 * n"); printf("* 這群猴子共摘了 %d 個桃子 *n",peach0); printf("* * * * * * * * * * * * * * * * *
20、* * * *n");void peach_2(stack *s) int i=0; int num=0; Push(s,1); /先把最后一天的桃子數(shù)進(jìn)棧i+; while(i < DAYS) Pop(s,num); Push(s,num); i+; printf("* * * * * * * * * * * * * * * * * * * * *n"); printf("* 鏈 表 法 * n"); printf("* 這群猴子共摘了 %d 個桃子 *n",num); printf("* * * * *
21、* * * * * * * * * * * * * * * *n");void peach_3(int n,int i) if(i =DAYS) /DAYS 為遞歸終止條件 由用戶給出 printf("* * * * * * * * * * * * * * * * * * * * *n"); printf("* 遞 歸 法 * n"); printf("* 這群猴子共摘了 %d 個桃子 *n",n); printf("* * * * * * * * * * * * * * * * * * * * *n")
22、; else n = 2*(n + 1); peach_3(n,i+1); /循環(huán)體 /二叉樹遍歷最左下角的孩子節(jié)點(diǎn)void peach_4(tree *T) calculate(T); while(T->lchild != NULL) T=T->lchild; printf("* * * * * * * * * * * * * * * * * * * * *n"); printf("* 二 叉 樹 法 *n"); printf("* 這群猴子共摘了 %d 個桃子 *n",T->data); printf("* * * * * * * * * * * * * * * * * * * * *n");7、 運(yùn)行結(jié)果分析 1、數(shù)組法:創(chuàng)建一個大小為DAYS的一維數(shù)組aDAYS,a0,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 IEC 63522-17:2024 EN-FR Electrical relays - Tests and measurements - Part 17: Shock,acceleration and vibration
- 【正版授權(quán)】 IEC SRD 63301-1:2024 EN Smart city use case collection and analysis – Water systems in smart cities – Part 1: High-level analysis
- 2025-2030年中國脲醛樹脂市場十三五規(guī)劃及投資風(fēng)險評估報告
- 2025-2030年中國翡翠玉鐲行業(yè)市場需求規(guī)模及前景趨勢預(yù)測報告
- 2025-2030年中國空氣凈化系統(tǒng)工程行業(yè)發(fā)展?fàn)顩r及營銷戰(zhàn)略研究報告
- 2025-2030年中國碳酸氫鈉干滅火劑市場運(yùn)營現(xiàn)狀及發(fā)展趨勢分析報告
- 2025-2030年中國硅鋼板行業(yè)運(yùn)行動態(tài)與營銷策略研究報告
- 廣東文藝職業(yè)學(xué)院《數(shù)據(jù)描述與可視化》2023-2024學(xué)年第二學(xué)期期末試卷
- 沈陽職業(yè)技術(shù)學(xué)院《課件設(shè)計與微課制作》2023-2024學(xué)年第二學(xué)期期末試卷
- 四川文化傳媒職業(yè)學(xué)院《汽車數(shù)據(jù)分析》2023-2024學(xué)年第二學(xué)期期末試卷
- 2021年中國高尿酸及痛風(fēng)趨勢白皮書
- 2023年甘肅省卷中考英語真題
- 最全-房屋市政工程安全生產(chǎn)標(biāo)準(zhǔn)化指導(dǎo)圖冊
- 《魅力教師的修煉》讀書心得體會4篇
- 2016年百貨商城商場超市企劃全年活動策劃方案模板
- 15 分章專項(xiàng)練習(xí)-整本書閱讀系列《經(jīng)典常談》名著閱讀與練習(xí)
- 幼兒園衛(wèi)生保健人員任命書(保健醫(yī)生)
- 一課一練┃二年級下冊:1古詩二首
- 財務(wù)報表2019新版-已執(zhí)行新金融和收入準(zhǔn)則(財會〔2019〕6號)
- 2023年湖南食品藥品職業(yè)學(xué)院高職單招(英語)試題庫含答案解析
- GB/T 39096-2020石油天然氣工業(yè)油氣井油管用鋁合金管
評論
0/150
提交評論