猴子吃桃问题

猴子吃桃问题:

  • 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

递归解决:

123456789101112131415161718
#include <stdio.h>int Cal_Peach(int day,int Left_Peach){    if (day==1) {        return (Left_Peach + 1) * 2;    } else {        int temp = (Left_Peach+1)*2;        return Cal_Peach(--day, temp);    }}int main(){    int day = 9;    int Left_Peach = 1;    int Peach = Cal_Peach(day,Left_Peach);    printf("%d\n",Peach);    return 0;}

for循环解决:

1234567891011121314
#include <stdio.h>int main(){    int day;    int x = 1;    int y = 0;    for (day=9; day>=1; day--)    {        y = ( x + 1 ) * 2;        x = y;    }    printf("%d\n",y);    return 0;}
🔍 ×