题目:
儿童排队领冰激凌,给你两个数n,x分别代表接下来有n行与初始的冰激淋数;接下来n行,每行有一个字符('+'or‘-’),还有一个整数d,+d表示新增的冰激
凌数(由搬运工搬运到此),-d表示儿童将要领走的冰激凌数(当剩余的数量有足够这么多时),求没领到冰激凌的儿童.
分析:
由案例可以知道当目前的冰激凌数不足以分发给当前儿童所要的数目d时,该儿童不能领到冰激凌
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24
25 int main()
26 {
27 int n;
28 LL x, sum;
29 int pos;
30 while(scanf("%d%lld", &n, &x)==2)
31 {
32 int i, j;
33 char c;
34 LL y;
35 sum = 0;
36 pos = 0;
37 sum += x;
38 for(i = 1; i <= n; i++ )
39 {
40 //输入数字时,编译器能区分数字之间的空格或换行(输入的两个数之间必须要有空格或换行,即它们之间必须要有间隙,这样编译器才能识别)
41 //当输入字符前有数字输入时需特别小心,因为它可能会将数字输入完成后的回车给读入到接下来要输入的字符中去了,可以在要输入字符前加一条getchar()
42 getchar(); //读取输入字符时的前一个字符(回车),不然输入的字符就是回车而不是本该要输入的字符
43 scanf("%c", &c);
44 scanf("%lld", &y);
45 if(c == '+')
46 sum += y;
47 else if(c == '-')
48 {
49 if(sum >= y)
50 sum -= y;
51 else
52 pos += 1;
53 }
54 }
55 printf("%lld %d
", sum, pos);
56 }
57
58 return 0;
59 }