对刚—约瑟夫环

背景: 
我营营长一言不合就想干人,搞得整个营乌烟瘴气 
题目描述: 
这次,营长让n个同学按照编号1至n顺时针围成一圈,从一号开始顺时针报数,报到t的人直接被教官打死,拖到圈子外面,然后从他下一位同学接着开始报数,某位同学为了伸张正义,准备找教官对仗,顺便搞一个大新闻,为了有充分的时间准备,他决定站在最后一个被教官干掉的地方,你需要求出这是第几个位置。 
输入描述: 
一行两个数n,t 
输出描述: 
一行一个数表示答案 
样例输入: 
3 2 
样例输出: 

数据范围: 
对于30%的数据,满足n≤100 
对于100%的数据,满足n≤100000,1≤t≤100 

思路:

  用链表模拟,每次把报到t的人从链表中绕过。

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define N 1000009
int n,t,now,last;
int nex[100009];
int main()
{
    freopen("resist.in","r",stdin);
    freopen("resist.out","w",stdout);
    scanf("%d%d",&n,&t);
    for(int i=1;i<n;i++)
    nex[i]=i+1;
    nex[n]=1;now=n;
    for(int i=1;i<n;i++)
    {
        
        for(int j=1;j<=t;j++)        
            last=now,now=nex[now];
        nex[last]=nex[now];
    
    }
    cout<<nex[now];
    return 0;
} 

样例:

  输入 10 2

  输出 5

原文地址:https://www.cnblogs.com/CLGYPYJ/p/7561501.html