注册 登录
编程论坛 C++教室

程序,我们需要多少年买的房

都督 发布于 2007-09-13 12:39, 552 次点击
有个问题是
假如2015年房价80万,今年我大学毕业第一年月工资是2000元,我的年工资增长率应该至少是多少,到2015年才能买的起房.
用循坏做呀
我想可以这样吗?
for(i=0;i<=7;i++)
{年工资=年工资*(1+年增长率(求的))
if(年工资<=80万)
{break;}
}
不行该怎么算呀
8 回复
#2
HJin2007-09-13 13:03
you don't need to spend money on

0. food
1. housing
2. clothes
3. travel
4. socializing
5. etc.

?

Just a joke.
#3
HJin2007-09-13 13:28

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

#define HOUSE_PRICE 800000.0
#define YEARS 2015-2007

/**
Your annual salary increase is 39.14%
Press any key to continue . . .
*/

float rate(float firstYearSalary)
{
float x=0.0;

do
{
x+=0.0001; // increase by 0.01%
}
while( firstYearSalary * (pow(1.0+x, YEARS)-1)/x < HOUSE_PRICE);

return x*100;
}


int main()
{
float firstYearSalary=24000.0;

printf("Your annual salary increase is %.2f%%\n", rate(firstYearSalary));

return 0;
}

#4
snakeImao2007-09-13 14:50

"if(年工资<=80万)"

照你的意思,2015年,这一年内至少要赚80万罗?
那你的年工资增长率不就是 55.01% 吗?

[此贴子已经被作者于2007-9-13 15:19:48编辑过]

#5
都督2007-09-13 18:28
付首付,三十万
#6
都督2007-09-13 18:35

do
{
x+=0.0001; // increase by 0.01%//为什么是0.0001呀,哪里的呀
}
while( firstYearSalary * (pow(1.0+x, YEARS)-1)/x < HOUSE_PRICE);

return x*100;

#7
都督2007-09-13 18:47
哦知道了,是x从0.0001开始增长,直到我的工资>=HOUSE_PRICE;
就能知道是增长率是多少了,你真聪明呀,

[此贴子已经被作者于2007-9-13 18:48:05编辑过]

#8
都督2007-09-13 19:01
pow(1.0+x, YEARS)-1)/x
我认为这里应该是firstYearSalary*(1.0+x)的7次方
为什么你这是开方,能解释一下吗?谢谢
#9
HJin2007-09-14 16:09
you need to work out the math.

denote the first year salary by a
then 2nd year salary is a(1+x);
3rd year is a(1+x)^2
...
nth year is a(1+x)^n

form year 1 to year n, you will earn (assume that you don't spend any money on food, clothes, etc)

a + a(1+x) + a(1+x)^2 + ...+ a(1+x)^n = a ((1+x)^(n+1) - 1 ) / x

if the last value >= house price, you own the house.
1