万卷网 > 题目详情
题型:简答题

(新壳栈)小 Z 设计了一种新的数据结构“新壳栈”。首先,它和传统的栈一样支持压入、弹出操作。此外,其栈顶的前 c 个元素是它的壳,支持翻转操作。其中,c > 2 是一个固定的正整数,表示壳的厚度。小 Z 还希望,每次操作,无论是压入、弹出还是翻转,都仅用与 c 无关的常数时间完成。聪明的你能帮助她编程实现“新壳栈”吗?

程序期望的实现效果如以下两表所示。其中,输入的第一行是正整数 c,之后每行输入都是一条指令。另外,如遇弹出操作时栈为空,或翻转操作时栈中元素不足 c 个,应当输出相应的错误信息。

#include <iostream> 

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)

         ①        ;

else

return ((k + c - 2) % c) + 1;

}

void push()

{

int element;

cin>>element;

if (next(head) == tail) { 

n++;

                          ②        ;

tail = next(tail);

}

if (empty)

empty = false; 

else

head = next(head);

       ③        = element;

}

void pop()

{

if (empty) {

cout<<"Error: the stack is empty!"<<endl; 

return;

}

cout<<      ④       <<endl; 

if (tail == head)

empty = true; 

else {

head = previous(head); 

if (n > 0) {

tail = previous(tail);

                 ⑤            = s[n];

n--;

}

}

}

void reverse()

{

int temp;

if (      ⑥      == tail) { 

direction = !direction; 

temp = head;

head = tail; 

tail = temp;

}

else

cout<<"Error: less than "<<c<<" elements in the stack!"<<endl;

}

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;

}

更新时间:2023-08-28 10:37:12 |
【知识点】 信息学NOIP提高组

相似题推荐

填空题

如下图所示,A到B是连通的。假设删除一条细的边的代价是1,删除一条粗的边的代价是2,要让A、B不连通,最小代价是_____(2分),最小代价的不同方案数是_______(3分)。(只要有一条删除的边不同,就是不同的方案)

2023-09-02
单选题

中国计算机学会于( )年创办全国青少年计算机程序设计竞赛。(2018年提高组)

A.

1983

B.

1984

C.

1985

D.

1986

2023-09-02
填空题

如右图所示,共有13个格子。对任何一个格子进行一次操作,会使得它自己以及与它上下左右相邻的格子中的数字改变(由1变0,或由0变1)。现在要使得所有的格子中的数字都变为0,至少需要      次操作。

2023-09-02
简答题

(最长路径)给定一个有向无环图,每条边长度为1,求图中的最长路径长度。(第五空2分,其余3分)

输入:第一行是结点数n(不超过100)和边数m,接下来m行,每行两个整数a,b,表示从结点a到结点b有一条有向边。结点标号从0到(n-1)。

输出:最长路径长度。

提示:先进行拓扑排序,然后按照拓扑序计算最长路径。


#include <iostream>

using namespace std;


int n, m, i, j, a, b, head, tail, ans;

int graph[100][100]; // 用邻接矩阵存储图

int degree[100]; // 记录每个结点的入度

int len[100]; // 记录以各结点为终点的最长路径长度

int queue[100]; // 存放拓扑排序结果


int main() {

cin >> n >> m;

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

graph[i][j] = 0;

for (i = 0; i < n; i++)

degree[i] = 0;

for (i = 0; i < m; i++) {

cin >> a >> b;

graph[a][b] = 1;

                 (1)           ;

}

tail = 0;

for (i = 0; i < n; i++)

if (      (2)      ) {

queue[tail] = i;

tail++;

}

head = 0;

while (tail < n - 1) {

for (i = 0; i < n; i++)

if (graph[queue[head] ][i] == 1) {

                               (3)        ;

if (degree[i] == 0) {

queue[tail] = i;

tail++;

}

}

                  (4)        ;

}

ans = 0;

for (i = 0; i < n; i++) {

a = queue[i];

len[a] = 1;

for (j = 0; j < n; j++)

if (graph[j][a] == 1 && len[j] + 1 > len[a])

len[a] = len[j] + 1;

if (      (5)      )

ans = len[a];

}

cout << ans << endl;

return 0;

}

2023-09-02
单选题

设A和B是两个长为n的有序数组,现在需要将A和B合并成一个排好序的数组,请问任何以元素比较作为基本运算的归并算法最坏情况下至少要做( )次比较。

A.

n2

B.

n logn

C.

2n

D.

2n-1

2023-09-02
公众号
客服 反馈
顶部