4355: GESP C++ 四级 4变长编码202309
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:4
解决:3
题目描述

样例输入 复制
0
样例输出 复制
00
提示
#include <iostream>
using namespace std;
void output_digit(int d) {
if (d >= 10)
cout << (char)('A' + d - 10);
else
cout << (char)('0' + d);
}
void output_code(int s) {
output_digit(s >> 4);
output_digit(s & 0x0f);
}
int main() {
long long n = 0;
cin >> n;
int split[10];
int l = 0;
if (n==0) {
cout<<"00";
return 0;
}
while (n > 0) {
split[l] = (int)(n & 0x7f);
n >>= 7;
l++;
}
for (int i = 0; i < l - 1; i++)
split[i] |= 0x80;
output_code(split[0]);
for (int i = 1; i < l; i++) {
cout << " ";
output_code(split[i]);
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
unsigned long long N;
cin >> N;
vector<unsigned char> bytes;
// 处理 N = 0 的情况
if (N == 0) {
bytes.push_back(0);
} else {
while (N > 0) {
unsigned char byte = N & 0x7F; // 取低 7 位
N >>= 7;
if (N > 0) {
byte |= 0x80; // 不是最后一组,最高位置 1
}
bytes.push_back(byte);
}
}
// 输出
for (size_t i = 0; i < bytes.size(); ++i) {
if (i > 0) cout << " ";
cout << hex << uppercase << setw(2) << setfill('0') << (int)bytes[i];
}
cout << endl;
return 0;
}