DateTime

描述:

假设银行利率为p%每年,你有a0元钱,希望得到a元,至少需要多久?设初始日期为2016-01-01,写一个方法,以”yyyy-mm-dd”的格式返回得到a的日期。

例如:

date_nb_days(100, 101, 0.98) –> “2017-01-01” (366 days)

date_nb_days(100, 150, 2.00) –> “2035-12-26” (7299 days)

注意:银行将360天视为一年,因此一天的利率应为p/36000

MyCode:

using System;
using System.Linq;

public class DateDays 
{

    public static string DateNbDays(double a0, double a, double p)
        {
            int dayCount = 0;
            int year = 2016;
            int month = 1;
            int[] bm = { 1,3,5,7,8,10,12 };
            int[] sm = { 4,6,9,11 };
            while (a0 < a)
            {
                a0 = a0 * (1 + p / 36000);
                dayCount++;
            }
            while (dayCount >= 366)
            {
                dayCount = year % 4 == 0 ? dayCount - 366 : dayCount - 365;
                year++;
            }
            while (dayCount >= 31)
            {
                if (month == 2 && year % 4 == 0) dayCount -= 29;
                if (month == 2 && year % 4 != 0) dayCount -= 28;
                if (bm.Contains(month)) dayCount -= 31;
                if (sm.Contains(month)) dayCount -= 30;
                month++;
            }
            int day = dayCount + 1;
            string retMonth = month < 10 ? "0"+month.ToString() : month.ToString();
            string retDay = day < 10 ? "0"+day.ToString() : day.ToString();
            return String.Format("{0}-{1}-{2}",year.ToString(),retMonth,retDay);
        }
}

CodeWar:

using System;

public class DateDays 
{    
  public static String DateNbDays(double a0, double a, double p) 
  {
    DateTime Start = new DateTime(2016,1,1);

    int days = 0;
    double dayP = p / 36000;
    while(a0 < a)
    {
      a0 *= (1 + dayP);
      days++;
    }

    return Start.AddDays(days).ToString("yyyy-MM-dd");
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值