HDU 2031 进制转换

自低位至高位生成每一位

考虑负数很烦

打个负号之后看成正数即可

注意输出顺序

#include <iostream>

using namespace std;

const int MAXL=111111;

int N, R;
int ANS[MAXL], L;

void show(int a){
    if(a<10)    cout << a;
    else    cout << (char)(a+'A'-10);
}

int main(){
    ios_base::sync_with_stdio(false);
    
    while(cin >> N >> R){
        if(N<0){
            cout << "-", N=-N;
        }
        L=0;
        while(N>0){
            ANS[++L]=N%R;
            N/=R;
        }
        for(int i=L;i>=1;--i)
            show(ANS[i]);
        cout << endl;
    }
    
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Pickupwin/p/8460331.html