PAT B 1008 array element loop right shift problem (C + + edition)

Source: Internet
Author: User

1008. Array element Loop right shift problem (20) time limit MS Memory limit 65536 KB code length limit 8000 B award Program Standard

An array A contains N (n>0) integers, and each integer loop is shifted to the right by the M (m>=0) position, and the data from a (a0a1 ...) is removed by the use of 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 the number of times a program moves data, how do you design a moving 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 21 2 3 4 5 6
Sample output:
5 6 1 2 3 4


Idea: The simplest way to do this is to use the chain list, you can try, O (1) Time to complete, this article uses a three-time inverse algorithm to achieve, we pay attention to the inverse of the interval, the idea is still relatively clear, the left interval is reversed, and then the right interval is reversed, then the whole range is reversed, you can get

Note the point:
1. Consider the case where m is greater than n
2. Selection of the inverse interval

1 //1008_1.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <list>7#include <algorithm>8 9 using namespacestd;Ten  One intMain () A { -     intN, m,t; -  theCIN >> N >>M; -  -     int*p =New int[N]; -  +      for(inti =0; i < N; i++) -CIN >>P[i]; +  AM = m%n;//prevents the presence of m greater than N, resulting in an error in address access at  -     //algorithm Key -Reverse (p, p +n-M); -Reverse (p + n-m, p +N); -Reverse (p, p +N); -  in      for(inti =0; i < N; i++) -     { to         if(i = =0) +cout << p[0]; -         Else thecout <<" "<<P[i]; *     } $ Panax Notoginseng     Delete[] p; -  the     return 0; +}

PAT B 1008 array element loop right shift problem (C + + edition)

Related Article

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.