Files
python/GESP/Level4/GESP202309/test.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

61 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N], al;
string s[N];
int sl;
// 2进制转16进制 0011 1100
char get(string x) { // x 必须是4位一组的01数字 --> E B A
int sum = 0;
int base = 1;
for (int i = x.size() - 1; i >= 0; i--) {
sum += (x[i] - '0') * base;
base *= 2;
}
if (sum >= 10)
return (char)(sum - 10 + 'A');
return (char)(sum + '0');
}
// x=0000 0111
// 0E
string getHex(string x) { // 0E FE AC
string a = x.substr(0, 4); // 前4个
string b = x.substr(4, 4); // 后4个
string c;
c.push_back(get(a));
c.push_back(get(b));
return c;
}
int main() {
long long n = 987654321012345678;
while (n) {
int x = n % 2;
n /= 2;
a[++al] = x;
}
// 需要添加若干0来完成补位,需要添加多少个?
int x = 7 - al % 7;
if (al % 7 == 0)
x = 0;
al += x;
for (int i = al; i >= 1; i -= 7) { // 每一组
string q = "";
if (i == 7)
q = q + "1";
else
q = q + "0";
for (int j = 0; j < 7; j++) // 每一个
q = q + to_string(a[i - j]);
s[++sl] = q;
}
for (int i = sl; i; i--)
cout << getHex(s[i]) << " ";
return 0;
}