位运算 格雷码 gray

题目:

It is necessary to arrange numbers from 0 to 2^(N+M)-1 in the matrix with 2^N rows and 2^M columns. Moreover, numbers occupying two adjacent cells must differ only in single bit in binary notation. Cells are adjacent if they have common side. Matrix is cyclic, i.e. for each row the leftmost and rightmost matrix cells are considered to be adjacent (the topmost and the bottommost matrix cells are also adjacent).

Input

The first line of input contains two integers N and M (0<N,M; N+M<=20).

Output

Output file must contain the required matrix in a form of 2^N lines of 2^M integers each.

Sample test(s)

Input

1 1

Output

0 2 
1 3

解答:

#include<cstdio>
#include<iostream>
#include<iomanip>
using namespace std;

int main(){

 int x,y,m,n,u;
 cin>>m>>n;
 cout<<(1<<m)<<" "<<(1<<n)<<endl;
 for(x = 0;x <= (1<<m) - 1;x++){
  u = (x ^ (x >> 1))<<n;//前m位
  for(y = 0;y <= (1<<n) - 1;y++){
   cout<<setw(3)<<(u | (y ^ (y >> 1)))<<" ";//前m位与后面的n位合起来
  }
  cout<<endl;
 }

 return 0;
}

参见:

http://www.matrix67.com/blog/archives/266

时间: 2024-08-28 20:51:24

位运算 格雷码 gray的相关文章

格雷码 Gray

格雷码 Gray 格雷码定义: 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code) 格雷码的求解: 上一次求解到的Gray码, 按顺序给每一个都加上前缀0,  得到前一半,然后,将Gray码逆序,给每一个都加上前缀1就是答案. 例如: n = 1 Gray: 0 1 n = 2 00  01  (顺序 0 1) 11  10  (逆序 1 0) 具体代码 #include <bits/stdc++.h> using namespace std

格雷码(Gray code)仿真

作者:桂. 时间:2018-05-12  16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码,简要记录格雷码的分析思路. 一.格雷码与8421码对应关系 通过真值表分析,可以得出: 即格雷码是:8421码从最右边起,依次与左边一位异或,最左边一位不变,对应实现语言: GrayCount_out <= {BinaryCount[COUNTER_WIDTH-1],BinaryCount[COUN

格雷码(Gray Code)

https://www.yuque.com/docs/share/45ca4609-0038-4ae1-beb5-76320d5b0acd 原文地址:http://blog.51cto.com/4754569/2326827

[C++]LeetCode: 86 Gray Code (格雷码)

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

产生n位元的所有格雷码

原文链接:http://blog.csdn.net/beiyeqingteng/article/details/7044471 问题:产生n位元的所有格雷码. 格雷码(Gray Code)是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数字,任两个数之间只有一个位元值不同. 例如以下为3位元的格雷码: 000 001 011 010 110 111 101 100 . 如果要产生n位元的格雷码,那么格雷码的个数为2^n. 假设原始的值从0开始,格雷码产生的规律是:第一步,改变最右

构造N位格雷码(递归,面向对象)

问题:递归打印出N位格雷码(相邻两个编码只有一位数字不同): 问题化归为:现有前N位的格雷码,如何构造N+1位的格雷码? 解决方法:采用递归构造格雷码集和. 递归出口:n = 1; 此时格雷码{0,1} N+1:N+1位的格雷码 = N位格雷码(顺序)+0,N位格雷码逆序+1(N位的格雷码顺序最后一个编码与逆序第一个编码是同一个编码,在此可以分别加0,1两个编码依旧只有一位不同) public class GC{ int[][] G;// int N; int SIZE; GC(int N){

格雷码的实现

问题:产生n位元的所有格雷码. 格雷码(Gray Code)是一个数列集合,每个数使用二进位来表示,假设使用n位元来表示每个数字,任两个数之间只有一个位元值不同. 例如以下为3位元的格雷码: 000 001 011 010 110 111 101 100 . 如果要产生n位元的格雷码,那么格雷码的个数为2^n. 假设原始的值从0开始,格雷码产生的规律是:第一步,改变最右边的位元值:第二步,改变右起第一个为1的位元的左边位元:第三步,第四步重复第一步和第二步,直到所有的格雷码产生完毕(换句话说,已

(算法)格雷码

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

[腾讯]生成格雷码

时间限制:3秒 空间限制:32768K 热度指数:24655 本题知识点: 递归 题目描述 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同, 则称这种编码为格雷码(Gray Code),请编写一个函数,使用递归的方法生成N位的格雷码. 给定一个整数n,请返回n位的格雷码,顺序为从0开始. 测试样例: 1 返回:["0","1"] 思路: class GrayCode {public:    vector<string> getGray(int