fjnu 1732 约瑟夫游戏

 

Description

n个人围坐一圈,从1到n编号,从1号开始报数(从1报至m),报m者退出;再从1报数,报m者退出。依退出顺序输出他们的编号。

Input

n m

Output

顺序退出之人编号。每个编号各占一行,前后不留空格。

Sample Input

10 3

 

Sample Output

3
6
9
2
7
1
8
5
10
4
KEY:用循环链表(其实很简单,不用也行)
 
  
  
Source:#include < iostream >
using   namespace  std;

struct  node
{
    
int num;
    node 
*next;
}
;

int  N;
int  m;

void  fun()
{
    node 
*head,*p,*q;
    head
=new(node);
    head
->num=1;
    q
=head;
    
int i;
    
for(i=2;i<=N;i++)
    
{
        p
=new(node);
        p
->num=i;
        q
->next=p;
        q
=p;
    }

    p
->next=head;
    
int n=N;
    p
=head;
    
while(n!=0)
    
{
        
for(i=1;i<m-1;i++) p=p->next;
        q
=p->next;
        cout
<<q->num<<endl;
        p
->next=q->next;
        p
=p->next;
        free(q);
        n
--;
    }

}


int  main()
{
    cin
>>N;
    cin
>>m;
    fun();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值