1477. The Weird Shuffle

Source: Internet
Author: User

Title Description:

For a deck of poker, we have a variety of different ways to shuffle. One way is to split it in half from somewhere in the middle, and then swap it, which we call shift (SHIFT). For example, the original order is 123456, from the 4th position Exchange, the result is 561234. This method is actually the loop shift of the array, in order to do this several times, it must be implemented in a way that is as fast as possible. In this topic, there is another way to shuffle, that is, the first half (if the total number is odd, that is (n-1)/2) card flip over, this operation is called Flip (flip). Flip on the result of the previous shift operation, and the result is 165234. Of course, if the actual poker, direct flipping will cause the positive and negative mix together, we do not care so much.
Given n cards, the initial order is from 1 to N, after a number of shift and flip operations, what will the result be?

Input:

The input includes multiple sets of test data, and the first row of each group of data consists of two numbers n and K. n represents the number of cards, 1<n<1000, if n is 0 indicates the end of the input, K indicates the number of operations to be performed below. The subsequent K-line, an integer x,1<=x<=n per line, represents the beginning of the shift from the first few positions. After each shift operation, a flip operation is followed.

Output:

For each set of data entered, the values of each position are computed after the given K-times shift and flip operations. and sequentially prints the values of all cards on a single line, followed by a space after each value (including the last one).

Sample input:
6 140 0
Sample output:
1 6 5 2 3 4

#include <iostream>using namespacestd;inta[1002];voidinit () { for(intI=0;i<1002; i++) {A[i]=i; }}voidReverseintLeftintRight ) {    inttemp;  for(intI=0;i< (right-left+1)/2; i++) {Temp=a[left+i]; A[left+i]=a[right-i]; A[right-i]=temp; }}voidShiftintNintx) {Reverse (1, x); Reverse (x+1, N); Reverse (1, n);}voidFlipintN) {    if(n%2==0) Reverse (1, n/2); ElseReverse1, (n1)/2);}intMain () {intn,k,p,i,j;  while(cin>>n>>k)        {init (); if(n==0) Break;  for(j=0; j<k;j++) {cin>>p;            Shift (N,P);        Flip (n);  for(i=1; i<=n;i++) {cout<<a[i]<<" "; } cout<<Endl; }}    return 0;}

1477. The Weird Shuffle

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.