acm hnu 134 Joseph's Puzzle

本文介绍了一个经典的约瑟夫环问题,通过设定初始人数及计数规则,模拟了淘汰过程,并提供了具体的输入输出样例与实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Joseph's Puzzle
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 134, Accepted users: 125
Problem 10045 : No special judgement
Problem description
Persons, whose number order is from 1 to N, hold a password (the straight integer less than 10000) each person and sit around clockwise.
At first we choose a positive integer factor as the count off number M and start to number clockwise from number one .As we number to M the person who numbers M leaves the rank and whose password will be the new number M. And then start to number again from the person who is sit clockwise next to the person who has been out off the rank just before then. Continue like this, until all people are out off the rank completely. Please design a program of leaving order.

Input
There have some groups data. First line of each group data is two integers N, M (0<N, M<100),the second line is N positive integer, separately express every person’s password.The last line contins 0,0 ,which means the end of input data.

Output
To each group of data, according to the order which leaves outputs their serial number. Between the numeral separates with a blank space. Each group of data monopolize line of outputs.

Sample Input
7 20
3 1 7 2 4 8 4
4 3
1 2 3 4
0 0
Sample Output
6 1 4 7 2 3 5
3 2 1 4
Problem Source
HNU 1'st Contest
/*18:10~  */



#include 
<stdio.h>

#define MAX 50000

int move( int num[],int Mr );

main()

{

      
int num[MAX],n,Mright=0,p=0,total=0,M=0,Mr=0;

      
int i,j;

      i 
= 0;

      
for (i = 0 ; i < MAX ; i++)num[i]=-2
                

      scanf(
"%d%d",&n,&M);

      
for (i = 0 ; i < n ; i++)scanf("%d",&num[i]); 

      
while( n!=0 && M!=0 ){

             total 
= n;

             

             p 
= 0;

             Mr 
= M%n;

             
while( total > 0 ){

                    p
=move(num,Mr);

            
if(total==1){printf("%d ",p+1);break;}

            
else  printf("%d ",p+1);

            M 
= num[p];//record M

            num[p]
=-1;

            total
--;

            p
++;    //delete it

                    M
=M%total;

            
//caculate the amount after this

            i
=p;

            Mright
=0;

            
while(num[i]!=-2){

              
if(num[i]!=-1)Mright++;

              i
++;

            }

            
if(M==0&&total==1){Mr=1;continue;}

            
if(M==0)Mr=total-Mright;

            
else if(M>Mright)Mr=M-Mright;

            
else Mr=total+M-Mright;

          }
//while total

              
for (i = 0 ; i < MAX ; i++)num[i]=-2;             

              scanf(
"%d%d",&n,&M);
              
for (i = 0 ; i < n ; i++)scanf("%d",&num[i]); 

      }

 
return 0;
}



//return current p,M < total

int move( int num[],int Mr )

{

    
int c=0,p=0;

    
while(1){

             
if( num[p] != -1 ){ 

                 c
++;

                
if(c == Mr)return p;

                 }

                 p
++;

             }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值