"Algorithm Notes" Section 2.5-C/C++ Quick Start -> Array Exercise B: Exercise 6-5: Inversion of Array Elements

Exercise B: Exercise 6-5: Reverse Array Elements

Title description
Restore the values ​​in an integer array of length 10 in reverse order.
For example: the original order is 1,2,3,4,5,6,7,8,9,0, the requirement is changed to 0,9,8,7,6,5,4,3,2,1
input
from Enter 10 integers separated by spaces on the keyboard.
Output
The 10 numbers are output in reverse order, each number occupies one line.
Sample input

1 2 3 4 5 6 7 8 9 0

Sample output

0
9
8
7
6
5
4
3
2
1
#include <stdio.h>


int main(void) {
    
    
    int a[10];
    for (int i = 0; i < 10; ++i) {
    
    
        scanf("%d", &a[i]);
    }
    for (int j = 9; j >= 0; --j) {
    
    
        printf("%d\n", a[j]);
    }
    return 0;
}
#include <stdio.h>


int main(void) {
    
    
    int a[10];
    int temp;
    for (int i = 0; i < 10; ++i) {
    
    
        scanf("%d", &a[i]);
    }
    for (int j = 0; j < 5; ++j) {
    
    
        temp = a[j];
        a[j] = a[9 - j];
        a[9 - j] = temp;
    }
    for (int i = 0; i < 10; ++i) {
    
    
        printf("%d\n", a[i]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109880960