时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
用分治算法生成循环赛日程表(1到2的n次方个人)
输入:
一个整数n
输出:
循环赛日程表(1到2的n次方个人)
输入样例:
3
输出样例:
1 2 3 4 5 6 7 82 1 4 3 6 5 8 73 4 1 2 7 8 5 64 3 2 1 8 7 6 55 6 7 8 1 2 3 46 5 8 7 2 1 4 37 8 5 6 3 4 1 28 7 6 5 4 3 2 1#include <iostream> using namespace std; int N = 1; void UpRightCopy(int n, int array[]) { for (int i = 0; i < n; i++) { for (int j = n; j < 2 * n;j++) { array[N * i + j] = 2 * n + 1 -array[N * i + 2 * n - 1 - j]; } } } void DownRightCopy(int n, int array[]) { for (int i = n; i < 2 * n; i++) { for (int j = n; j < 2 * n;j++) { array[N * i + j] = array[N * (i - n) + j - n]; } } } void LeftDownCopy(int n, int array[]) { for (int i = n; i < 2 * n; i++) { for (int j = 0; j < n;j++) { array[N * i + j] = array[N * (i - n) + j + n]; } } } void tura(int n, int array[]) { if (n == 1) array[0] = 1; else { tura(n / 2, array); DownRightCopy(n / 2, array); UpRightCopy(n / 2, array); LeftDownCopy(n / 2, array); } } int main(int argc, char *argv[]) { int m; cin >> m; for (int i = 0;i < m;i++) N *= 2; int *array = new int[N * N]; tura(N, array); for (i = 0;i < N; i++) { for(int j = 0;j < N; j++) { if(j < N - 1) cout << array[N * i + j] << " "; else cout << array[N * i + j] << endl; } } delete []array; return 0; }
posted on 2011-06-21 22:39 ruce.fan 阅读(165) 评论(0) 收藏 举报