明码问题四种方法(转载自其他博客)

问题:

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

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

一个字节可以存储8位信息,用32个字节就可以存一个汉字的字形了。
把每个字节转为2进制表示,1表示墨迹,0表示底色。每行2个字节,
一共16行,布局是:

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

这道题目是给你一段多个汉字组成的信息,每个汉字用32个字节表示,这里给出了字节作为有符号整数的值。
题目的要求隐藏在这些信息中。你的任务是复原这些汉字的字形,从中看出题目的要求,并根据要求填写答案。
这段信息是(一共10个汉字):
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

分析:

最近一直在看这个明码问题,2018年第二题,乍看看不懂啥意思,但是仔细看看再加上看别人的代码之后,明白这个题的意思了。其实就是将这些十进制数字转换为二进制数字,然后0代表底色或者空白;1代表有颜色。
自己做的话,将这些数字转换为二进制数字之后,人工处理数据,是最麻烦的,不建议采用。
在网上看了别的大佬的博客之后,稍微总结了一下,有这样四种方法:

1,利用Excel表格:

转载自Excel熟练程度解决问题https://www.cnblogs.com/liqiujiong/p/8733320.html)

使用Word,将十行数据转换为两列一行,方便导入excel处理。
在这里插入图片描述

    copy到excel里面,使用DEC2BIN函数(十进制Dec,二进制Bin),进行进制转换.             

在这里插入图片描述

效果:

在这里插入图片描述

2,模拟法:

转载自模拟法

https://blog.csdn.net/u013377068/article/details/79778568)

代码:

#include <iostream>
#include <string.h> 
using namespace std;
int  Binary[8];
int TransFormation(int a)
{
    bool is = true;
    int index = 0;
    if(a < 0)
    {
        is = false;
        a = -a;
    }
    memset(Binary,0,sizeof(Binary));  //因为一个字节只储存八位,并且除了有效位之外全部是0;
    while(a)
    {
        Binary[index++] = a % 2;  //倒着储存二进制 
        a /= 2;
    }
    if(!is)
    {
        for(int i = 0; i < 8; i++)
        {
            Binary[i] ^= 1; //取原码的补码
        }
        int temp;
        for(int i = 0; i < 8; i++)
        {
            if(i == 0) 
                temp = Binary[i] += 1;    //取反码的补码 
            else
                temp = Binary[i];
            if(temp >= 2)
            {
                Binary[i+1] += 1;
                Binary[i] %= 2; 
            }
            else
                break;
            
        }
    }
    for(int i = 7; i >= 0; i--)
    {
        if(Binary[i] == 1)
            printf("#");
        else
            printf(" ");
    }
}
int main()
{
    int data[15][40];
    int n = 10,  m = 32;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            scanf("%d",&data[i][j]);
        }
        
    }
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
                    
            TransFormation(data[i][j]);  
            
            if(j&1)         //每两个字符代表一个汉字的一行相当于(j%2!=0) 
            printf("\n");
        }
        printf("\n");
    }
    
    return 0;
} 

3,库函数(bitset()函数):

转载自库函数

https://blog.csdn.net/u011747888/article/details/79781040)

代码:

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    int n,m;
    int len;
    string temp;
    bitset<8> t;
    while(cin>>n>>m)
    {
        t = n;
        temp = t.to_string();
        len = temp.length();
        for(int i = 0; i < len; i++)
        {
            if(temp[i]=='0')
            {
                cout << " ";
            }
            else
            {
                cout << "*";
            }
        }
        t = m;
        temp = t.to_string();
        len = temp.length();
        for(int i = 0; i < len; i++)
        {
            if(temp[i]=='0')
            {
                cout << " ";
            }
            else
            {
                cout << "*";
            }
        }
        cout << endl;
 
 
    }
 
}

4,直接将每个数字转为2进制:

转载自:直接转换

https://blog.csdn.net/huang826336127/article/details/79779367#reply)

代码:

#include<iostream>  
using namespace std;  
  
int main()  
{  
    int m,n,w[16];  
    while(cin>>m>>n)  
    {  
        for(int i=7;i>=0;i--){w[i]=m&1;m>>=1;}  
        for(int i=15;i>=8;i--){w[i]=n&1;n>>=1;}  
        for(int i=0;i<=15;i++){cout<<w[i]<<" ";}  
        cout<<endl;  
    }  
}  

感悟:

唉,牛逼的大佬太多了。转载他们的博客,互相学习,共同进步。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值