Monkeys eat peach of algorithm analysis

Monkey eating peach issues: the monkey off his first day of a number of peach, half eaten immediately, not fun, but also eat a; the next morning in turn eaten by the remaining peach half, then eat one; this later, when the tenth day want to eat in the morning, left with a peach. Seeking first day how many peaches were picked?

 

Recurrence relation:

f(n)=f(n-1)/2 -1

f(n-1)=(f(n)+1)*2

Boundary conditions: f (10) = 1

 

#include <iostream>
using namespace std;
int func(int day){
    if(day==10)
        return 1; 
    else
        return (func(day+1)+1)*2; 
}
int main(){
    cout<<"第一天有%d个桃子!"<<func(1)<<endl;
    return 0;

}

Guess you like

Origin www.cnblogs.com/khnl/p/11582864.html