11111 - Generalized Matrioshkas
2014-01-15 20:38
183 查看
Problem B - Generalized Matrioshkas |
Input
The input file contains several test cases, each one of them in a separate line. Each test case is a sequence of non zero integers, each one with an absolute value less than 107.Output
Output texts for each input case are presented in the same order that input is read.For each test case the answer must be a line of the form:-) Matrioshka!if the design describes a generalized matrioshka. In other case, the answer should be of the form:-( Try again.Sample Input
-9 -7 -2 2 -3 -2 -1 1 2 3 7 9 -9 -7 -2 2 -3 -1 -2 2 1 3 7 9 -9 -7 -2 2 -3 -1 -2 3 2 1 7 9 -100 -50 -6 6 50 100 -100 -50 -6 6 45 100 -10 -5 -2 2 5 -4 -3 3 4 10 -9 -5 -2 2 5 -4 -3 3 4 9
Sample Output
:-) Matrioshka! :-( Try again. :-( Try again. :-) Matrioshka! :-( Try again. :-) Matrioshka! :-( Try again.
方法:
和括号匹配问题差不多。
基本想法是:
1.依次读取数字(方便起见设为x), 如果x < 0,则入栈,否则(即x > 0)转到2
2.若栈顶元素大于0,则出栈并计算出栈元素的和(方便起见设为s),直到栈顶元素小于0。
3.若s > x 或 栈空 或 栈顶元素不是x 的相反数, 则返回0,退出。
4.出栈栈顶元素(x的相反数),入栈 s。
5.若还有元素可读,转到 1.
6.若栈中还剩一个大于0的元素(即最后入栈的s),返回 1,否则返回 0.
相关文章推荐
- ZOJ 1889 n的倍数使11111…位数最少
- 11111
- UVa 11111 - Generalized Matrioshkas
- uva 11111(线性表)
- 11111
- UVA 11111-Generalized Matrioshkas
- 11111
- 11111
- UVa 11111 - Generalized Matrioshkas
- uva 11111
- UVA11111 Generalized Matrioshkas
- UVA 11111 Generalized Matrioshkas
- UVA-11111 Generalized Matrioshkas
- 11111啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
- 11111
- UVA 11111 一般XX
- UVA 11111 Generalized Matrioshkas
- Uva 11111 - Generalized Matrioshkas
- UVA11111套娃匹配题,注意字符串流使用简化代码
- uva 11111