zoukankan      html  css  js  c++  java
  • [BZOJ 1081] [SCOI2005] 超级格雷码 【找规律】

    题目链接: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[n];
    	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("
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    hdu 4948 Kingdom(推论)
    codeforces 407 div1 A题(Functions again)
    Atcoder regular Contest 073(C
    Atcoder regular Contest 073(D
    Nginx阅读笔记(二)之location的用法
    Nginx阅读笔记
    django virtualenv
    Supervisor
    捕捉攻击者
    django user模块改写
  • 原文地址:https://www.cnblogs.com/JoeFan/p/4264212.html
Copyright © 2011-2022 走看看