P1089津津的储蓄计划

题目描述

津津的零花钱一直都是自己管理。每个月的月初妈妈给津津300300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同。

为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里,到了年末她会加上20\%20%还给津津。因此津津制定了一个储蓄计划:每个月的月初,在得到妈妈给的零花钱后,如果她预计到这个月的月末手中还会有多于100100元或恰好100100元,她就会把整百的钱存在妈妈那里,剩余的钱留在自己手中。

例如1111月初津津手中还有8383元,妈妈给了津津300300元。津津预计1111月的花销是180180元,那么她就会在妈妈那里存200200元,自己留下183183元。到了1111月月末,津津手中会剩下33元钱。

津津发现这个储蓄计划的主要风险是,存在妈妈那里的钱在年末之前不能取出。有可能在某个月的月初,津津手中的钱加上这个月妈妈给的钱,不够这个月的原定预算。如果出现这种情况,津津将不得不在这个月省吃俭用,压缩预算。

现在请你根据2004年1月到12月每个月津津的预算,判断会不会出现这种情况。如果不会,计算到2004年年末,妈妈将津津平常存的钱加上20%还给津津之后,津津手中会有多少钱。

输入输出格式

输入格式:

1212行数据,每行包含一个小于350350的非负整数,分别表示11月到1212月津津的预算。

输出格式:

一个整数。如果储蓄计划实施过程中出现某个月钱不够用的情况,输出-XX,XX表示出现这种情况的第一个月;否则输出到20042004年年末津津手中会有多少钱。

注意,洛谷不需要进行文件输入输出,而是标准输入输出。

说明:

这道题目看似很简单,但是有很多需要注意的地方,很容易写成下面的这种代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std ;

int main(){
    int month ;
    int left_per_month = 0 ;
    int save = 0 ;
    int check = -1 ;    //记录当上述第三种情况出现时的月份。
    bool first = true ; //是否第一次出现上述第三种情况。
    for ( month = 1 ; month <= 12 ; month ++ ){
        int budget_per_month ;
        cin >> budget_per_month ;
        int index = 300 - budget_per_month + left_per_month ;
        if (index >= 100){
            save += index / 100 * 100 ;
            left_per_month = index - index / 100 * 100 ;
        }else if (index < 100 && index >= 0){
            left_per_month = index ;
        }else if (index < 0 && first){
            first = false ;
            check = month ;
        }
    }
    if (check != -1){
        cout << -1 * check ;
    }else{
//**输出到2004年年末津津手中会有多少钱** 题目原句,所以得加上最后一个月的结余。
        cout << save * (1 + 0.2) + left_per_month ;
    }
    return 0 ;
}

实际上理解清楚了:就是下面的代码:很简洁

package select.squnence;

import java.util.Scanner;

public class P1089 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int total=0;int current=0;int i;
        for ( i = 1; i <= 12; ++i) {
            int x=in.nextInt();
            current+=300-x;
            if(current<0) {
                System.out.println("-"+i);
                break;
            }
            if(current>=200) {total+=200;current-=200;}
            if(current>=100) {total+=100;current-=100;}
        }
        if(i==13)System.out.println((int)(total*1.2+current));
    }
}

猜你喜欢

转载自www.cnblogs.com/dgwblog/p/10029995.html