当前位置:网站首页>3438. 数制转换

3438. 数制转换

2022-08-10 00:30:00 NEFU AB-IN

Powered by:NEFU AB-IN

Link

3438. 数制转换

  • 题意

    求任意两个不同进制非负整数的转换(2 进制 ∼ 16 进制),所给整数在 int 范围内。
    不同进制的表示符号为(0,1,…,9,a,b,…,f)或者(0,1,…,9,A,B,…,F)

  • 思路

    手写两个函数,分别是 整数转字符,字符转整数
    其余正常写就行

  • 代码

    /* * @Author: NEFU AB-IN * @Date: 2022-08-08 00:33:59 * @FilePath: \Acwing\3438\test.cpp * @LastEditTime: 2022-08-09 20:29:03 */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define SZ(X) ((int)(X).size())
    #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0);
    #define DEBUG(X) cout << #X << ": " << X << endl;
    typedef pair<int, int> PII;
    
    const int INF = INT_MAX;
    const int N = 1e6 + 10;
    
    char itoc(int x)
    {
          
        if (x < 10)
            return '0' + x;
        return 'A' + x - 10;
    }
    
    int ctoi(char c)
    {
          
        if (c <= '9')
            return c - '0';
        if (c <= 'Z')
            return c - 'A' + 10;
        return c - 'a' + 10;
    }
    
    signed main()
    {
          
        IOS;
        string n;
        int a, b;
        cin >> a >> n >> b;
    
        // a进制转10进制
        int ans = 0;
        for (auto i : n)
        {
          
            ans = ans * a + ctoi(i);
        }
        // 10进制转b进制
        string res;
        while (ans)
        {
          
            res += itoc(ans % b);
            ans /= b;
        }
        reverse(res.begin(), res.end());
        cout << res << '\n';
        return 0;
    }
    
    
原网站

版权声明
本文为[NEFU AB-IN]所创,转载请带上原文链接,感谢
https://ab-in.blog.csdn.net/article/details/126255206