Java 算法练习 ——— CodeWar Day 2

本文记录了作者在CodeWar Day 2的挑战,包括四个问题的详细描述和翻译。第一题涉及投资回报年数计算,第二题是统计字符串中元音的数量,第三题是实现摩斯码解码器,第四题则是判断是否能构成有效三角形。
摘要由CSDN通过智能技术生成

Code War Day 2

  • code war honor 44
  • uestc position 37

quesetion 1

Mr. Scrooge has a sum of money ‘P’ that wants to invest, and he wants to know how many years ‘Y’ this sum has to be kept in the bank in order for this sum of money to amount to ‘D’.
The sum is kept for ‘Y’ years in the bank where interest ‘I’ is paid yearly, and the new sum is re-invested yearly after paying tax ‘T’
Note that the principal is not taxed but only the year’s accrued interest

  Example:
  Let P be the Principal = 1000.00      
    Let I be the Interest Rate = 0.05      
    Let T be the Tax Rate = 0.18      
    Let D be the Desired Sum = 1100.00


  After 1st Year -->
    P = 1041.00
  After 2nd Year -->
    P = 1083.86
  After 3rd Year -->
    P = 1128.30

Thus Mr. Scrooge has to wait for 3 years for the initial pricipal to ammount to the desired sum.
Your task is to complete the method provided and return the number of years ‘Y’ as a whole in order for Mr. Scrooge to get the desired sum.
Assumptions : Assume that Desired Principal ‘D’ is always greater than the initial principal, however it is best to take into consideration that if the Desired Principal ‘D’ is equal to Principal ‘P’ this should return 0 Years.

translation 1

Scrooge将本金(principal)存入银行,每年的利息和交税计算公式如下:result = princial * ( 1 + interest)- princial*interest*tax
给定目标金额、本金、利率、税率,求出需要几年可以达到这个金额。如果目标金额和本金相等,返回0;

public class Money {

    public static int calculateYears(double principal, double interest,  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值