蓝桥杯之生日蜡烛

题目描述:

某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。

现在算起来,他一共吹熄了236根蜡烛。

请问,他从多少岁开始过生日party的?

请填写他开始过生日party的年龄数。

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

题目分析:

类似于求累加之和,于是将原本的累加递归函数稍作修改,可得到:

#include<iostream>

using namespace std;

int f(int s,int e);

int main(){
    int i = 1;
    int j=i;
    while(j<236)
    {
        if(f(i,j) == 236)
            break;
        else if(f(i,j) < 236)
            j++;
        else
            i++;
    }
    cout<<i;
    return 0;
}

int f(int s,int e)
{
    if(s > e)
        return 0;
    if(s == e)
        return s;
    else
        return e+f(s,e-1);
}
原文地址:https://www.cnblogs.com/NikkiNikita/p/9450769.html