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

 

posted @ 2018-02-22 22:21  Pickupwin  阅读(193)  评论(0编辑  收藏  举报