首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> C 語言問答

C語言人口增長預測:複利增長模型與循環實現

     編輯:C 語言問答

經典數學建模題:年增長率 1%,何時突破 15 億

數學模型

複利增長公式:Pₙ = P₀ × (1 + r)ⁿ

  • P₀ = 13.0756 億(2005 年基數)
  • r = 1% = 0.01
  • 目標 Pₙ > 15 億

精確解析解

13.0756 × 1.01ⁿ > 15
1.01ⁿ > 15 / 13.0756 ≈ 1.1471
n × ln(1.01) > ln(1.1471)
n > ln(1.1471) / ln(1.01) ≈ 0.1372 / 0.00995 ≈ 13.79

即第 14 年結束時超過,年份 = 2005 + 14 = 2019 年

C 語言迭代實現

#include <stdio.h>

int main() {
    double population = 13.0756;  // 億
    const double growth_rate = 1.01;
    int year = 2005;
    
    printf("年份\t人口(億)\t增長量(億)\n");
    printf("----\t--------\t----------\n");
    
    while (population <= 15.0) {
        double increase = population * (growth_rate - 1.0);
        population *= growth_rate;
        year++;
        printf("%d\t%.4f\t\t%.4f\n", year, population, increase);
    }
    
    printf("\n>>> %d 年中國人口超過 15 億 <<<\n", year);
    return 0;
}

執行輸出

年份	人口(億)	增長量(億)
----	--------	----------
2006	13.2064		0.1308
2007	13.3384		0.1321
2008	13.4718		0.1334
2009	13.6065		0.1347
2010	13.7426		0.1361
2011	13.8800		0.1374
2012	14.0188		0.1388
2013	14.1590		0.1402
2014	14.3006		0.1416
2015	14.4436		0.1430
2016	14.5880		0.1444
2017	14.7339		0.1459
2018	14.8812		0.1473
2019	15.0301		0.1488

>>> 2019 年中國人口超過 15 億 <<<

進階版本:支援任意參數

#include <stdio.h>
#include <math.h>

int predict_year(double initial, double rate_percent, double target, int start_year) {
    double current = initial;
    double rate = 1.0 + rate_percent / 100.0;
    int year = start_year;
    
    while (current <= target) {
        current *= rate;
        year++;
    }
    return year;
}

int main() {
    // 原題驗證
    int year = predict_year(13.0756, 1.0, 15.0, 2005);
    printf("基礎題目: %d 年突破 15 億\n", year);
    
    // 擴展應用
    printf("\n不同增長率下突破 15 億年份:\n");
    for (double r = 0.5; r <= 2.0; r += 0.5) {
        int y = predict_year(13.0756, r, 15.0, 2005);
        printf("增長率 %.1f%% -> %d 年\n", r, y);
    }
    
    // 解析解對比
    double n_exact = log(15.0/13.0756) / log(1.01);
    printf("\n解析解: %.2f 年後突破,即 %d 年\n", n_exact, 2005 + (int)ceil(n_exact));
    
    return 0;
}

工程注意事項

問題風險解決方案
浮點數精度累積誤差導致邊界判斷錯誤用 double,關鍵判斷加微小 epsilon
溢出風險長期預測人口數超出 double 範圍用 long double 或任意精度庫 (GMP)
模型過簡忽略出生率/死亡率/遷徙變化引入年齡結構模型、時間變化參數

擴展思考

  • 連續複利:P(t) = P₀ × e^(rt),更精確的自然增長模型
  • Logistic 增長:引入環境承載力 K,dP/dt = rP(1-P/K),S型曲線
  • 隨機模擬:蒙特卡洛方法模擬出生死亡隨機過程
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved