openjudge约瑟夫问题

听说这是个用链表做,然而。。。。

#include 
#include 
#include 
using namespace std;
int n,m;

struct po
{
    int pre,next,x;
}a[2333];
int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(n == 0 && m == 0)
        break;
        for(int i = 1; i <= n; i ++)
        {
            a[i].x = i;
            a[i].pre = i-1;
            a[i].next = i+1;
        }
        int xx;
        for(int i = 1; i <= n; i ++)
        {
            if(i % m == 0)
            {
                xx = i;
                break;
            }
        }
        int tot = 0;
        int ans = m;
        while(tot < n-1)
        {
            if(ans == m)
            {
                a[a[xx].next].pre = a[(xx)].pre;
                a[a[xx].pre].next = a[(xx)].next;
                ans =0;
                a[(xx)].x = 0;
                tot ++;
            }
            xx ++;
            if(xx > n)
            xx = 1;
            if(a[xx].x != 0)
            ans ++;
        }
        for(int i = 1; i <= n; i ++)
        if(a[i].x != 0)
        cout << a[i].x <

你可能感兴趣的:(模拟)