其他分享
首页 > 其他分享> > CF686A Free Ice Cream(洛谷+模拟)

CF686A Free Ice Cream(洛谷+模拟)

作者:互联网

题目描述

凯和格尔达开了个冰淇凌店。他们最开始有x个冰淇淋。冰淇淋是免费的。人们可以给他们提供d个冰淇淋,也可以从他们这里要d个冰淇淋。若他们的冰淇淋不够给要冰淇淋的人,要冰淇淋的人会失落,他们的冰淇淋不会减少。他们想知道收摊以后,他们还剩多少冰淇淋和有多少失落的人。

输入输出格式

输入格式

输入n,x(1<=n<=1000,0<=x<=10^9),表示有n个人,最开始有x个冰淇淋 接下来每行输入‘+’或‘-’和d,“+ d”表示有人给了d个冰淇淋,“- d”表示有人想要d个冰淇淋

输出格式

输出两个数,剩的冰淇淋数和失落的人数

输入输出样例

输入 #1
5 7
+ 5
- 10
- 20
+ 40
- 20
输出 #1
22 1
输入 #2
5 17
- 16
- 2
- 98
+ 100
- 98
输出 #2
3 2

说明/提示

Consider the first sample.

  1. Initially Kay and Gerda have 7 packs of ice cream.
  2. Carrier brings 5 more, so now they have 12 packs.
  3. A kid asks for 10 packs and receives them. There are only 2 packs remaining.
  4. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed.
  5. Carrier bring 40 packs, now Kay and Gerda have 42 packs.
  6. 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; 
}

标签:CF686A,洛谷,packs,冰淇淋,Ice,cin,long,20,tmp2
来源: https://www.cnblogs.com/zjy15000284340/p/15440709.html