CodeForces 686A Free Ice Cream (水题模拟)

题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于,

那么孩子就会离家。问你最后剩下多少冰激凌,和出走的孩子数量。

析:多水的一个题,就是一个模拟,如果是+,就加上,如果是‘-’,就判断一下,如果不够,就记录下来。

代码如下:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;
typedef long long LL;
char s[5];

int main(){
    int n, x;
    LL sum = 0, t;
    int ans = 0;
    scanf("%d %d", &n, &x);
    sum += x;
    for(int i = 0; i < n; ++i){
        scanf("%s", s);
        scanf("%lld", &t);
        if('+' == s[0])  sum += t;
        else {
            if(sum >= t)  sum -= t;
            else  ++ans;
        }
    }
    printf("%lld %d
", sum, ans);
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5645463.html