随笔-68  评论-10  文章-0  trackbacks-0
题目链接:http://poj.org/problem?id=1220
#include<iostream>
#include
<string>
using namespace std;
string idx = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

//将字符转换为对应的数字 
inline int getValue(char ch)
{
    
if(ch >= '0' && ch <= '9'return ch - '0';
    
else if(ch >= 'A' && ch <= 'Z'return ch - 'A' + 10;
    
else return ch - 'a' + 36;
}


//将s由from进制转换为to进制 
string change(string s, int from, int to)
{
    
string res;
    
int sum, remainder;
    
do
    
{
        remainder 
= sum = 0;
        
for(int i = 0; i < s.size(); ++i)
        
{
            remainder 
= remainder * from + getValue(s[i]);
            s[i] 
= idx[remainder / to];
            sum 
+= getValue(s[i]);
            remainder 
%= to;
        }

        res 
= idx[remainder] + res;
    }
while(sum);
    
return res;
}


int main()
{
    
string s;
    
int t, from, to;
    cin 
>> t;
    
while(t--)
    
{
        cin 
>> from >> to >> s;
        cout 
<< from << ' ' << s << endl;
        cout 
<< to << ' ' << change(s, from, to) << endl << endl;
    }

    
return 0;
}

posted on 2011-10-21 15:47 wuxu 阅读(245) 评论(0)  编辑 收藏 引用 所属分类: 高精度

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理