P1089津津的储蓄计划-C++编程解析-分支

题目
测试样例
解题思路

津津在每个月的月初,会得到妈妈给的固定的300元。加上津津上个月没有花完的钱,就是津津本月初拥有的钱。此时,津津需要根据自己目前所拥有的钱,和本月的预算进行判断。一种情况是,津津月初拥有的钱不够本月预算。那么,我们此时,已经得到了程序要的一种类型结果。另一种情况是,津津拥有的钱大于等于预算。这种情况下,我们进行存钱和这个月的结余计算。最后,我们根据最后一个月的结余,加上从妈妈那里获得的钱,就是另一种类型结果。

源代码

#include<iostream>
using namespace std;
int main(){
	const int months = 12;     //一年的月份数目 
	const int cash = 300;      //固定的零花钱 
	const int hundred = 100;   //100元的面值 
	const int rate = 20;       //利率 
	int maxMoney = 0;          //月初的钱 
	int saveMoney  = 0;        //存的钱 
	int surplus = 0;           //月底的结余 
	int cost;                  //花费预算 
	for(int i = 0;i < months;i++){
		cin>>cost;
		maxMoney = cash + surplus;
		if(maxMoney < cost){   //不够预算 
			saveMoney = 0 - (i + 1);
			break;
		}
		int num = (maxMoney - cost)/hundred;
		saveMoney += num*hundred;  //存的钱 
		surplus = maxMoney - cost - num*hundred; 
	}
	//计算钱的总数 
	if(saveMoney >= 0){
		saveMoney += saveMoney*rate/hundred + surplus;
	}
	cout<<saveMoney;  //输出 
	return 0;
} 

程序运行结果
程序运行结果1
程序运行结果2
问候

发布了34 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xingzhe_666/article/details/101066319