组合题5 个小题
已作答 0 / 5
(双栈模拟数组)只使用两个栈结构 stack1 和 stack2,模拟对数组的随机读取。作为栈结构,stack1 和 stack2 只能访问栈顶(最后一个有效元素)。栈顶指针 top1 和 top2 均指向栈顶元素的下一个位置。
输入第一行包含的两个整数,分别是数组长度 n 和访问次数 m,中间用单个空格隔开。
第二行包含 n 个整数,一次给出数组各项(数组下标从 0 到 a−1)。第三行包含 m 个整数,需要访问的数组下标。对于每次访问,输出对应的数组元素。
1#include <stdio.h>2const int SIZE = 100;3int stack1[SIZE], stack2[SIZE];4int top1, top2;5int n, m, i, j;6void clearStack()7{8 int i;9 for ( i = top1; i < SIZE; i++ )10 stack1[i] = 0;11 for ( i = top2; i < SIZE; i++ )12 stack2[i] = 0;13}14 15int main()16{17 scanf( "%d,%d", &n, &m );18 19 for ( i = 0; i < n; i++ )20 scanf( "%d", &stack1[i] );21 top1 = ______ (1)______;22 top2 = ______ (2)______;23 for ( j = 0; j < m; j++ )24 {25 scanf( "%d", &i );26 while ( i < top1 - 1 )27 {28 top1--;29 ______( 3 ) ______;30 top2++;31 }32 while ( i > top1 - 1 )33 {34 top2--;35 ______( 4 ) ______;36 top1++;37 }38 clearstack();39 printf( "%d\n", stack1[______( 5 ) ______] );40 }41 return(0);42}第 1 空应填( )