当前位置:网站首页>Inversion problem - key point

Inversion problem - key point

2022-07-31 10:18:00 [email protected]

Given a linear table, how to invert the elements in it?Two integer variables i and j can be set, i points to the first element, j points to the last element, while exchanging the elements pointed to by i and j, and let i and j move toward each other until they meet, the implementation code is as follows.Suppose the elements are stored in the array a[ ], and left and right are the subscripts of the elements at both ends of the array.

for(int i=lefft,j=right;i<j;i++,j--){int temp=a[i];a[i]=a[j];a[j]=temp;}

【Example 2-5】

(1) Move the front k(k

Only need to invert the entire array to satisfy the requirement that the front-end k elements are reversed and placed in the back-end of the array

(2) Move the front k(k

Only need to invert the front-end k elements, and then invert the entire array to satisfy the front-end k elements in the original order and put them in the back end of the array

(3) Move the elements in the array (X0, X1, ..., Xn-1) tobecomes (Xp, Xp+1, ..., Xn-1, X0, X1,...,Xp-1), that is, circularly shifted left by p(0

Only need to set 0~p-1 The element at the position is reversed, and then p~n-1Invert the elements of the position, and then invert the entire array.
原网站

版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207311008455709.html