1029: 猴子吃桃问题

版权声明:看看就好,如需转载,注明一下出处 https://blog.csdn.net/sinat_34337520/article/details/89203265

Description

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

Input

N

Output

桃子总数

Sample Input

10

Sample Output

1534

Note:

采取逆向思维的方法,从后往前推断。

1) 设x1为前一天桃子数,设x2为第二天桃子数, 则:

x2=x1/2-1, x1=(x2+1)*2

x3=x2/2-1, x2=(x3+1)*2

以此类推: x前=(x后+1)*2

2) 从第10天可以类推到第1天,是一个循环过程。

#include <stdio.h>
//版本1:非递归
int main()
{
    int N, n=1;
    scanf("%d", &N);
    for(int i=1; i<N; i++)
        n = (n+1)*2;
    printf("%d", n);
    return 0;
}
//版本2:递归
int fun(int n)
{
    if(n==1)
        return 1;
    else
        return (fun(n-1)+1)*2;
}

int main()
{
    int N;
    scanf("%d", &N);
    printf("%d", fun(N));
}

 

猜你喜欢

转载自blog.csdn.net/sinat_34337520/article/details/89203265
今日推荐