已知2018年末中国人口为142706万,人口增长为0.381%;印度人口为135405万,人口增长为1.11%,如果按此数据进行推演,印度将在哪一年人口超过中国。
用while循环,通过人口增长率来计算每年中国和印度的人口数量,然后不断比较即可。代码如下:
ChinesePopulation = 1427060000
IndianPopulation = 1354050000
cRate = 0.381
iRate = 1.11
year = 1
while IndianPopulation<=ChinesePopulation:
ChinesePopulation = ChinesePopulation*(1+cRate/100)
IndianPopulation = IndianPopulation*(1+iRate/100)
year=year+1
print("经过", year,"年,印度人口数量是",int(IndianPopulation),",中国人口数量是:",int(ChinesePopulation))