Pat Serie 1008. Array elements Loop right shift problem (20)

Source: Internet
Author: User

1008. Loop right shift of array elements (20)
An array A contains N (n>0) integers, and each integer loop is shifted to the right by M (m>=0), and the data from a (A0 A1 ...) is not allowed to use a different array. AN-1) transform to (an-m ... AN-1 A0 A1 ... AN-M-1) (the last m-number loop moves to the first m-position). If you need to consider how often the program moves data, how to design the mobile
The method?
Input format: Each input contains a test case, line 1th input n (1<=n<=100), M (m>=0), and line 2nd enter n integers,
separated by a space.
Output format: The sequence of integers after the output loop is shifted to the right of the M-bit in a row, separated by a space, with no extra spaces at the end of the sequence.
Input Sample:
6 2
1 2 3 4 5 6
Sample output:
5 6 1 2 3 4

Analysis:Find the number that corresponds to the translation, subscript 0, and subscript N-1, and when the bounds are found, the number in the middle of the output
#include <iostream>#include<stdlib.h>using namespacestd;inta[ -];intMain () {intN, M; CIN>> N >>M; intFlag =0;  for(inti =0; i < N; i++) Cin>>A[i];  for(inti =0; ; i++)    {        if((i + M)% N = =0) Flag=1; if(Flag = =1)        {            if((i + M)% n = = N-1) {cout<< a[i% N] <<Endl;  Break; }            Elsecout<< a[i% N] <<" "; }} system ("Pause"); return 0;}


Pat Serie 1008. Array elements Loop right shift problem (20)

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: 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.