题目链接:http://codeforces.com/problemset/problem/1159/A

Codeforces 1159A A pile of stones

Codeforces 1159A A pile of stones


题意:初始石头堆石子数未知,求进行 n 次加减操作后,石头堆石子数最小的可能是多少。

思路:正常模拟就好了,题意理解了老半天orz! 开始数量未知,所以模拟的答案是负数的话,最小可能就是0。

 AC代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     char a;
 9     cin >> n ;
10     int ans = 0;
11     for(int i = 0;i < n;i++)
12     {
13         cin >> a;
14         if(a == '+') ans++;
15         else if(ans > 0)ans--;
16     }
17     cout << max(ans,0) << endl;
18     return 0;
19 }

 

相关文章: