Programming Zhu Ji Nanxiong string inversion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.