蓝桥杯 - 明码 java

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

汉字的字形存在于字库中,即便在今天,1616 点阵的字库也仍然使用广泛。

1616 点阵的字库把每个汉字看成是 16 \times 1616×16 个像素信息。并把这些信息记录在字节中。

一个字节可以存储 88 位信息,用 3232 个字节就可以存一个汉字的字形了。 把每个字节转为 22 进制表示,11 表示墨迹,00 表示底色。每行 22 个字节,一共 1616 行,布局是:

    第 1 字节,第 2 字节
    第 3 字节,第 4 字节
    ....
    第 31 字节, 第 32 字节

这道题目是给你一段多个汉字组成的信息,每个汉字用 3232 个字节表示,这里给出了字节作为有符号整数的值。

题目的要求隐藏在这些信息中。你的任务是复原这些汉字的字形,从中看出题目的要求,并根据要求填写答案。

这段信息是(一共 1010 个汉字):

4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16 
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4 
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64 
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128 
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0 
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0 
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0 
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0 

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

理解题意

输入的每个数是10进制,转为二进制是8位数,题目说每个汉字是16*16显示,因此每次读入两个数即为一行

问题转化为将输入的数转化为二进制然后每两个一行输出

将数字转化为2进制的数

//将数字转化为2进制的数
    private static int[] exchange(int value){
        int[] arr = new int[8];
        Arrays.fill(arr,0);
        int va = Math.abs(value);
        //先都当做正数
        for(int i = 7; i >= 0; i--){
            arr[i] = va % 2;
            va /= 2;
        }
        int flag = 0;
        if(value < 0){
            for(int i = 7; i >= 0; i--){
                if(flag == 0 && arr[i] == 1){
                    flag = 1;
                }else {
                    arr[i] = 1 - arr[i];
                }
            }
        }
        return arr;
    }

主函数

package com.example.leetcode_testing.lanqiao;

/**
 * @author
 * @description:
 * @since 2022-10-01 10:41
 */
import java.util.Scanner;
import java.math.BigDecimal;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class mingma {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int flag = 0;
        while(scan.hasNext()){
            int value1 = scan.nextInt();
            int value2 = scan.nextInt();
            int [] arr1 = exchange(value1);
            int [] arr2 = exchange(value2);
            for (int i : arr1) {
                if(i==1){
                    System.out.print(""+i);
                }else{
                    System.out.print(" ");
                }

            }
            for (int i : arr2) {
                if(i==1){
                    System.out.print(""+i);
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
            flag+=1;
            if(flag>=16&&flag%16==0){
                System.out.println("\n");
            }
        }
        scan.close();
    }
    //将数字转化为2进制的数
    private static int[] exchange(int value){
        int[] arr = new int[8];
        Arrays.fill(arr,0);
        int va = Math.abs(value);
        //先都当做正数
        for(int i = 7; i >= 0; i--){
            arr[i] = va % 2;
            va /= 2;
        }
        int flag = 0;
        if(value < 0){
            for(int i = 7; i >= 0; i--){
                if(flag == 0 && arr[i] == 1){
                    flag = 1;
                }else {
                    arr[i] = 1 - arr[i];
                }
            }
        }
        return arr;
    }
}

根据字形可以看出需要求9的9次方

     1          
     1          
     1    1     
1111111111111111
     1    1     
     1    1     
     1    1     
     1    1     
     1    1     
    1     1     
    1     1     
   1      1   1 
   1      1   1 
  1        1111 
11111111        


   1     1      
   1     1      
  1   1  1   1  
 1111111 111111 
 1    1 1    111
 1    11     1  
 1    1      1  
 1    1 1    111
 111111  11  1  
 1    1   1  1  
 1    1      1  
 1    1      1  
 1    1      1  
 111111      1  
 1    1   1 1   
           1    


     1          
     1          
     1          
     1    1     
1111111111111111
     1    1     
     1    1     
     1    1     
     1    1     
     1    1     
    1     1     
    1     1     
   1      1   1 
   1      1   1 
  1        1111 
11111111        


        11111111
 1      11111111
  11    11111111
   1   1    1   
       111111111
      1     1   
    1    1 1    
   1     1      
  1      1      
11111111 1      
  1     1 111111
  1     1 111111
  1    1   1    
  1   1     1   
  1  1      111 
  1 1        1  


     1          
      11        
       1        
             1  
1111111111111111
     1          
     1     1    
     11111111111
     1     1    
     1     1    
     1     1    
    1      1    
    1      1    
   1       1    
  1     1 111111
 1       1      


   1     1      
   1 1   1  1   
  11111 11111111
 1  1  1  1     
     1 1   1    
       1        
  11111111111111
       1        
1111111111111111
         1      
         1 1    
  11111111111111
    1    1      
     1   1      
       1 1      
        11111111


           1    
  11111111111111
       1        
       1        
       1        
       1     1  
1111111111111111
       1        
       1        
       1        
       1        
       1        
       1        
       1        
     1 1        
      1         


      1         
      1         
     11111111111
    1     1     
   11    1      
  1  1 111111111
      1 11111111
    11  11111111
 111   111111111
      1     1   
    11     1    
   1  1   1     
  1    111111111
       1        
    111         
 111            


       1        
       1        
       1        
    1  1  1     
    1  1   1    
   1   1    11  
   1   1     1  
  1    1   1    
 1     1   1    
       1  1     
       1 1      
        11111111
       1        
      1         
    11          
 111            


                
                
     11111111111
   11      11   
  11        11  
  111       11  
          111   
        11111111
        11111111
        11111111
                
                
       111111111
      1111111111
       111111111
                



使用BigDecimal或者BigInteger

import java.util.*;
import java.math.BigDecimal;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(BigDecimal.valueOf(Math.pow(9, 9)));
        scan.close();
    }

}
import java.util.Scanner;
import java.math.BigInteger;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        BigInteger a = BigInteger.valueOf(9);
        System.out.println(a.pow(9));
        scan.close();
    }
    
}

猜你喜欢

转载自blog.csdn.net/weixin_51712663/article/details/127133422