算法刷题记录:译码

发布时间 2023-05-30 16:53:13作者: 想个昵称好难ABCD

题目链接

https://ac.nowcoder.com/acm/contest/19306/1046

解题思路:

10进制转x进制,只要反复%x、/x即可。
%x取出末尾的数字,因为末尾的数字已经取出,所以将该数字\
可以一起算也可以循环,取模不会影响除数

AC代码

#include <iostream>

using namespace std;

int T, n;

// 将10进制转化成26进制
int main()
{
    cin >> T;
    while (T -- )
    {
        cin >> n;
        string ans, s;
        cin >> s;
        
        // 00000 00001
        // 00002 17575 11222    -> 5个一组计算
        for (int i = 0; i < n; i += 5)
        {
            int num = 0, bit = 10000;
            for (int j = i; j < i + 5; ++ j)
            {
                num += (s[j] - '0') * bit;
                bit /= 10;
            }

            ans += num / 26 / 26 + 'a';
            ans += num / 26 % 26 + 'a';
            ans += num % 26 + 'a';
        }
        
        cout << ans << endl;
    }
}