M進制轉換爲R進制

#include <iostream>
#include <cstring>
#include<cstdio>
const int maxn = 1000;
char arr[maxn];

using namespace std;

void sol(int n, int r){
    int tot=-1;
    int temp = n;
    memset(arr, 0, sizeof(arr));
    while(temp!=0){
        arr[++tot] = temp%r>9?(temp%r+55):(temp%r+48);
        temp = temp/r;
    }
    for(int i=tot; i>=0; i--){
        cout<<arr[i];
    }
    cout<<endl;
}

int main(){
    int n, r;
    while(cin>>n>>r){
        if(n>=0)
            sol(n, r);
        else{
            cout<<"-";
            sol(-n, r);
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章