对电影《天才枪手》记答案方法的优化丨附转换代码

emmmm。。。好像好久没有更新了,没关系,WordPress可以手动更改发布时间23333,所以时间就被我改成电影上映的日期咯~

事情是这样的,看完电影后刷知乎,刷到这样一个问题:如何评价电影《天才枪手》?,好像每一部电影上映,或者有点啥事件发生的话,知乎好像都会有个“如何评价XXX?”的问题出现。。。

好啦言归正传,在这个题目的下边,我发现这样一个回答:

我就搞不懂为什么他们记个答案这么辛苦…00表示A,01表示B,10表示C,11表示D。一份答案,比如:CAADB ADDAC BCDAB DDCCA,换成2进制:1000 0011 0100 1111 0010 0110 1100 0111 1110 1000。每4位转换成16进制,834F 26C7 E8,长度缩短一倍,如果你还觉得长,可以采用各种数据压缩算法,完全可以达到更短。看他们一个个记的头都要炸了表示很无语….

知乎:@爱熬夜的程序猿

作为一只酷爱敲简单代码的半程序员(也就是我(雾。。。))来说,当然是实现它啦!!!

直接上代码!!!

[cc lang = “cpp” escaped = “true”]
#include

using namespace std;

int main() {
string options;
int bi[100];
int index = 0;
//input
getline(cin, options);
for (int i = 0; i < options.length(); i++) {
if (options[i] == ‘A’) { bi[index++] = 0; bi[index++] = 0;}
else if (options[i] == ‘B’) { bi[index++] = 0; bi[index++] = 1;}
else if (options[i] == ‘C’) { bi[index++] = 1; bi[index++] = 0;}
else if (options[i] == ‘D’) { bi[index++] = 1; bi[index++] = 1;}
else continue;
}
//transform and output
int mul = 8, temp = 0, count = 0;
for (int i = 0; i < index; i++) {
if (count == 5) {cout << ‘ ‘; count = 0;}
temp += bi[i] * mul;
mul /= 2;
if (!mul) {
if (temp == 15) {cout << ‘F’; count++;}
else if (temp == 14) {cout << ‘E’; count++;}
else if (temp == 13) {cout << ‘D’; count++;}
else if (temp == 12) {cout << ‘C’; count++;}
else if (temp == 11) {cout << ‘B’; count++;}
else if (temp == 10) {cout << ‘A’; count++;}
else {cout << temp; count++;}
mul = 8;
temp = 0;
}
}
cout << endl;
return 0;
}
[/cc]

可能有朋友没明白,这个场景是,小琳和小巴答完题需要记答案时,可以使用这种方法,并把十六进制字符串发送给富二代朋友们。
所以这里与上述代码对应的,还有反向转换成答案,我就不写了,交给你们咯~



Leave a Reply

Your email address will not be published. Required fields are marked *