第1关:存款买房 - A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import math

total_cost = float(input()) # '请输入总房价:'total_cost为当前房价
annual_salary = float(input()) # '请输入年薪:'
portion_saved = float(input()) / 100 # '请输入月存款比例:'月存款比例,输入30转为30%

# 根据首付款比例计算首付款金额和每个月需要存款数额
down_payment = total_cost * 0.3
monthly_deposit = (annual_salary / 12) * portion_saved

print(f'首付 {down_payment:.2f} 元')
print(f'月存款 {monthly_deposit:.2f} 元')

# 计算多少个月才能存够首付款,结果为整数,不足1月按1个月计算,即向上取整
number_of_months = math.ceil(down_payment / monthly_deposit)

print(f'需要{number_of_months}个月可以存够首付')

第2关:存款买房 - B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
total_cost = float(input())  # total_cost为当前房价
annual_salary = float(input()) # 年薪
portion_saved = float(input()) / 100 # 月存款比例,输入30转为30%
semi_annual_raise = float(input()) / 100 # 输入每半年加薪比例,输入7转化为7%

portion_down_payment = 0.3 # 首付比例,浮点数
down_payment = total_cost * portion_down_payment # 计算首付款

print(f"首付 {down_payment:.2f} 元")

current_savings = 0 # 存款金额,从0开始
number_of_months = 0
monthly_salary = annual_salary / 12 # 月工资
monthly_deposit = monthly_salary * portion_saved # 月存款

while current_savings < down_payment:
current_savings += monthly_deposit
number_of_months += 1
if number_of_months % 6 == 0:
monthly_salary *= 1 + semi_annual_raise
monthly_deposit = monthly_salary * portion_saved

if number_of_months % 12 == 0:
print("第{}个月月末有{:,.0f}元存款".format(number_of_months, current_savings))

print(f"需要{number_of_months}个月可以存够首付")

第3关:存款买房 - C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
total_cost = float(input())  # total_cost为当前房价
annual_salary = float(input()) # 年薪
portion_saved = float(input()) / 100 # 月存款比例,输入30转为30%
semi_annual_raise = float(input()) / 100 # 输入每半年加薪比例,输入7转化为7%

portion_down_payment = 0.3 # 首付比例,浮点数
down_payment = portion_down_payment * total_cost # 首付款
print(f"首付 {down_payment:.2f} 元")

current_savings = 0 # 存款金额,从0开始
number_of_months = 0

while current_savings < down_payment:
current_savings += current_savings * 0.0225 / 12 # 计算每月存款利息
current_savings += annual_salary / 12 * portion_saved # 计算每月存款
number_of_months += 1
if number_of_months % 6 == 0:
annual_salary += annual_salary * semi_annual_raise

if number_of_months % 12 == 0:
print("第{}个月月末有{:,.0f}元存款".format(number_of_months, current_savings))

print(f"需要{number_of_months}个月可以存够首付")