Blue Bridge Cup 09th 2018 Provincial Competition C/C++ College Students Group B - Question 2 Clear Code

Test question 2 clear code

The glyphs of Chinese characters exist in the font library, and even today, the 16-dot font library is still widely used.
The 16-dot font library regards each Chinese character as 16x16 pixel information. And record this information in bytes.

One byte can store 8 bits of information, and 32 bytes can store the glyph of a Chinese character.
Convert each byte to binary representation, 1 for ink, 0 for background color. 2 bytes per line,
A total of 16 lines, the layout is:

1st byte, 2nd byte
 3rd byte, 4th byte
....
31st byte, 32nd byte

This question is to give you a piece of information composed of multiple Chinese characters, each Chinese character is represented by 32 bytes, and the value of the byte as a signed integer is given here.

The title's requirements are hidden in this information. Your task is to restore the glyphs of these Chinese characters, find out the requirements of the questions, and fill in the answers according to the requirements.

This piece of information is (10 Chinese characters in total):

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 

Note: What needs to be submitted is an integer, do not fill in any redundant content.

Problem solving ideas:

This problem is a binary conversion problem. It should be noted that this is a signed binary conversion. For negative numbers, the sign bit of the most significant bit needs to be set to 1.

For decimal to binary conversion, use the stack to implement.

C++ code:
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
#include <cmath> 
using namespace std;
 
 // fun function: handles decimal conversion to binary, a is the absolute value of decimal, flag indicates whether it is a negative number
string fun(int a, bool flag) {
	stack<char> s;
	// binary conversion
	do {
		s.push(a%2+'0');
		a /= 2;
	}while(a>0);
	// String initialization
	string str = "";
	while(s.size()<8) {
		s.push('0');
	}
	// binary number to string
	while(!s.empty()) {
		if(flag) // Sign bit judges positive and negative
			str += '1';
		else
			str += s.top() == '1' ? '1':' ';
		s.pop();
		flag = false;
	}
	return str;
}
 
int main(int argc, char** argv) {
	for(int j=0;j<10;j++) {
		for(int i=0;i<16;i++) {
	    	int a,b;
	    	cin>>a>>b;
	    	cout<<fun(abs(a),a<0 ? true:false);
	    	cout<<fun(abs(b),b<0 ? true:false)<<endl;
    	}
    	cout<<endl;
	}
	
    return 0;
}

Answer: How much is nine to the ninth power?
Result: 387420489

Tags: C++ data structure stack

Posted by misterph on Sun, 15 May 2022 06:56:43 +0300