C++ 在循环时执行…如果。。。else语句中包含临时变量

C++ 在循环时执行…如果。。。else语句中包含临时变量,c++,if-statement,for-loop,do-while,C++,If Statement,For Loop,Do While,有人问我,在给了我 初始人口(7), 增长率(1.2%), 初始人口年(2011年),以及用以下公式将最终人口与初始人口联系起来的公式: initial_population * exp ( (final_year - initial_year) * (rate/ 100.0)) 对于输入的特定人口,我已使该人口年复一年地增长,如下所示: double pc(0.0); // pc = population entered while (pc <= initial_pop

有人问我,在给了我 初始人口(7), 增长率(1.2%), 初始人口年(2011年),以及用以下公式将最终人口与初始人口联系起来的公式:

initial_population * exp ( (final_year - initial_year) * (rate/ 100.0))
对于输入的特定人口,我已使该人口年复一年地增长,如下所示:

    double pc(0.0); // pc = population entered
    while (pc <= initial_population)
        {
        cout << "How many billion (> 7) ? ";
        cin >> pc;
        };

    int temp(year);
    do {
        ++temp;
        cout << "Population in " << temp << " : " << 
        initial_population * exp ( (final_year - initial_year) * (rate/ 100.0))
        << endl;
        } 
        while ( pc > initial_population * 
                     exp ( (final_year - initial_year) * (rate/ 100.0)));
< >我在C++中所知道的只是直到for和do while循环,当然还有if语句。

有没有人可以帮我,我不需要有完美的答案,只要帮我从这部分开始。例如,当人口翻倍时,如何作出声明,等等


非常感谢。

将初始总体值乘以2保存在单独的变量中。然后在每次迭代中检查当前_总体(我编的一个变量,但它的值应该很明显)是否大于或等于另一个存储的变量,再次将该值乘以2,并将增长率一分为二。大概是这样的:

double population_doubled_check_val = initial_population * 2;
double current_population;
do {
    ++temp;
    current_population = initial_population * exp ( (final_year - initial_year) * (rate/ 100.0));
    cout << "Population in " << temp << " : " << current_population << endl;
    if (current_population >= population_doubled_check_val) {
        population_doubled_check_val *= 2;
        rate /= 2;
        }
    } 
while ( current_population < pc );
double population\u double\u check\u val=初始总体*2;
双倍流动人口;
做{
++温度;
当前人口=初始人口*exp((最终人口-初始人口)*(比率/100.0));
库特
double population_doubled_check_val = initial_population * 2;
double current_population;
do {
    ++temp;
    current_population = initial_population * exp ( (final_year - initial_year) * (rate/ 100.0));
    cout << "Population in " << temp << " : " << current_population << endl;
    if (current_population >= population_doubled_check_val) {
        population_doubled_check_val *= 2;
        rate /= 2;
        }
    } 
while ( current_population < pc );