人口增长

In a small town the population is p0 = 1000 at the beginning of a year. The population regularly increases by 2 percent per year and moreover 50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to p = 1200 inhabitants?

  • town镇、城镇、城市
  • population人口
  • beginning开始、开头、初期、开端、起头
  • regularly经常
  • increase增加、增长
  • percent百分
  • per每
  • moreover此外
  • more更多
  • over:.adv过度,.prep以上,.n多余、余额
  • inhabitants居民
  • come来、来到、过来
  • greater 更大的、优越、优胜
  • 在一个小镇,人口在年初是p0 = 1000。 人口每年定期增长2%,并且每年有50多个新居民居住在该镇。 该镇需要多少年才能看到其人口大于等于p = 1200?

At the end of the first year there will be:

  • 在第一年末,将有:
    • will be将会

1000 + 1000 * 0.02 + 50 => 1070 inhabitants

At 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 => 1213

It will need 3 entire years.
  • 这将需要3年的时间。
    • will将
    • entire 整个
More generally given parameters:
  • 通常给定参数
    • generally 通常
    • given 给、给定
    • parameters参数

p0, percent, aug (inhabitants coming or leaving each year), p (population to surpass)

inhabitants coming or leaving each year
  • 每年来或离开的居民
    • coming未来、来、过来
    • leaving离开、出发
    • Leaves树叶
    • each每、各、各个、各自
population to surpass
  • 人口要超过
    • surpass超过、超越、赛过、优于
the function nb_year should return n number of entire years needed to get a population greater or equal to p.
  • 函数nb_year应该返回使人口大于或等于p所需的整数n年。
    • should 应该、应当
    • entire整个
    • greater更大,优越,优胜
aug is an integer, percent a positive or null number, p0 and p are positive integers (> 0)
  • aug是整数,正数或空数的百分比,p0p是正整数(> 0)
    • integer整数
    • percent百分比
    • positive正
    • exam考试

example(例)

nb_year(1500, 5, 100, 5000) -> 15
nb_year(1500000, 2.5, 10000, 2000000) -> 10

Note: Don't forget to convert the percent parameter as a percentage in the body of your function: if the parameter percent is 2 you have to convert it to 0.02.

  • 注意:不要忘记将百分比参数转换为函数体内的百分比:如果参数百分比为2,则必须将其转换为0.02。
    • note注意
    • none 没有
    • convert兑换、变换、转换
    • parameter参数
    • per每
    • percent百分
    • percentage百分比
    • body身体
  • [x] Fundamentals(基本原理)

code Analysis(代码分析)

源码

JS描述

//函数封装
function nb_year(p0,percent,aug,p){	
	var count = 0;//计数器
	while (p0 < p){
		++count;
		p0 = p0 + p0*(percent/100) + aug;
	}
	return count;
}
//测试代码
var p0 = 1000, percent = 2, aug = 50, p = 1200;
document.write(p0+'-->'+p+'需要'+nb_year(p0,percent,aug,p)+'年</br>');//3
var p0 = 1500, percent = 5, aug = 100, p = 5000;
document.write(p0+'-->'+p+'需要'+nb_year(p0,percent,aug,p)+'年</br>');//15
var p0 = 1500000, percent = 2.5, aug = 10000, p = 2000000;
document.write(p0+'-->'+p+'需要'+nb_year(p0,percent,aug,p)+'年</br>');//10
var p0 = 1500000, percent = 5, aug = 10000, p = 2000000;
document.write(p0+'-->'+p+'需要'+nb_year(p0,percent,aug,p)+'年</br>');//6
var p0 = 10, percent = 5, aug = 10, p = 50;
document.write(p0+'-->'+p+'需要'+nb_year(p0,percent,aug,p)+'年</br>');//4
//运行结果
1000-->1200需要3年
1500-->5000需要15年
1500000-->2000000需要10年
1500000-->2000000需要6年
10-->50需要4年

C语言描述

#include "stdio.h"
int nb_year(float p0,int percent,int aug,int p){	
	int count = 0;//计数器
	while (p0 < p){
		++count;
		p0 = p0 + p0*(percent/100.0) + aug;
	}
	return count;
}
int main()
{
	printf("%d
",nb_year(1000,2,50,1200));//3
	printf("%d
",nb_year(1500,5,100,5000));//15
	printf("%d
",nb_year(1500000,2.5,10000,2000000));//12
	printf("%d
",nb_year(1500000,5,10000,2000000));//6
	printf("%d
",nb_year(10,5,10,50));//4
}

----------分割线----------

个人主页MrFlySand.github.io


❤️有则改之,无则加勉。如有错误、建议、疑问,评论或联系飞沙QQ:2602629646
❤️本文来自作者:MrFlySand,转载请注明原文链接:https://www.cnblogs.com/MrFlySand/p/13780981.html

原文地址:https://www.cnblogs.com/MrFlySand/p/13780981.html