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

3438. 数制转换

2022-07-17 21:50:00 Ray.C.L

在这里插入图片描述

思路:将a进制数number转化为10进制(秦九韶),再用短除法转为b进制

代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char itoc(int x){
    
    if(x < 10) return '0' + x;
    return 'A' + x - 10;
}

int ctoi(char x){
    
    if(x <= '9') return x - '0';
    if(x <= 'Z') return x - 'A' + 10;
    return x - 'a' + 10;
}
int main()
{
    
    int n, a, b;
    string number;
    
    cin >> a >> number >> b;
    
    n = 0;
    for(auto c : number) n = n * a + ctoi(c);
    string res;
    while(n) res += itoc(n % b) , n /= b;
    
    reverse(res.begin(), res.end());
    cout << res << endl;
    
    
    return 0;
}
原网站

版权声明
本文为[Ray.C.L]所创,转载请带上原文链接,感谢
https://raycl.blog.csdn.net/article/details/125835157