注意输入和输出的结果

-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
-10 -5 -3 3 -1 1 5 -4 4 10

10
-10 10

-10

:-) Matrioshka!
:-( Try again.
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.

#include <iostream>
#include <sstream>
#include<memory.h>
#include<stdio.h>
using namespace std;
const int N = ;
struct stack
{
int a[N];
int index;
stack()
{
index = ;
}
void push(int i)
{
a[index++] = i;
}
int pop()
{
int i = a[--index];
return i;
}
};
int main()
{
int n;
string str;
while (getline(cin, str))
{
istringstream is(str);
int hasInt = ;
stack s;
memset(s.a, , sizeof(s.a));
int error = ;
while (is >> n)
{
hasInt++;
if (n < )
s.push(n);
else
{
int t = ;
while (s.index)
{
int j = s.pop();
if (j > )
t += j;
else
{
if (j != n * -)
error = ;
break;
}
}
if (t < n && !error)
s.push(n);
else
break;
}
}
if (!hasInt)
{
//cout << ":-( Try again." << endl;
continue;
}
if (hasInt == )
{
cout << ":-( Try again." << endl;
continue;
}
if (error || s.index != )
cout << ":-( Try again." << endl;
else if (s.index == )
{
if (s.pop() > )
cout << ":-) Matrioshka!" << endl;
else
cout << ":-( Try again." << endl;
}
}
return ;
}

uva-11111-栈的更多相关文章

  1. UVa 11111 Generalized Matrioshkas

    嵌套玩具, 要求外层玩具的尺寸比内层玩具尺寸的和要大. 每一个玩具由一个负数与相应的正数表示, 在这两数之间的部分即为此玩具内部的玩具. 要求判断一串输出的数字是否能组成一个合法的玩具. 一个合法的玩 ...

  2. UVa 442 (栈) Matrix Chain Multiplication

    题意: 给出一个矩阵表达式,计算总的乘法次数. 分析: 基本的数学知识:一个m×n的矩阵A和n×s的矩阵B,计算AB的乘法次数为m×n×s.只有A的列数和B的行数相等时,两个矩阵才能进行乘法运算. 表 ...

  3. 【紫书】Rails UVA - 514 栈

    题意:判断出栈顺序是否合法 题解:两个指针,A指向入栈序列,B指向出栈. 的分三种情况:if     1.A==B :直接入栈加出栈即可A++,B++ else 2.和栈顶相同,直接出栈A==stac ...

  4. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

  5. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  6. UVa 442 矩阵链乘(栈)

    Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...

  7. UVa 12265 (单调栈) Selling Land

    紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...

  8. UVa 673 Parentheses Balance【栈】

    题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...

  9. UVa 1451 (数形结合 单调栈) Average

    题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...

  10. UVa 673 Parentheses Balance(栈的使用)

     栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description You are ...

随机推荐

  1. (GoRails)ActionCable如何用Redis + 菜鸟redis教程

    视频: https://gorails.com/episodes/how-actioncable-uses-redis?autoplay=1 原理PubSub, 你进入一个频道,然后就能接收,和发布信 ...

  2. 使用Bootstrap的suggest下拉插件

    前端代码 /*html代码*/ <input type="text" class="form-control search_ul" id="ca ...

  3. hdu 5303 DP(离散化,环形)+贪心

    题目无法正常粘贴,地址:http://acm.hdu.edu.cn/showproblem.php?pid=5303 大意是给出一个环形公路,和它的长度,给出若干颗果树的位置以及树上的果子个数. 起点 ...

  4. 牛客网——C列一列

    链接:https://www.nowcoder.net/acm/contest/71/C来源:牛客网 题目描述 小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An.尽管他计算 ...

  5. 『转』Panda Antivirus Pro 2014 – 免费6个月

    Panda Antivirus Pro 2014 为您的计算机提供了最简单的使用和最直观的保护.最近,Panda公司和 softonic公司合作推出免费半年版本活动地址:点此进入点击“Kostenlo ...

  6. javascript 与vbscript 互相调用

    在 VBScript 的function 中可以直接编写 javascript 代码 但是只能返回一个字符串给变量 <html><head> <script type=& ...

  7. 利用python完成多个url状态码的检测

    import re import requests import json from threading import Thread,Lock from concurrent.futures impo ...

  8. 【spring源码学习】Spring @PostConstruct和@PreDestroy实例

    在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...

  9. Hibernate环境搭建+struts整合

    说明:本文档,是和struts2+hibernate的整合示例. lib下还有struts2的jar包,本示例没有显示.struts2的搭建参考struts2的环境搭建 一下载hibernate的ja ...

  10. C#对Jason序列化匿名对象

    引用: using System.Web.Script.Serialization; 代码: var resp = new { flag = false, url = ConfigReader.Log ...