假设你每年初往银行账户中1000元钱,银行的年利率为4.7%。 一年后,你的账户余额为: 1000 * ( 1 + 0.047) = 1047 元 第二年初你又存入1000元,则两年后账户余额为: (

  • 假设你每年初往银行账户中1000元钱,银行的年利率为4.7%。
    一年后,你的账户余额为:
    1000 * ( 1 + 0.047) = 1047 元
    第二年初你又存入1000元,则两年后账户余额为:
    (1047 + 1000) * ( 1 + 0.047) = 2143.209 元
    以此类推,第10年年末,你的账户上有多少余额?
    注:结果保留2位小数(四舍五入)。

这一题我又是想了很久才找到解决方案,差不多半小时才解决:

# -*- coding: UTF-8 -*-
"""
Created on 2017/3/16
@author: cat
假设你每年初往银行账户中1000元钱,银行的年利率为4.7%。
一年后,你的账户余额为:
1000 * ( 1 + 0.047) = 1047 元
第二年初你又存入1000元,则两年后账户余额为:
(1047 + 1000) * ( 1 + 0.047) = 2143.209 元
以此类推,第10年年末,你的账户上有多少余额?
注:结果保留2位小数(四舍五入)。

f(1) =1000*(1+u)
f(2) =(f(1)+1000) *(1+u)
...
f(n) =( f(n-1)+1000) * (1+u)

"""

def compute(base, update, years):
    c_money = 0  # 当年余额
    c_year = 0  # 当前是第几年
    while c_year < years:
        c_year += 1
        c_money = (c_money + base) * (1 + update)
    return (round(c_money, 2), c_year)


base = 1000
update = 0.047

print "total money and years are ", compute(base, update, 1)
print "total money and years are ", compute(base, update, 2)
print "total money and years are ", compute(base, update, 10)

print

total money and years are  (1047.0, 1)
total money and years are  (2143.21, 2)
total money and years are  (12986.11, 10)

后来一想,还有更简便的方式:

def compu(base, update, years):
    c_money = 0
    while years > 0:
        c_money = (c_money + base) * (1 + update)
        years -= 1
    return round(c_money,2)
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值