Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:数组循环移位的O(n)算法实现 详见编程之美——数字之谜 0MS通过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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator