Growth of a Population

来源:互联网 发布:翅片式换热器计算软件 编辑:程序博客网 时间:2024/05/20 21:59

一、问题描述

在一个小镇,人口在一年初是p0 = 1000。 人口每年定期增加2%,此外每年有50个新居民生活在城里。 城市需要多少年才能看到其人口大于或等于p = 1200居民?

At the end of the first year there will be: 1000 + 1000 * 0.02 + 50 => 1070 inhabitantsAt the end of the 2nd year there will be: 1070 + 1070 * 0.02 + 50 => 1141 inhabitants (number of inhabitants is an integer)At the end of the 3rd year there will be:1141 + 1141 * 0.02 + 50 => 1213It will need 3 entire years.

More generally given parameters:

p0, (一年之初的人口数)percent, (每年的人口增长率)aug (每年离开或者新定居的居民), p (超越的人口数,作对比用)aug是 int 型的数(> 0),percent 为positive or null floating number,p0和p是正整数(> 0)函数nb_year应该返回整数 n ,以获得大于或等于p的总体数。

Note: 不要忘记在函数体中将参数 percent 转换为百分比:如果参数percent为2,则必须将其转换为0.02。


解决方案

class Arge {    public static int nbYear(int p0, double percent, int aug, int p) {        int n=0;        while(p0<p){          p0 = (int)(p0 + 0.01 * percent * p0 + aug);          n++;        }        return n;    }}
0 0