标题:明码
汉字的字形存在于字库中,即便在今天,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
注意:需要提交的是一个整数,不要填写任何多余内容。
代码:
/*
Project: 2018蓝桥杯省赛c/c++B组 第二题
Date: 2018/04/03
Author: Frank Yu
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<set>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;
//考试时懵逼了,非要把数组返回主函数再输出,结果没写出来。数组返回问题,可以转化为struct再返回
//10进制转二进制的原码(应该是补码,但是相信你用眼睛可以看出),
void d_to_b(int n)
{
int a[8]; //一个笔画
if(n>=0) //正数反码与原码同
{ for(int i=7;i!=-1;i--) //转换为二进制补码并输出
{
a[i]=n/(int)pow(2,i);
if(a[i]==1)printf("%d",a[i]);
else printf(" ");
n=n%(int)pow(2,i);
}
}
else
{
n=-n; int temp=0;
for(int i=7;i!=-1;i--) //转换为二进制反码
{
a[i]=n/(int)pow(2,i);
if(a[i]==1) a[i]=0;//取反
else a[i]=1;
n=n%(int)pow(2,i);
}
int carry=0; //进位
for(int i=0;i<8;i++) //转为补码,从低位加1
{
if(i==0){
temp=a[i];
a[i]=(a[i]+1)%2;
carry=(temp+1)/2;
}
else
{
temp=a[i];
a[i]=(a[i]+carry)%2;
carry=(temp+carry)/2; //一定要想好进位怎么算
}
}
for(int i=7;i!=-1;i--)
{
if(a[i]==1) printf("%d",a[i]);
else printf(" ");
}
}
}
int main()
{
int n;int count=0;
FILE *fp;
if ((fp=fopen("2.txt","r"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
while(fscanf(fp,"%d",&n)==1)
{
//scanf("%d",&n);
//printf("%d\n",n);
d_to_b(n);
count++;
if(count%2==0) printf("\n"); //转换完一行
if(count%32==0) printf("\n");//转换完一个汉字
//printf("\n");
}
fclose(fp);
int ans=1;
for(int i=0;i<9;i++)
{
ans*=9;
}
printf("9的9次方=%d",ans);
return 0;
}
运行截图:
答案:387420489
请大声告诉我你写的是81(手动滑稽)
注意:计算机中存的是补码(计算机组成原理讲过),这里只转换成的原码,反码只需对原码取反,补码的话需要加1,可以模拟手算。可以看我第三题的题解,模拟手算乘法。
其他题解:
20180401第九届蓝桥杯省赛c/c++真题题解
有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。