當前位置:編程學習大全網 - 編程軟體 - 猴子吃桃問題(3種方法實現)

猴子吃桃問題(3種方法實現)

/*猴子吃桃*/

#include "malloc.h"

int day[10]; //每天的桃數

struct d{

int tao; //桃

struct d *yesterday; //昨天的指針

}today;

int all;

int Fun1()

{

int i;

day[9]=1;

for(i=8;i>-1;i--) //從最後壹天算起

{

day[i]=(day[i+1]+1)*2;

}

printf("%d\n",day[0]); //輸出第壹天的

}

int Fun2()

{

int i;

int tot;

struct d *p;

today.tao=1;

p=&today; //先到最後壹天

i=1;

while(i<10)

{

tot=p->tao; //保存當前天的桃

p->yesterday=malloc(sizeof(struct d)); //分配空間,並當前天的前壹天指向該地址

p=p->yesterday; //當前天變為前壹天

p->tao=(tot+1)*2; //計算

i++;} //下壹天

printf("%d\n",p->tao); //輸出

}

int Fun3(d)

int d;

{

if(d==1) return 1; //遞歸出口

else return (Fun3(d-1)+1)*2; //計算

}

int main()

{

Fun1();

Fun2();

all=Fun3(10);

printf("%d",all);

getchar();

}

  • 上一篇:華碩b660重炮手支持m2 4.0嗎
  • 下一篇:數據溢出是什麽意思?
  • copyright 2024編程學習大全網