2018蓝桥杯 明码 解题方法

题目:明码
汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛。
###16点阵的字库把每个汉字看成是16x16个像素信息。并把这些信息记录在字节中。

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

第1字节,第2字节
第3字节,第4字节
....
第31字节, 第32字节
1
2
3
4
###这道题目是给你一段多个汉字组成的信息,每个汉字用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

###注意:需要提交的是一个整数,不要填写任何多余内容。

一、简单方法

利用bitset库完成,它能实现对位的操作,bitset<8> v(a[i])构建一个占用8位(1bit),内容为a[i]的对象v;

题目数据通过ifstream读取放在数组a中
 

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
#include<bitset>
#include<string>
using namespace std;
int main()
{
	int cnt = 0;
	int a[320];
	ifstream file("DATA.txt");
	for(int i = 0; i < 320; i++)
		file >> a[cnt++];
	for (int i = 0; i < 320; i++)
	{
		bitset<8> v(a[i]);
		string s = v.to_string();
		for (int j = 0; j < 8; j++)
		{
			if(s[j] == '1') s[j] = '#';
			else s[j] = ' ';
		}
		cout << s;
		if ((i + 1) % 2 == 0) cout << endl;
	}
	
		
	return 0;
}

二、通过模拟手工二进制算法解题

读取二进制数据,模拟手工计算(负数需要转为正数计算二进制,然后取反加一)

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
#include<bitset>
#include<string>
#include<cmath>
using namespace std;

int main()
{
	int cnt = 0;
	int a[320];
	ifstream file("DATA.txt");
	for(int i = 0; i < 320; i++)
		file >> a[cnt++];
	int k = 0;
	string s;
	for (int w = 0; w < 320; w++)
	{
		if (a[w] >= 0)
		{
			for (int i = 0; i < 8; i++)
			{
				k = a[w] % 2;
				a[w] /= 2;
				s += k + '0';
			}
		}
		else
		{
			a[w] = abs(a[w]);
			for (int i = 0; i < 8; i++)
			{
				k = a[w] % 2;
				a[w] /= 2;
				s += k + '0';
			}
			for (int j = 0; j < 8; j++)
			{
				if (s[j] == '1') s[j] = '0';
				else s[j] = '1';
			}
			int k = 0;
			while (s[k] != '0')
			{
				s[k] = '0'; k++;
			}
			s[k] = '1';
		}
		reverse(s.begin(), s.end());
		cout << s;
		if ((w + 1) % 2 == 0) cout << endl;
		s.clear();
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值