8954. Print array 2

 

Given an array of n integers, print its elements in a single line in reverse order.

 

Input. The first line contains the integer n (1 ≤ n ≤ 100). The next n lines contain the elements of the array, one number per line. The absolute value of each element does not exceed 100.

 

Output. Print the elements of the array in a single line in reverse order.

 

Sample input

Sample output

7

0

4

7

-4

0

3

-2

-2 3 0 -4 7 4 0

 

 

SOLUTION

array

 

Algorithm analysis

Read the input sequence into an array. Then, print the elements of the array in reverse order.

 

Algorithm implementation

Declare an array m to store the input sequence.

 

int m[101];

 

Read the input data.

 

scanf("%d", &n);

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

  scanf("%d", &m[i]);

 

Print the elements of the array in a single line in reverse order.

 

for (i = n - 1; i >= 0; i--)

  printf("%d ", m[i]);

printf("\n");

 

Algorithm implementation – STL

Declare a vector v to store the input sequence.

 

vector<int> v;

 

Read the input data.

 

scanf("%d", &n);

v.resize(n);

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

  scanf("%d", &v[i]);

 

Invert the order of the elements in the array.

 

reverse(v.begin(), v.end());

 

Print the elements of the array in a single line in reverse order.

 

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

  printf("%d ", v[i]);

printf("\n");

 

Python implementation

Read the input data.

 

n = int(input())

v = [int(input()) for _ in range(n)]

 

Invert the order of the elements in the list.

 

v.reverse()

 

Print the elements of the array in a single line in reverse order.

 

print(*v)