← 查看本卷题目
组合题5 个小题
已作答 0 / 5

(双栈模拟数组)只使用两个栈结构 stack1\mathrm{stack1} 和 stack2\mathrm{stack2},模拟对数组的随机读取。作为栈结构,stack1\mathrm{stack1} 和 stack2\mathrm{stack2} 只能访问栈顶(最后一个有效元素)。栈顶指针 top1\mathrm{top1} 和 top2\mathrm{top2} 均指向栈顶元素的下一个位置。

输入第一行包含的两个整数,分别是数组长度 nn 和访问次数 mm,中间用单个空格隔开。

第二行包含 nn 个整数,一次给出数组各项(数组下标从 00 到 a−1a-1)。第三行包含 mm 个整数,需要访问的数组下标。对于每次访问,输出对应的数组元素。

#include <stdio.h>const int	SIZE = 100;int		stack1[SIZE], stack2[SIZE];int		top1, top2;int		n, m, i, j;void clearStack(){	int i;	for ( i = top1; i < SIZE; i++ )		stack1[i] = 0;	for ( i = top2; i < SIZE; i++ )		stack2[i] = 0;} int main(){	scanf( "%d,%d", &n, &m ); 	for ( i = 0; i < n; i++ )		scanf( "%d", &stack1[i] );	top1	= ______ (1)______;	top2	= ______ (2)______;	for ( j = 0; j < m; j++ )	{		scanf( "%d", &i );		while ( i < top1 - 1 )		{			top1--;			______( 3 ) ______;			top2++;		}		while ( i > top1 - 1 )		{			top2--;			______( 4 ) ______;			top1++;		}		clearstack();		printf( "%d\n", stack1[______( 5 ) ______] );	}	return(0);}
第 31 题填空题

第 5 空应填( )

填写答案