Wannafly挑战赛12之银行存款

Wannafly挑战赛12之银行存款

链接:https://www.nowcoder.com/acm/contest/79/A
来源:牛客网

银行存款
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
Special Judge, 64bit IO Format: %lld
题目描述
银行的定期存款一般有1年期、2年期、3年期、5年期四种。
现在我们有1块钱,我们想知道,通过合理安排存款方式,n年以后这1块钱最多会变成几块钱。
假设在这n年里利率不变,且n年以后这笔钱不能处于2年期、3年期、5年期存款年限的中间(否则会变成活期)。
输入描述:

第一行五个数n, r1, r2, r3, r5分别表示年数,1年期年利率,2年期年利率,3年期年利率和5年期年利率。
假设我们有1块钱,i年期存款到期后这1块钱会变成(1 + ri)i块钱。
1 <= n <= 20 且 n为整数,
0.04 <= r1 <= r2 <= r3 <= r5 <= 0.05;

输出描述:

一行一个数表示答案。保留5位小数(绝对误差或相对误差在1e-5之内的结果均判断为通过)。

示例1
输入

8 0.0430 0.0449 0.0458 0.0473

输出

1.44112


代码实现:

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		int n = input.nextInt();     //年数
		double r1 = input.nextDouble();      //1年期年利率
		double r2 = input.nextDouble();      //2年期年利率
		double r3 = input.nextDouble();      //3年期年利率
		double r5 = input.nextDouble();      //5年期年利率
		
		double ans = fun(n, r1, r2, r3, r5);
		System.out.printf("%.5f", ans);
		input.close();
	}

	public static double fun(int n, double r1, double r2, double r3, double r5) {

		if (n == 0) {
			return 0;
		}
		if (n == 1) {
			return r1 + 1;
		}
		if (n == 2) {
			double max = Math.max(Math.pow(r2 + 1, 2), Math.pow(r1 + 1, 1)*fun(n - 1, r1, r2, r3, r5));
			return max;
		}
		if (n == 3) {
			double max = Math.max(Math.pow(r3 + 1, 3), Math.pow(r2 + 1, 2)*fun(n - 2, r1, r2, r3, r5));
			max = Math.max(max, Math.pow(r1 + 1, 1)*fun(n - 1, r1, r2, r3, r5));
			return max;
		}
		if (n == 4) {
			double max = Math.max(Math.pow(r3 + 1, 3)*fun(n - 3, r1, r2, r3, r5), Math.pow(r2 + 1, 2)*fun(n - 2, r1, r2, r3, r5));
			max = Math.max(max, Math.pow(r1 + 1, 1)*fun(n - 1, r1, r2, r3, r5));
			return max;
		}
		if (n == 5) {
			double max = Math.max(Math.pow(r5 + 1, 5), Math.pow(r3 + 1, 3)*fun(n - 3, r1, r2, r3, r5));
			max = Math.max(max, Math.pow(r2 + 1, 2)*fun(n - 2, r1, r2, r3, r5));
			max = Math.max(max, Math.pow(r1 + 1, 1)*fun(n - 1, r1, r2, r3, r5));
			return max;
		}
		//n >= 5
		double max = Math.max(Math.pow(r5 + 1, 5)*fun(n - 5, r1, r2, r3, r5), Math.pow(r3 + 1, 3)*fun(n - 3, r1, r2, r3, r5));
		max = Math.max(max, Math.pow(r2 + 1, 2)*fun(n - 2, r1, r2, r3, r5));
		max = Math.max(max, Math.pow(r1 + 1, 1)*fun(n - 1, r1, r2, r3, r5));
		return max;
	}

}




编译结果:




查阅相关github代码请至:https://github.com/striner/javaCode/blob/master/bankAccount












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值