算法练习题目5(小六零花钱问题)

本文通过编程模拟了小六一年内的财务规划过程,包括每月预算、剩余资金管理和年终利息计算。若全年预算得当,将累积资金交由妈妈保管并获得额外20%的利息回报。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是根据小六每个月的预算,推结果:

 

我:

/*
	小六每个月零花钱有300元,小六会预算这个月的花销,如果有多的钱就会凑成整数给妈妈。
	如果能存到年底的话 就会给她利息。比如总共存1000元  那就能从妈妈那边拿到1200
	如果花销不够的话 就直接输入出现不够的那个月 输出-w
	
	*/
	int totalMoney = 0;//总共存的钱
	int handMoney = 0;//手里的钱
	int monGive = 300;//妈妈每个月给的钱
	int ys[12] = { 0 };//每个月预算花费的钱
	for (int i = 0; i <12; i++)
	{
		cin >>ys[i];

	}
	bool isBreak = false;
	for (int i = 0; i <12; i++)
	{
		int sy = monGive+handMoney - ys[i];
		int currentGiveMom = (sy / 100) * 100;
		totalMoney = totalMoney + currentGiveMom;//总共存的钱
		handMoney = sy - currentGiveMom;
		if (handMoney < 0) {
			isBreak = true;
			//string result = "-" + (i + 1);
			cout <<  "-" << (i+1) << endl;
			break;
		}

	}
	
	if (!isBreak) {
		totalMoney = totalMoney * 1.2 + handMoney;
		cout << totalMoney << endl;
	}

 

 

大佬:

// 预算
	int budget[12] = {0};
	int self = 0, mom = 0;
	int tmp = 0;

	// 得到预算
	for(int i = 0; i < 12; i++) {
		cin >> budget[i];
	}

	bool flag = true;
	for(int i = 0; i < 12; i++) {
		// 判断是否会超支
		tmp = 300 - budget[i];
		if(tmp < 0) {
			cout << "-" << i+1 << endl;
			flag = false;
			break;
		}
		
		// self = 上月结余 + 除出去交给妈妈的,手里还剩的钱
		self = self + tmp%100;   // 月初时手里的钱
		
		// tmp/100  --> 出去预算后,能给他妈妈的钱
		// self/100 --> 月初时手里的钱够百,也要存
		mom = mom + (tmp/100 + self/100)*100;
		
		self = self % 100;   // 月底时,手里还有这些钱
	}
	
	if(flag) {
		cout << mom*1.2 + self << endl;
	}

 

 

总结对比了下:  应该

 handMoney = sy %100;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值