您的位置:首页 > 其它

[BZOJ 1081] [SCOI2005] 超级格雷码 【找规律】

2015-01-31 15:56 330 查看
题目链接:BZOJ - 1081

备注:此题BZOJ上貌似没有 spj ,要把一般顺序的每个格雷码倒着输出...比如 0102 输出为 2010

题目分析

就是按照 Gray 码的生成方法写前几个出来找找规律就好了。

生成方法:以 3 位 3 进制为例

0 0 0

0 0 1

0 0 2

0 1 2 //中位写 1 ,后面镜像复制

0 1 1

0 1 0

0 2 0 //中位写 2,后面镜像复制

0 2 1

0 2 2

1 2 2 //高位写 1 ,后面镜像复制

1 2 1

........

代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int MaxN = 20 + 5;

int n, m, Tot;
int Pow[MaxN];

void PrintCh(int x) {
if (x < 10) printf("%d", x);
else printf("%c", 'A' + x - 10);
}

int main()
{
scanf("%d%d", &n, &m);
Pow[0] = 1;
for (int i = 1; i <= n; ++i) Pow[i] = Pow[i - 1] * m;
Tot = Pow
;
int x, y;
for (int i = 0; i < Tot; ++i) {
for (int j = n - 1; j >= 0; --j) {
x = i / Pow[n - j];
y = (i / Pow[n - j - 1]) % m;
if (x & 1) PrintCh(m - y - 1);
else PrintCh(y);
}
printf("\n");
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: