Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:数组循环移位的O(n)算法实现 详见编程之美——数字之谜 0MS通过

Posted by ghvn7777 at 2015-07-26 23:44:46 on Problem 3032
In Reply To:数组循环移位的O(n)算法实现 详见编程之美——数字之谜 0MS通过 Posted by:xuchang at 2011-01-06 10:07:22
没看过,感觉你说的和我做好像的差不多
#include <stdio.h>
#include <stdlib.h>

int n;
int left[20];
int right[20];
int data[20];

void init()
{
    int i;
    left[1] = n;
    for (i = 2; i <= n; i++) left[i] = i - 1;
    right[n] = 1;
    for (i = 1; i < n; i++) right[i] = i + 1;
}

void Connect(int X, int Y)
{
    right[X] = Y;
    left[Y] = X;
}

int main(void)
{
    int T, cur, i, j, t;

    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        init();
        cur = 1;
        for (i = 1; i <= n; i++)
        {
            for (j = 0; j < i; j++) cur = right[cur];
            data[cur] = i;
            t = cur;
            cur = right[cur];
            Connect(left[t], right[t]);
        }

        printf("%d", data[1]);
        for (i = 2; i <= n; i++) printf(" %d", data[i]);
        printf("\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator