Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Input 10 integer numbers in the array question, store them in reverse order and then output them
Input 10 integer numbers in the array question, store them in reverse order and then output them

This program is a classic program in Tan Haoqiang’s book:

for (i=0;i

(1)

p>

About N/2:

It cycles from zero until it reaches a certain number, for example, N=15, it is 0--7;

If N=16, then 0--8

Why?

Because odd numbers are symmetrical about the middle: for example, 15, there are 8 in the middle, and those on both sides are: 1--7 and

9--15, there are 7 on both sides, and one in the middle< /p>

So the middle one does not need to be cycled, that is, the middle 8 does not need to be exchanged, the two sides are exchanged

And the even number 16, 1--8 and 9--16 are all 8, they can be exchanged directly Just

{

(2)

t=a[i];

Assign the i-th to t

a[i]=a[N-1-i];

Assign the symmetrical value to the i-th one

a[N-1 -i]=t;

Then t (that is, the i-th assignment just now is symmetrical with i)

The swap is completed

}

(3)

About N-1-i

For example 15: Because the first one is 0 (i=0, so ai=a0), so with 0 The symmetrical one is 14 (because it is 15 numbers, so it is numbered 0----14), that is, a0----a14,

a[1]----a[13], a[2]---a[12],………………

So it is

0-----15-1-0

< p>1-----15-1-1

2-----15-1-2

………………

That is, i and N-1-i are swapped