皮皮学,免费搜题
登录
logo - 刷刷题
搜题
【简答题】
(新壳栈) 小 Z 设计了一种新的数据结构“新壳栈”。首先,它和传统的栈一样支持 压入、弹出操作。此外,其栈顶的前 c 个元素是它的壳,支持翻转操作。其中, c > 2 是一个固定的正整数,表示壳的厚度。小 Z 还希望,每次操作,无论是压入、弹出还 是翻转,用与 c 无关的常数时间完成。聪明的你能帮助她编程实现“新壳栈”吗? 程序期望的实现效果如以下两表所示。其中,输入的第一行是正整数 c ,之后每行输入都是一条指令。另外,如遇弹出操作时栈为空,或翻转操作时栈中元素不足 c 个, 应当输出相应的错误信息。 指令 涵义 1[ 空格 ]e 在栈顶压入元素 e 2 弹出(并输出)栈顶元素 3 翻转栈顶的前 c 个元素 0 退出 表 1 :指令的涵义 输入 输出 栈中的元素 ( 栈底,右为栈顶 ) 说明 3 输入正整数 c 1 1 1 压入元素 1 1 2 1 2 压入元素 2 1 3 1 2 3 压入元素 3 1 4 1 2 3 4 压入元素 4 3 1 4 3 2 翻转栈顶的前 c 个元素 1 5 1 4 3 2 5 压入元素 5 3 1 4 5 2 3 翻转栈顶的前 c 个元素 2 3 1 4 5 2 弹出栈顶元素 3 2 2 1 4 5 弹出栈顶元素 2 2 5 1 4 弹出栈顶元素 5 3 错误信息 1 4 由于栈中元素不足 c 个,无法翻转,故操 作失败,输出错误信息 2 4 1 弹出栈顶元素 4 2 1 空 弹出栈顶元素 1 2 错误信息 空 由于栈为空,无法弹出栈顶元素,故操作失败,输出错误信息 0 空 退出 表 2 :输入输出样例 #include using namespace std; const int NSIZE = 100000, CSIZE = 1000; int n, c, r, tail, head, s[NSIZE], q[CSIZE]; // 数组 s 模拟一个栈, n 为栈的元素个数 // 数组 q 模拟一个循环队列, tail 为队尾的下标, head 为队头的下标 bool direction, empty; int previous(int k) { if (direction) return ((k + c - 2) % c) + 1; else return (k % c) + 1; } int next(int k) { if (direction) 1 ; else return ((k + c - 2) % c) + 1; } void push() { int element; cin>>element; if (next(head) == tail) { n++; 2 ; tail = next(tail); } if (empty) empty = false; else head = next(head); 3 = element; } void pop() { if (empty) { cout< } cout<< 4 < if (tail == head) empty = true; else { head = previous(head); if (n > 0) { tail = previous(tail); 5 = s[n]; n--; } } } void reverse() { int temp; if ( 6 == tail) { direction = !direction; temp = head; head = tail; tail = temp; } else cout< } int main() { cin>>c; n = 0; tail = 1; head = 1; empty = true; direction = true; do { cin>>r; switch (r) { case 1: push(); break; case 2: pop(); break; case 3: reverse(); break; } } while (r != 0); return 0; }
手机使用
分享
复制链接
新浪微博
分享QQ
微信扫一扫
微信内点击右上角“…”即可分享
反馈
参考答案:
举一反三
【简答题】Complete the text with the best words. The way of the wiki In the Hawaiianlanguage, wiki means ‘quick’. Wikipedia was _1_ by Jimmy Wales and Larry Sanger as a free online encyclopedia written by anybo...
【单选题】That the author mention the old roman is
A.
To compare the old Romans with today' s people.
B.
to give an example.
C.
to show human beings in the past know nothing better.
D.
to indicate human beings are used to bloodthirsty.
【简答题】阅读理解。 It was Jimmy's birthday and he was five years old. He got a lot of nice birthday presents from his family, and one of them was a beautiful drum. 'Who gave him that thing? 'Jimmy's father said wh...
【单选题】Which of the following is Not mention as a reason to enable women to original texts?
A.
Female communities provided some counterweight to patriarchy.
B.
Queen Anne's political activities.
C.
Most women had a good education.
D.
Queen Elizabeth's political activities.
【简答题】I cut one of my ________ (finger) when I was cooking. 2 . “You are old enough and you should learn to be ________(dependent) , dear , ”my mother said to me. 3. Would you mind _____(take) out the trash...
【单选题】如果制造费用在产品成本中占较大比重,比较适宜采用的成本计算法是(    )
A.
变动成本法
B.
责任成本法
C.
完全成本法
D.
作业成本法
【单选题】如果制造费用在产品成本中占较大比重,比较适宜采用的成本计算法是( )
A.
作业成本计算法
B.
变动成本计算法
C.
责任成本计算法
D.
完全成本计算法
【简答题】It was Jimmy's birthday, and he was five years old. He got quite a lot of nice birthday presents from his family, and one of them was a beautiful big drum(鼓). 'Who gave him that thing?' Jimmy's father...
【单选题】What is the relation of memory to glucose tolerance, as is indicated by a research mentioned in paragraph 4 ?
A.
People with poor memory have high glucose tolerance.
B.
People with good memory have low glucose tolerance.
C.
Memory level has nothing to do with glucose tolerance.
D.
The poorer the memory, the poorer glucose tolerance.
【简答题】There is one point which is worth ( ) (mention).
相关题目:
参考解析:
知识点:
题目纠错 0
发布
创建自己的小题库 - 刷刷题