下面是根据小六每个月的预算,推结果:
我:
/*
小六每个月零花钱有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;