对刚

这里写图片描述

约瑟夫问题,数组链表可以过的。而且贼快。

#include<iostream>
#include<cstdio>
#include<vector>
#define N 100009
using namespace std;
int n,t,L[N],R[N],p,d,rest;
bool f[N];
int main()
{
    freopen("resist.in","r",stdin);
    freopen("resist.out","w",stdout);
    scanf("%d%d",&n,&t);
    for(int i=1;i<n;i++) R[i]=i+1;R[n]=1;
    for(int i=2;i<=n;i++) L[i]=i-1;L[1]=n;

    rest=n;d=1;p=1;
    while(rest>1)
    {
        while(p<t-1)
        {
            p++;
            d=R[d];
        }
        f[R[d]]=1;
        R[d]=R[R[d]];
        rest--;
        p=1;d=R[d];
    }
    for(int i=1;i<=n;i++) if(!f[i])
    {
        printf("%d
",i);break;
    } 
    return 0;
} 
原文地址:https://www.cnblogs.com/dfsac/p/7587787.html