【算法】程序猿不写代码是不对的58

本文介绍了一个简单的算法案例,模拟阿明如何管理每月300元的零花钱,包括预算分配、剩余资金存储及年终结算过程。通过具体数值展示算法流程。
package com.kingdz.algorithm.time201705;

/**
 * <pre>
 * 阿明的零花钱
 * 
 * 爸爸每月给阿明零花钱300,阿明每月初制定预算并严格执行
 * 阿明每月初根据预算将剩余的整百元存储于爸爸手上
 * 储蓄的金额不能取出
 * 如果某月预算超过手中剩余则压缩预算
 * 年末,爸爸会根据所存储的金额加上20%的奖励还给阿明
 * 求年末阿明手中的余额
 * </pre>
 * 
 * @author kingdz
 * 
 */
public class Algo19 {

	public static void main(String[] args) {
		// 每月获得的钱数
		int get = 300;
		// 每月的预算
		// int[] cost = { 190, 310, 290, 300, 150, 270, 240, 340, 290, 280, 220,
		// 260 };
		int[] cost = { 250, 230, 280, 280, 320, 350, 330, 250, 290, 280, 220, 260 };
		// 在爸爸处存储的钱数
		int fatherKeep = 0;
		// 每个月儿子保存的钱数
		int sonKeep = 0;
		for (int i = 0; i < cost.length; i++) {
			// 首先加上每个月获得的钱数
			sonKeep = sonKeep + get;

			// 如果剩余大于预算则直接花掉预算
			if (sonKeep >= cost[i]) {
				sonKeep = sonKeep - cost[i];
			} else {
				// 否则剩余为0
				sonKeep = 0;
			}

			// 如果有超过整百的金额就进行储蓄的操作
			if (sonKeep >= 100) {
				fatherKeep = fatherKeep + sonKeep / 100 * 100;
				sonKeep = sonKeep % 100;
			}
			System.out.println(sonKeep);
		}
		// 年末加上剩余和储蓄的利息
		sonKeep = sonKeep + (int) (fatherKeep * 1.2);
		System.out.println("total\t" + sonKeep);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值