hdu2925 Musical Chairs(约瑟夫问题)

Musical Chairs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 964    Accepted Submission(s): 548


Problem Description
In the traditional game of Musical Chairs, N + 1 children run around N chairs (placed in a circle) as long as music is playing. The moment the music stops, children run and try to sit on an available chair. The child still standing leaves the game, a chair is removed, and the game continues with N children. The last child to sit is the winner.

In an attempt to create a similar game on these days' game consoles, you modify the game in the following manner: N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N . Your program pre-selects a positive number D . The program starts going in circles counting the children starting with the first chair. Once the count reaches D , that child leaves the game, removing his/her chair. The program starts counting again, beginning with the next chair in the circle. The last child remaining in the circle is the winner.


For example, consider the game illustrated in the figure above for N = 5 and D = 3 . In the figure, the dot indicates where counting starts and × indicates the child leaving. Starting off, child #3 leaves the game, and counting restarts with child #4. Child #1 is the second child to leave and counting restart with child #2 resulting in child #5 leaving. Child #2 is the last to leave, and child #4 is the winner. Write a program to determine the winning child given both N and D .
 

Input
Your program will be tested on one or more test cases. Each test case specifies two positive integers N and D on a single line, separated by one or more spaces, where N, D < 1, 000, 000 .

The last line of the input file contains two 0's and is not part of the test cases.
 

Output
For each test case, write the winner using the following format:


N D W


Where N and D are as above, is a space character, and W is the winner of that game.
 

Sample Input
  
  
5 3 7 4 0 0
 

Sample Output
  
  
5 3 4 7 4 2
 


约瑟夫问题解析  http://baike.baidu.com/link?url=cWITPhhby7QRCs8X5d12Lf2-YJR2nE_pqNWig3YGre2fyVHU7NMfIEVOg90zAdt6

#include<stdio.h>
int main()
{
    int n,d;
    while(scanf("%d%d",&n,&d),n|d)
    {
        __int64 w=0;
        for(int i=2;i<=n;i++)
        {
            w=(w+d)%i;
        }
        printf("%d %d %I64d\n",n,d,w+1);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值