Programming Zhu Ji Nanxiong is really a people feel the light of the book, although the book is not thick, but the knowledge is very close to practical application, but also very enlightening, if you can really digest the content inside, the understanding of the program will certainly be on a grade.
This article gives the second chapter of the fifth problem of the approximate procedure!
#include <iostream>using namespace std;void move (int * arr, int arrsize,int n);//Moves the first n elements of the array arr left void show (int * arr,i NT arrsize); void reverse (int * arr, int n, int m); void move_re (int * arr, int arrsize, int n); void move_re_twodif (int * AR R, int arrsize, int n, int m),//Exchange position of the first n elements of the array and the after M elements int main () {int arr[] = {x, n, x, 302, 154, 284};show (arr , 8); cout << endl;move_re (arr,8, 6); show (arr,8); Move_re_twodif (arr, 8, 5, 3); Show (arr, 8); return 0;} void Move (int * arr,int arrsize, int n) {int i = 0;int k = n%arrsize;for (; I <k; ++i) {int temp = arr[i];int m = i+k;whi Le (M < arrsize) {Arr[m-k] = arr[m]; m+= K;} if (m>=arrsize) arr[m-k] = Arr[m%arrsize];show (arr, arrsize);}} void Show (int * Arr,int arrsize) {int n = 0;while (n<arrsize) {cout << * (arr+n) << "\ t"; ++n;} Cout<<endl;} void reverse (int * arr, int n, int m) {if (n >= m) Return;int i = n;int j = m;for (; i < J;) {int temp = arr[i];arr[i++ ] = arr[j];arr[j--] = temp;}} void Move_re (int * arr, int arrsize, int n) {int k = n%arrsize;reverse (arr, 0, k-1); reverse (arr, K, arrsize-1); reverse (arr, 0, Arrsize -1);} void move_re_twodif (int * arr, int arrsize, int n, int m)//Exchange Position {if ((arrsize-m) < n) of the first n elements of the array and the last m elements return;//if the second half of the data and the previous Half-segment data overlaps to stop swapping reverse (arr, 0, n-1); reverse (arr, n, arrsize-m-1); reverse (arr, arrsize-m, arrsize-1); reverse (arr, 0, Arrs IZE-1);}If there is reprint please indicate the source!
Programming Zhu Ji Nanxiong string inversion