UVA 10935 约瑟夫环

Throwing cards away I

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit

Status

Description

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

<span style="color:#6600cc;">/************************************************
    author    :   Grant Yuan
    time      :   2014.8.4
    algorithm :   约瑟夫环
    source    ;   UVA 10935
************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

queue<int> q;
int n;

int main()
{
    while(1){
    scanf("%d",&n);
    if(n==0) break;
    while(!q.empty()) q.pop();
    for(int i=1;i<=n;i++)
    {
      q.push(i);
    }
    int t;
     printf("Discarded cards:");
    while(q.size()>1){
        if(q.size()==n) printf(" %d",q.front());
         else printf(", %d",q.front());
        q.pop();
        t=q.front();
        q.pop();
        q.push(t);

    }
    cout<<endl;
    printf("Remaining card: %d\n",q.front());}
    return 0;
}
</span>

UVA 10935 约瑟夫环

时间: 2024-07-29 11:55:55

UVA 10935 约瑟夫环的相关文章

uva live 3882 And Then There Was One 约瑟夫环

// uva live 3882 And Then There Was One // // 经典约瑟夫环问题.n是规模,k是每次数的人数,m是第一个出列的人. // // 但是暴力用链表做肯定是不行的,因为 1 <= n <= 10000 , 1<= k <= 10000 // 1 <= m <= n; 虽然我知道公式是什么,但是我并不会推导,看了几乎一个下午的 // 数学推导过程,又弄了几个样例亲自动手实验一下,这样才算是有了一点明悟. // 下面来分享一下自己能力范

UVa 133 双向约瑟夫环

背景:1_TlE:没有考虑到,当k,m很大的时候,就会用太多时间,那么我想到了: k=k%n+n;// 之所以要加n,是为了避免,k是n的倍数时,k等于0. m=m%n+n; 2_WA:经过_TLE:之后没有完善,当k不是n的倍数时就不能加n!终究来说还是没有测试所有数据,以后切题,就把所有数据保存在记事本,要全部通过,才提交!! 好多人都说这是一个双向链表的数据结构题,被我数组模拟过了,双向约瑟夫环... 思路:小紫书在这里出这道题,是想让我们锻炼自顶向下的程序框架思想,即:想建立大框架,一些

一个小笔记(3):约瑟夫环

什么是约瑟夫环?其实百度有说http://baike.baidu.com/view/717633.htm 以一个传说中的问题为例子,提供源代码主要是能够通过这个问题,了解如何来操作循环链表 在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止.然而Josephus 和他的朋友并不想遵从.

C++ 约瑟夫环

约瑟夫环: 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周围的人全部出列. 例如:n = 9, k = 1, m = 5 [解答]出局人的顺序为5, 1, 7, 4, 3, 6, 9, 2, 8. 1 int main()//约瑟夫环 2 { 3 int n=9, m=5,k=2;//n是人数(编号1,2,……,x),m是出列号,k是起始人编号 4 int

【数据结构算法】约瑟夫环

1 约瑟夫环: 2 3 指针 4 5 void Joseph(Node*head,int n,int m) 6 { int i,int j; 7 Node*p,*q; 8 q=head;p=q->next; 9 for(j=1) 10 { 11 for(i=1;i<n;i++){ 12 q=p;p=p->next; 13 } 14 printf("%d",p->number); 15 q->next=p->next; 16 p=q->next;

poj 1781 In Danger(约瑟夫环,找规律)

http://poj.org/problem?id=1781 约瑟夫环的模板,每次数到2的人出圈. 但直接求会TLE,n太大. 打表发现答案和n有关系.当n是2的幂的时候,答案都是1,不是2的幂的时候都与小于2的幂那个数相差差值的2的倍数. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack&

用循环单链表实现约瑟夫环

题目:n个人编号分别是1,2,3,...,n,围坐在一张圆桌周围,从编号为k的人开始报数,数到m的人出列.然后他的下一个人开始报数,数到m的那个人又出列:依次循环,直到所有人出列. struct LNode{ int data; LNode *next; }; //n为总人数,k为第一个开始报数的人,m为出列者喊到的数 void solve(int k,int m, int n) { if(k>n || n<=0 || m<=0) { throw "Invalid argume

php解决约瑟夫环

今天偶遇一道算法题 "约瑟夫环"是一个数学的应用问题:一群猴子排成一圈,按1,2,-,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数, 再数到第m只,在把它踢出去-,如此不停的进行下去, 直到最后只剩下一只猴子为止,那只猴子就叫做大王.要求编程模拟此过程,输入m.n, 输出最后那个大王的编号. 方法一:递归算法 1 function killMonkey($monkeys , $m , $current = 0){ 2 $number = count($mon

php通过循环链解决约瑟夫环

本想着用php写些数据结构提升一下,写到链的时候看到约瑟夫环问题,尝试用循环链写了一下 约瑟夫环: 约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周围的人全部出列 代码: <?php header("Content-type: text/html; charset=utf-8"); /** * 约瑟