题解:P14602 [NWRRC 2025] Compact Encoding

· · 题解

一道简单的模拟题。

代码实现

n 的二进制表示从高位到低位每 7 位分一组除最后一组其余每组最高位设为 1 ,然后从低位提取 7 位块,再逆序输出得到大端序。

::::::info[细节]

代码

#include <iostream>
using namespace std;

int a[5];
int main() 
{
    int n;
    cin >> n;
    if (n == 0) 
    {
        cout << 0 << endl;
        return 0;
    }
    int cnt = 0;
    while (n > 0) 
    {
        a[cnt] = n % 128;
        n /= 128;
        cnt++;
    }
    for (int i = cnt - 1; i >= 0; i--) 
    {
        if (i == 0) 
        {
            cout << a[i] << " ";
        } 
        else 
        {
            cout << a[i] + 128 << " ";
        }
    }
    return 0;
}