1. 程式人生 > 其它 >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 havepacks of ice cream.
  2. Carrier bringsmore, so now they have12packs.
  3. A kid asks forpacks and receives them. There are only2packs remaining.
  4. Another kid asks for20packs. Kay and Gerda do not have them, so the kid goes away distressed.
  5. Carrier bring40packs, now Kay and Gerda have42packs.
  6. Kid asks for20packs and receives them. There are22 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; 
}