进制转换

阅读时长: 1 分钟 110 字

十进制转其他进制

#include 
using namespace std;

string s="0123456789ABCDEF";

void solve(int x,int m)
{
    // 递归
    if(x/m) solve(x/m,m);
    cout st;
    while (x)
    {
        st.push(x%m);
        x/=m;
    }
    while (!st.empty()){
        cout>x>>m;
    solve(x,m);
    return 0;
}

其他进制转十进制

#include 
using namespace std;

int n, m;
string es = "0123456789ABCDEF";

int main()
{
    cin >> n >> m;
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        ans *= m;
        ans += es.find(s[i]);
    }
    cout << ans << '\n';
    return 0;
}
本网站已运行: 计算中...
备案 赣公网安备36072502000191 | 赣ICP备2024040374号
使用 Hugo 构建 · 主题 Stack 由 Jimmy 设计