"Guessing algorithm" magician's Game

Source: Internet
Author: User

Description

The magician hand has m card, the card number is 1-n, the magician pre-cards lined up and folded together, face down. Said to the audience: I do not look at the card, only a few can guess what each card is, I loudly count, you listen. Don't believe me? You just watch.

The magician in order from the top to the next number of cards, the first number 1, the first card (the top of the card) turned over to the desktop, just the card 1. The second number 1, 2, the first card placed under the fold, the second card turned over to the desktop, just the card 2. The third number of 1, 2, 3, the front two is placed in turn under the fold, and then turn the third card to the desktop, just the card 3. This is done in turn, the n cards are all turned over, accurate. How do you arrange the original order of the cards in the magician's hands?

Input

Enter the first behavior of an integer T (1<=t<=10), indicating that there is a T group of test data;

For each set of test data formats are as follows: First behavior 1 integer n (1<=n<=26). Output

Output a single line for each test sample the original order of the Magician's hands, followed by a space after each brand.

Sample Input
Sample Output


Problem Solving Ideas:
The array num is used to store the card, and index indicates the current subscript.
1. Initialization, inside the tablet is empty, 0;
2. The first position is placed 1, then the next number of n vacant (that is, non-empty) to put the card N, to reach the end of the array to jump back to subscript 0 continue loop

Implementation code:
#include <iostream>using std::cin;using std::cout;using std::endl;int main () {int num[26];    int T, n, total, index;  Cin >> T; T test example for (; T > 0;        t--) {cin >> n;           for (int i = 0; i <; i++) num[i] = 0;                Initialize empty num[0] = 1;        The first one definitely puts 1 index = 1; for (int i = 2, I <= N; i++) {for (total = 0; total < i;)               {//total is used to indicate the current number of empty tablets if (num[index] = = 0) total++;               if (total = = i) break;               if (index = = n-1)//reach the end of the array jump back to index = 1;            else index++;           } Num[index] = i;        Put card} for (int i = 0; i < n; i++) {cout << num[i] << ";    } cout << Endl;                                                                                                                                            }                        }          

(Ben Boven more or less reference other online materials, but the time has long forgotten the original reference, here to thank them! )

"Guessing algorithm" magician's Game

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.