首页 > 其他分享 >卡片游戏(Throwing cards away I)

卡片游戏(Throwing cards away I)

时间:2022-11-28 18:34:49浏览次数:40  
标签:Throwing away System cards input line card out


Problem B: Throwing cards away I


Given is an ordered deck of

n cards numbered 1 to

n with card 1 at the top and card

n at the bottom. The following operation is performed as long as there are at least two cards in the deck:

Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.

Your task is to find the sequence of discarded cards and the last, remaining card.

Each line of input (except the last) contains a number n ≤ 50. The last line contains 0 and this line should not be processed. For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing spaces. See the sample for the expected format.

Sample input


7 19 10 6 0


Output for sample input


Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card: 4
Discarded cards: 1, 3, 5, 2, 6
Remaining card: 4


Folklore, adapted by Piotr Rudnicki

【分析】

       模拟队列:卡片按编号1,2,...,n依次加入,按照规则从队头取出,再将新的队头取出加入到队尾。

用java语言编写程序,代码如下:


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int n;
while(input.hasNext()) {
n = input.nextInt();
if(n == 0)
break;

Queue<Integer> q = new LinkedList<Integer>();
for(int i = 1; i <= n; i++)
q.add(i);

System.out.print("Discarded cards:");
boolean first = true;
while(q.size() > 1) {
if(first) {
System.out.print(" " + q.poll());
first = false;
}
else {
System.out.print(", " + q.poll());
}

int temp = q.poll();
q.add(temp);
}
System.out.println();
System.out.println("Remaining card: " + q.peek());
}
}
}




标签:Throwing,away,System,cards,input,line,card,out
From: https://blog.51cto.com/u_15894233/5893366

相关文章

  • CF1539E Game with Cards
    把最终答案看成一段\(0\),一段\(1\)的一个串。如果说我们的答案中有一段\(0\)(\(1\)同理)。那么所有\(0\)的数都满足所有第一个范围,这段\(0\)前面的\(1\)代......
  • CF1392H ZS Shuffles Cards 题解
    linkDescription有\(n\)张数字牌以及\(m\)张鬼牌,有一个不可重集合\(S\),初始为空。不断执行以下操作:抽出一张牌,如果为数字牌,则加入\(S\)并移除。如果为鬼牌,如果......
  • D. Knowledge Cards
    D.KnowledgeCardsPakChanek,arenownedscholar,inventedacardpuzzleusinghisknowledge.Inthepuzzle,youaregivenaboardwith$n$rowsand$m$colum......
  • 开始使用AspectJ-@AfterThrowing ,@After ,@Pointcut 定义切入点(比较重要)
    开始使用AspectJ(接下来的作为了解就行)1.[了解]@AfterThrowing异常通知-注解中有throwing属性在目标方法抛出异常后执行。该注解的throwing属性用于指定所发生的异......
  • How far away ?
    ​​Howfaraway?​​预处理出每一个节点到根节点的距离即可,两个节点之间的距离为​​len[x]+len[y]-2*len[lca(x,y)]​​//CreatedbyCAD#include<bits/stdc++.h>#defi......
  • Spring AOP(三)之AfterThrowing增强处理 异常通知,拦截异常,处理异常,继续向外传播和cat
    SpringAOP(三)之AfterThrowing增强处理异常通知,拦截异常,处理异常,继续向外传播和catch不同使用@AfterThrowing注解可以修饰AfterThrowing增强处理,AfterThrowing增强处......
  • C# 四舍五入 MidpointRounding.AwayFromZero
    ROUND()是C#中math的一个成员函数.System.Math.Round(),这个函数有四种用法,最长用的是对小数点位数的舍入.但这和现实生活中的“四舍五入”有一定区别,也有别JAVA中Math.Round(......
  • ERROR 2006 (HY000): MySQL server has gone away
    sourcesql文件的时候,报错如下:ERROR2006(HY000):MySQLserverhasgoneaway 经查阅导致该error的原因很多,具体分析了我的问题是语句太长了,修改max_allowed_packet......
  • SAS 用cards/datalines读入原始数据举例
    SAS用cards/datalines读入原始数据:input作用:1)当数据没有这个变量时生成新变量2)读取cards或外部数据。语法:inputinformat.  在input设定的输入格式并不存储在创......
  • mysql运行sql文件报错[ERR] 2006 - MySQL server has gone away [ERR] -- MySQL dump
    原因:在运行数据库脚本文件时报该错,由于mysql对max_allowed_packect 允许最大的数据包的大小有限制解决方法:1.先查看现在允许的最大包大小,单位(字节) select@@max_allow......