2012 年培養 25 萬人,每年增長 25%,求達到 100 萬人所需年份。
每年人口 = 上年人口 × 1.25,循環累計直到 ≥ 100。
public class PopulationGrowth {
public static void main(String[] args) {
double population = 25; // 萬
int year = 2012;
while (population < 100) {
population *= 1.25;
year++;
}
System.out.println("達到 100 萬人的年份: " + year);
}
}
2012: 25.00
2013: 31.25
2014: 39.06
2015: 48.83
2016: 61.04
2017: 76.29
2018: 95.37
2019: 119.21 // 首次超過 100
答案:2019 年
i++ 與 i *= 1.25 衝突)