-uva10935 question: trowing cards away1 solution and simple analysis

Source: Internet
Author: User

trowing cards away

Original question test instructions:

There are n cards, starting from the first card (the card at the top head), numbered 1 to n from the top down. When there are at least two cards left, do the following: Throw the first card away and place the new first card at the end of the stack (into tail). Lose as each row contains an n, and the output is discarded each time the card and the last remaining cards. Requirements: n<=50

test Instructions Analysis and Procedures :

Define an array of at least 2n-1 size, and make the array space a little larger to avoid errors. Set a variable head and tail subscript records the first and last data of the input array, each time moving it backward one position to the next array element.

The case output is as follows:

Intput:

7

Output

Discarded cards:1,3,5,7,4,2

Remaining Cards:6

The program code is as follows:

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 inta[ +];6 7 intMain ()8 {9     intN;Ten      while(Cin >> N &&N) One    { A          for(inti =0; I <= N; ++i) -A[i] =i; -  the         intHead =1, tail = N, flag =1; -printf"Discarded cards:"); -          while(Head <tail) -         { +             if(!flag) -printf","); +printf"%d", A[head + +]); Aa[++ Tail] = A[head + +]; atFlag =0; -         } -printf"\nremaining card:%d\n", A[head]); -     } -     return 0; -}

-uva10935 title: trowing cards Away1 solution and simple analysis

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.