zoukankan      html  css  js  c++  java
  • 【原】 POJ 3517 And Then There Was One Joseph问题 解题报告

    http://poj.org/problem?id=3517

    方法:具体参见3750

    假设此环为以m+1起始,m-1终止的n-1环,且m+1--->1,求得此环最后出队的编号
    再将此编号转换成原n环的编号


    注意:
    此题m为起始点,k为步长。且首先出队编号m,再以步长k循环

    Description

    Let’s play a stone removing game.

    Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from mand remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k − 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.

    clip_image001
    Initial state

    clip_image002
    Step 1

    clip_image003
    Step 2

    clip_image004
    Step 3

    clip_image005
    Step 4

    clip_image006
    Step 5

    clip_image007
    Step 6

    clip_image008
    Step 7

    clip_image009
    Final state

     

    Figure 1: An example game

    Initial state: Eight stones are arranged on a circle.

    Step 1: Stone 3 is removed since m = 3.

    Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.

    Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

    Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.

    Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

    Input

    The input consists of multiple datasets each of which is formatted as follows.

    n k m

    The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

    2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ mn

    The number of datasets is less than 100.

    Output

    For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

    Sample Input

    8 5 3

    100 9999 98

    10000 10000 10000

    0 0 0

    Sample Output

    1

    93

    2019

       1: #include <iostream>
       2: #include <fstream>
       3:  
       4: using namespace std ;
       5:  
       6:  
       7: int Joseph(int n,int m)
       8: {
       9:     int lastout = 1 ;
      10:     for( int i=2 ; i<=n ; ++i )
      11:         lastout = (lastout+m-1)%i+1 ;
      12:     return lastout ;
      13: }
      14:  
      15: void run3517()
      16: {
      17:     ifstream in("in.txt");
      18:     int n,k,m ;
      19:     while(in>>n>>k>>m && n!=0)
      20:     {
      21:         m = (m-1)%n+1 ;
      22:         cout << ( Joseph(n-1,k)+m-1 )%n+1 << endl;
      23:     }
      24: }

    如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

    原创文章属于Allen Sun
    欢迎转载,但请注明文章作者Allen Sun和链接
  • 相关阅读:
    Json操作(汇总)
    Jersey实现Client端:GET/PUT/POST/DELETE
    tomcat配置文件server.xml详解
    DB2 Catalog浅析&学习笔记
    《学习笔记》Maven
    【58沈剑架构系列】单点系统架构的可用性与性能优化
    JavaScript(获取或设置html元素的宽,高,坐标),确定和判断鼠标是否在元素内部,二级导航菜单鼠标离开样式问题解决
    纯CSS绘制三角形(各种角度)
    css3 box-sizing属性值详解
    css字体图标的使用方法
  • 原文地址:https://www.cnblogs.com/allensun/p/1872085.html
Copyright © 2011-2022 走看看