1008. Loop right shift of array elements

Source: Internet
Author: User

Topic:

Ideas:

The entire array is reversed first, then the first M elements of the array and the subsequent elements are reversed respectively.

Code:
1 /*2 1008. Loop right shift of array elements3 */4 5#include <stdio.h>6#include <string.h>7#include <math.h>8#include <stdlib.h>9#include <time.h>Ten  One #defineMAXN 101 A intP[MAXN] = {0}; -  - //Reverse Position the voidSwapintAintb) { -     inti; -      for(I=a; i<= (a+b)/2; ++i) { -         inttemp =P[i]; +P[i] = p[a+b-i]; -P[a+b-i] =temp; +     } A } at  - intMain () { -     intN, M, I, flag=0; -scanf"%d%d", &n, &M); -M%= N;//M may be greater than N -      for(i=0; i<n; ++i) { inscanf"%d", &p[i]); -     } to     if(M! =0) {//M is 0 no need to move +Swap0, N-1);//invert the entire array -Swap0, M-1);//Inverse array of first M elements theSwap (M, n1);//inverse array of elements behind *     } $      for(i=0; i<n; ++i) {//output by FormatPanax Notoginseng         if(flag) { -printf" "); the         } +printf"%d", P[i]); AFlag =1; the     } +  -     return 0; $}

1008. Loop right shift of array elements

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.