Huawei OD Computer Test Real Questions - Calculating the Remaining Numbers in the Stack - 2023 OD Unified Examination (Paper B)

Topic description:

Store positive integers into an empty stack in sequence. Assume that the elements n (1<=n<=2^31-1) pushed onto the stack are nx...n4, n3, n2, n1 in order. Whenever an element is pushed onto the stack When, if n1=n2+...+ny (range of y [2,x], 1<=x<=1000), then all elements n1~ny are popped off the stack, and new elements m (m=2* n1).

For example: store 6, 1, 2, 3 into the stack in sequence. When 6, 1, 2 is stored, the order from the bottom to the top of the stack is [6, 1, 2]; when 3 is stored, 3=2+ 1, 3, 2, and 1 are all popped off the stack, and element 6 (6=2*3) is pushed back onto the stack. At this time, there is element 6 in the stack; because 6=6, all two 6s are popped off the stack, and 12 is stored in it. Finally, There is only one element left on the stack, 12.

Enter description:

Use a string of positive integers separated by a single space, such as "5 6 7 8". The number on the left is pushed onto the stack first, and the number of positive integers entered is x, 1<=x<=1000.

Output description:

The element values ​​stored in the final stack are separated by spaces, such as "8 7 6 5", and the number on the top of the stack is on the left.

Additional instructions:

Example 1

enter:

5 10 20 50 85 1

Output:

1 170

illustrate:

5+10+20+50=85. When 85 is entered, 5, 10, 20, 50, and 85 are all popped off the stack, and 170 is pushed onto the stack. Finally, the numbers popped off the stack are 1 and 170. <

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/132778713