zoukankan      html  css  js  c++  java
  • 格雷码示例

    package java1;
    import java.util.*;
    public class Java1 {
    
        public static void main(String[] args) {
            
            System.out.println("请输入一个正整数:");
            Scanner input = new Scanner(System.in);
            int n = input.nextInt();
            String[] getReturn = gray(n);
            System.out.println("格雷码为:");
            for(int i=0;i<Math.pow(2, n);i++){
            if(i%5==0){
                System.out.println();
            }
            System.out.print(getReturn[i]+"  ");
            
            }
        }
        
        public static String[] gray(int n){
             
                // produce 2^n grade codes  
                String[] graycode = new String[(int) Math.pow(2, n)];            
                if (n == 1) {  
                    graycode[0] = "0";  
                    graycode[1] = "1";  
                    return graycode;  
                }            
                String[] last = gray(n - 1);            
                for (int i = 0; i < last.length; i++) {  
                    graycode[i] = "0" + last[i];  
                    graycode[graycode.length - 1 - i] = "1" + last[i];  
                }      
                return graycode;
                
             
        }
    }
    

      

    时间最会骗人,但也能让你明白,这个世界上没有什么是不能失去的,留下的尽力珍惜,得不到的都不重要
  • 相关阅读:
    git 文件回滚
    常用函数
    触发器
    UPDATE
    DELETE
    INSERT
    完整性与约束
    流程控制
    SET ANSI_NULL ON 和 SET QUOTED_IDENTIFIFR ON
    SELECT 与 SET给标量赋值
  • 原文地址:https://www.cnblogs.com/www-x/p/7967971.html
Copyright © 2011-2022 走看看