POJ 1220 http://poj.org/problem?id=1220


高精度之间的转换,利用模板即可,可以看的资料里面

代码:


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char str[1000];;
int newBase , oldBase;
int start[1000] , ans[1000] , res[1000];
int getNum(char c){
    if(c >= '0' && c <= '9')
        return c-'0';
    if(c >= 'A' && c <= 'Z')
        return c-'A'+10;
    //if(c >= 'a' && c <= 'z')
        return c-'a'+36;
}
char getChar(int i){
    if(i >= 0 && i<= 9)
        return i+'0';
    if(i >= 10 && i<= 35)
        return i-10+'A';
    //if(i >= 36 && i <= 61)
        return i-36+'a';
}
void change(){
    int i;
    start[0] = strlen(str);
    for(i = 1 ; i <= start[0] ; i++)
        start[i] = getNum(str[i-1]);
}
void solve(){
    memset(res , 0 , sizeof(res));
    int y, i, j;
    while(start[0] >= 1){
        y = 0;
        i = 1;
        ans[0] = start[0];
        while(i <= start[0]){
            y = y*oldBase + start[i];
            ans[i++] = y / newBase;
            y %= newBase;
        }
        res[++res[0]] = y;
        i = 1;
        while(i <= ans[0] && ans[i] == 0)
            i++;
        memset(start , 0 , sizeof(start));
        for(j = i;j <= ans[0] ;j++)
            start[++start[0]] = ans[j];
        memset(ans , 0 ,sizeof(ans));
    }
}
void output(){
    int i;
    for(i = res[0] ; i >= 1 ; i--)
        printf("%c",getChar(res[i]));
    cout<<endl;
}   
int main(){
    int n , i;
    cin>>n;
    for(i = 1 ; i <= n ; i++){
        cin>>oldBase>>newBase;
        cin>>str;
        cout<<oldBase<<" "<<str<<endl;
        cout<<newBase<<" ";
        change();
        solve();
        output();
        if(i != n)
            cout<<endl;
    }
    return 0;
}      
    
    


posted on 2012-06-21 15:56  c语言源码  阅读(273)  评论(0编辑  收藏  举报

导航