Monk's Love for Food
 

Our monk loves food. Hence,he took up position of a manager at Sagar,a restaurant that serves people with delicious food packages. It is a very famous place and people are always queuing up to have one of those packages. Each package has a cost associated with it. The packages are kept as a pile. The job of a manager is very difficult. He needs to handle two types of queries:

1) Customer Query:
When a customer demands a package, the food package on the top of the pile is given and the customer is charged according to the cost of the package. This reduces the height of the pile by 1. 
In case the pile is empty, the customer goes away empty-handed.

2) Chef Query:
The chef prepares a food package and adds it on top of the pile. And reports the cost of the package to the Manager.
Help him manage the process.

Input:
First line contains an integer Q, the number of queries. Q lines follow.
A Type-1 ( Customer) Query, is indicated by a single integer 1 in the line.
A Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) .

Output:
For each Type-1 Query, output the price that customer has to pay i.e. cost of the package given to the customer in a new line. If the pile is empty, print "No Food" (without the quotes).

Constraints:
1 ≤ Q ≤ 105
1 ≤ C ≤ 107

用到了slice的两个基本方法  stack = stack[0:stackLength-1] 就是将最后的一个元素删除,slice中下标用的其实是相当[0,stackLength-1} 就是从前标开始,包括前标到后标,但是不包括后标,下来就是往slice中添加元素。

package main

import "fmt"

var stack []int

func main() {
var inputCount int
fmt.Scanln(&inputCount) stack = make([]int,0,0) var flag,value int
var stackLength int
for i:=0;i<inputCount;i++{
fmt.Scanln(&flag,&value)
stackLength = len(stack)
if(flag ==1){
if(stackLength == 0){
fmt.Println("No Food")
}else{
fmt.Println(stack[stackLength-1])
stack = stack[0:stackLength-1]
}
}else{
stack = append(stack,value)
}
}
}

  

 

golang 栈操作的更多相关文章

  1. 一文教你搞懂 Go 中栈操作

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/513 本文使用的go的源码15.7 知识点 LInux 进程在内存布 ...

  2. Python模拟入栈出栈操作

    目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [ ...

  3. 第一回写的用arraylist模拟栈操作

    package hashMap; import java.util.ArrayList; import d.Student; /** * 用ArrayList模拟栈操作 * @author zhuji ...

  4. c语言学习,模拟栈操作

    1.stack.c模拟栈操作函数的实现 #include<stdio.h> #include<stdlib.h> ; static char *stack;//数据栈 ;//栈 ...

  5. JavaScript中的栈及通过栈操作的实例

    <script> /*栈操作*/ function Stack() { this.dataStore = []; this.top = 0; this.push = push; this. ...

  6. php实现栈操作(不用push pop 库函数)

    直接上代码 <?php /*php不用库函数实现栈操作 * @author Geyaru 2019-04-20 */ class stack{ private $top = -1; //栈指针初 ...

  7. Lua和C++交互 学习记录之二:栈操作

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  8. 如何仅用递归函数和栈操作逆序一个栈——你要先用stack实现,再去改成递归——需要对递归理解很深刻才能写出来

    /** * 如何仅用递归函数和栈操作逆序一个栈 * 题目: * 一个栈依次压入1,2,3,4,5,那么从栈顶到栈底分别为5,4,3,2,1. * 将这个栈转置后,从栈顶到栈底为1,2,3,4,5,也就 ...

  9. lua和C++交互的lua栈操作——以LuaTinker为例

    一. -- C++类注册函数(LuaTinker) 的lua栈操作: -- lua栈内容(执行到pop语句) 栈地址 <--执行语句 space_name[name] = t1 -- (2b8) ...

随机推荐

  1. C# DropDownList 绑定枚举类

    第一种 DropDownList_Franchiser_Type.DataSource = ListTypeForEnum(); DropDownList_Franchiser_Type.DataVa ...

  2. Golang学习--TOML配置处理

    上一篇文章中我们学会了使用包管理工具,这样我们就可以很方便的使用包管理工具来管理我们依赖的包. 配置工具的选择 但我们又遇到了一个问题,一个项目通常是有很多配置的,比如PHP的php.ini文件.Ng ...

  3. 【hdu5419】Victor and Toys

    求求求 搞搞搞 搞法例如以下:考虑每一个数w[i]w[i]对答案的贡献,呃. . .首先答案一定是 ∑[...](m3) \sum [...]\over {m\choose 3}的形式,仅仅须要搞分子 ...

  4. 剑指 offer代码解析——面试题39推断平衡二叉树

    题目:输入一颗二叉树的根结点.推断该树是不是平衡二叉树. 假设某二叉树中随意结点的左右子树的高度相差不超过1,那么它就是一棵平衡二叉树. 分析:所谓平衡二叉树就是要确保每一个结点的左子树与右子树的高度 ...

  5. macox下编译snappy静态库

    源代码地址:https://github.com/google/snappy 下载 git clone https://github.com/google/snappy 编译 进入snappy源代码文 ...

  6. MySQL远程訪问的两个问题

    安装MySQL后,不能远程訪问.进行例如以下配置就可以 连接后,依旧不能连接,报例如以下错误: 1042 - Can't get hostname for your address 解决方法在my.i ...

  7. 【Android使用Shape绘制虚线,在4.0以上的手机显示实线】解决方式

    问题描写叙述: 用下面代码绘制虚线: <span style="font-family:Comic Sans MS;font-size:18px;"><? xml ...

  8. 【Sqlserver系列】CAST和CONVERT

    1   概述 本篇文章主要讲解SqlServer中类型转换涉及的两个函数:CAST和CONVERT. 2   具体内容 2.1  CAST (1)作用:将一种数据类型的表达式转换为另一种数据类型的表达 ...

  9. F04 我的投资策略

    我的投资理念:价值投资和右侧趋势投资.我的目标年化收益率: 15-25%我的投资时间:3-5年我的投资品种:股票 + EFT基金 买卖时间点的纪律(买入,卖出的时间原则)股票MA20为界限,高于则持有 ...

  10. Java加密与解密笔记(二) 对称加密

    前面的仅仅是做了编码或者摘要,下面看看真正的加密技术. DES public class DESUtil { static final String ALGORITHM = "DES&quo ...