nyoj714Card Trick (队列)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_26019265/article/details/51474516

nyoj714



      

Card Trick

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

The magician shuffles a small pack of cards, holds it face down and performs the following procedure:

  1. The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.
  2. Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.
  3. Three cards are moved one at a time…
  4. This goes on until the nth and last card turns out to be the n of Spades.

This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle). Your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 13.

输入
On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 10 Each case consists of one line containing the integer n. 1 ≤ n ≤ 13
输出
For each test case, output a line with the correct permutation of the values 1 to n, space separated. The first number showing the top card of the pack, etc…
样例输入
2
4
5
样例输出
2 1 4 3
3 1 4 5 2
来源
第六届河南省程序设计大赛
上传者
ACM_赵铭浩



   这个题,不好理解。。。

   题意:  抽取上面第一张牌后放到最后,然后现在上面显现的是(梅花)A,然后去掉A,然后再抽取两张放到后面,现在上面的牌显示(梅花) 2 , 然后去掉2  ,依次,,,知道第n次 。。


   倒着推就掉坑里了 ,,  正着来  先显示的一定是1 然后是 2 ,3 , 4,,,n

   那个位置上先放1 ,然后那个位置上再放2 ,,,,,依次类推。。

    因此,队列里初始 值为位置值,正着来推,先轮到哪个位置,那个位置的值就是 显现的牌的值

 
#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
	queue<int> que;
	int n;
	int i,j,t,m,k;
	int distion;
	int c[14];
	scanf("%d" ,&n);
	while(n--)
    {
    	scanf("%d",&m);
    	k = 1;
    	for(i= 1;i<=m;i++)
    	{
    		que.push(i);
		}
		
		for(i = 1;i<=m;i++)
		{
			
	      for(j = 1;j<=i;j++)
		  {
			t = que.front();
			que.pop();
	   	  	que.push(t); 
		  }
		  distion = que.front();
		  c[distion] = k++;
		  que.pop();
     	}
     	
     	for(i = 1 ;i<= m ;i++)
     	if(i == 1)printf("%d",c[i]);
     	else printf(" %d",c[i]);
     	printf("\n");

     }
   	 
   	 
    return 0;

}        







展开阅读全文

Magic Trick

12-02

BackgroundnnnWarning! This problem statement contains a serious spoiler. It shows the trick behind a magic trick. So if you still want to be amazed in case somebody shows this trick to you then do NOT read the rest of this problem statement. Stop reading… NOW!nnnProblemnnnWell, you’re still reading, so obviously you have no respect for magic tricks. Be ashamed, please. Ok,here’s what happens. The magician shows you a text with three paragraphs like this one:nnnIt was a horribly dark night.nnnThe moon was shining, but not much.nnA suspicious stranger entered thennnbar and went straight to John Doe.nnn“I’m searching for aliens, can Innnborrow your computer?”, he said.nnnHe then asks you to secretly pick a word in the first paragraph. Then you shall do this:nnn1. Count the number of characters in your word (call that number X).nnn2. From your word move on X words.nnnRepeat these two steps until you reach the third paragraph. Then tell the magician that you’re done.After some hocus pocus he tells you the word you ended up with.nnnFor our purposes, a “word” is defined as consecutive letters (A-Z,a-z). For example, “I’m” is regarded as two separate words.nnnFor example, let’s say you choose “night” in the above example. It has 5 characters, so you move on five words: “The”, “moon”, “was”, “shining”, “but”. Our new word is “but”. You move on 3 words to “A”,then 1 to “suspicious”, then 10 to “Doe” and then 3 to “searching”. Now you tell the magician that you’re ready. He says that you’ve reached “searching”.nnnHow can he know? Well, it doesn’t matter where you start in the first paragraph, you’ll always end up at “searching”. The magician needs new texts and asks you to help him to find all possible outcomes (in the above example, “searching” is the only one). Apart from words, a possible outcome is “-outside-”,which means it’s possible to jump behind the third paragraph. Also, he’s not interested if more than three outcomes are possible. nn输入:nnnThe first line contains the number of scenarios. For each scenario, three lines are given, representing the three paragraphs. No line is longer than 100000 characters. Every paragraph will contain at least one word. n输出:nnnThe output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print the possible outcomes (possibly including “-outside-”) in alphabetical/lexicographical order, one word per line. Write words in lower case. Don’t list outcomes more than once. If however there are more than three possible outcomes, then print “-too many-” and do *not* print any of them. Terminate the output for the scenario with a blank line.n样例输入:nn4nIt was a horribly dark night. The moon was shining, but not much.nA suspicious stranger entered the bar and went straight to John Doe.n"I'm searching for aliens, can I borrow your computer?", he said.n!pablo espanol!n!pablo espanol!n!pablo espanol!nc'mon howLongOrShortCanASingleWordBe?na b c d e f g f e d c b an54254#@%$^%^@4626^#^%^$hahaha#$@%#$@63456326nHello buddy dance tango!nPleaseOhPleaseJumpOverMenThis is too much for me...n样例输出:nnScenario #1:nsearchingnnScenario #2:n-outside-nespanolnnScenario #3:n-outside-nhahahannScenario #4:n-too many- 问答

A Card Trick

02-17

The following card trick is performed by a Magician and her Assistant. The Assistant asks a member of the audience to choose 5 cards from a standard deck of 52 cards (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K of C[lubs], D[iamonds], H[earts] and S[pades]). The Assistant returns one of the cards to the audience member and then hands the remaining cards to the Magician, one at a time. After suitable mumbo-jumbo, the Magician identifies the (fifth) card held by the audience member.nnThe Magician determines the card as follows:nnThe order of the cards in the deck is determined first by the value and for cards of the same value by the suit (both in the order given above). So the total order of cards is: AC, AD, AH, AS, 2D, ... , KH, KSnnRemember the suit and value of the first card.nAmong the remaining three cards find the position of the smallest card (in the above order). Add this position (1, 2, or 3) to the value of the first card.nIf the larger two of the last three cards are not in order, add 3 to the result of step 2.nThe missing card has the same suit as the first card and value that computed in step 3 wrapping around if necessary.nFor example:nn QH, 10D, 10C, 4DnnSmallest of the last 3 cards is 4D in place 3. 10D and 10C are out of order so add 3 + 3 to Q. Wrapping around the missing card is 5H.nnThis problem is to write a program to perform the function of the Assistant.nnnInputnnThe first line of the input consists of a positive integer n, which is the number of datasets that follow. Each of the n following lines contain one data set. The dataset is a sequence of 5 cards separated by a space. Each card is given by a one or two character value and a one character suit as described in the first paragraph.nnnOutputnnFor each dataset, the output on separate lines is an ordering of the 5 input cards as shown in the sample output. The first card in the sequence is the card to be returned to the audience member. The remaining cards are those given to the Magician (in the order given to the Magician). There may be more than one solution to some problems (but that is not to say there *will* be). In cases such as these, output the smallest one, determine the order base at the order of cards given above. For instance 10D 4D QH 10C 5H is also a solution to the first problem below but 5H QH 10D 10C 4D < 10D 4D QH 10C 5H.nnnSample Inputnn2n4D 5H 10C 10D QHn7H 5C KS 6C 8DnnSample OutputnnProblem 1: 5H QH 10D 10C 4DnProblem 2: 6C 5C 7H 8D KS 问答

没有更多推荐了,返回首页