/**
*
*/
package base;
/**
* http://t.sina.com.cn/k/Google%25E7%25AC%2594%25E8%25AF%2595%25E8%25AF%2595%25E9%25A2%2598
* 据说是#Google笔试试题# 现在北京有一套房子,价格200万,假设房价每年上涨10%,一个软件工程师每年固定能赚40万。
* 如果他想买这套房子,不贷款,不涨工资,没有其他收入,每年不吃不喝不消费,那么他需要几年才能攒够钱买这套房子?
* A. 5年 B. 7年 C. 8年 D. 9年 E. 永远买不起
*
* @author yangwm Sep 28, 2010 04:22:43 PM
*/
public class BuyHouse {
/**
* @param args
*/
public static void main(String[] args) {
int i = 0;
long housePrice = 2000000;
long salary = 0;
while (salary < housePrice && i < 100) {
housePrice += housePrice * 0.1;
salary += 600000;
i++;
System.out.println(i + " years later, salary " + salary + ", housePrice " + housePrice);
}
System.out.println("oh, my god! " + i + " years later, salary "
+ salary + ", house price " + housePrice + ", I can not buy a house!");
}
}
年赚40万输出:
1 years later, salary 400000, house price 2200000
2 years later, salary 800000, house price 2420000
3 years later, salary 1200000, house price 2662000
4 years later, salary 1600000, house price 2928200
5 years later, salary 2000000, house price 3221020
6 years later, salary 2400000, house price 3543122
7 years later, salary 2800000, house price 3897434
8 years later, salary 3200000, house price 4287177
9 years later, salary 3600000, house price 4715894
10 years later, salary 4000000, house price 5187483
......
99 years later, salary 39600000, house price 25055628576
100 years later, salary 40000000, house price 27561191433
oh, my god! 100 years later, salary 40000000, house price 27561191433, I can not buy a house!
修改为年赚60万输出:(社会还是很阳光的)
1 years later, salary 600000, house price 2200000
2 years later, salary 1200000, house price 2420000
3 years later, salary 1800000, house price 2662000
4 years later, salary 2400000, house price 2928200
5 years later, salary 3000000, house price 3221020
6 years later, salary 3600000, house price 3543122
oh, my god! 6 years later, salary 3600000, house price 3543122, I can not buy a house!