题目描述
凯和格尔达开了个冰淇凌店。他们最开始有x个冰淇淋。冰淇淋是免费的。人们可以给他们提供d个冰淇淋,也可以从他们这里要d个冰淇淋。若他们的冰淇淋不够给要冰淇淋的人,要冰淇淋的人会失落,他们的冰淇淋不会减少。他们想知道收摊以后,他们还剩多少冰淇淋和有多少失落的人。
输入输出格式
输入格式
输入n,x(1<=n<=1000,0<=x<=10^9),表示有n个人,最开始有x个冰淇淋 接下来每行输入‘+’或‘-’和d,“+ d”表示有人给了d个冰淇淋,“- d”表示有人想要d个冰淇淋
输出格式
输出两个数,剩的冰淇淋数和失落的人数
输入输出样例
5 7
+ 5
- 10
- 20
+ 40
- 20
22 1
5 17
- 16
- 2
- 98
+ 100
- 98
3 2
说明/提示
Consider the first sample.
- Initially Kay and Gerda have 7 packs of ice cream.
- Carrier brings 5 more, so now they have 12 packs.
- A kid asks for 10 packs and receives them. There are only 2 packs remaining.
- Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed.
- Carrier bring 40 packs, now Kay and Gerda have 42 packs.
- Kid asks for 20 packs and receives them. There are 22 packs remaining
CODE
#include <iostream>
using namespace std;
typedef long long ll;
ll n, x, tmp2, cnt;
char tmp1;
int main(){
cin >> x >> n;
while(x--){
cin >> tmp1 >> tmp2;
if(tmp1 == '+')
n += tmp2;
else if(tmp2 > n)
cnt++;
else if(tmp2 < n)
n -= tmp2;
}
cout << n << " " << cnt << endl;
return 0;
}
当然这样做是错的
AC·CODE
#include <iostream>
using namespace std;
long long n, x, ans;
int a[1010];
char c[1010];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> x;
for(int i=1; i<=n; i++){
cin >> c[i] >> a[i];
if(c[i] == '-')a[i] *= -1;
if(x + a[i] >= 0)x += a[i];
else ans++;
}
cout << x << " " << ans << endl;
return 0;
}