Test instructions: Given the initial amount of ice cream, then n operations, if "+", then the number will increase, if "-", if the existing quantity is greater than or equal to the amount to be reduced, then subtract, if less than,
Then the child will leave home. Ask you how many ice creams you have left, and the number of kids that are leaving.
Analysis: A problem of multi-water, is a simulation, if it is +, plus, if it is '-', to judge, if not enough, record down.
The code is as follows:
#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\n", sum, ans); return 0;}
Codeforces 686A free Ice Cream (water problem Simulation)