加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

poj 1220 NUMBER BASE CONVERSION

发布时间:2020-12-14 03:05:34 所属栏目:大数据 来源:网络整理
导读:NUMBER BASE CONVERSION Time Limit: ?1000MS ? Memory Limit: ?10000K Total Submissions: ?4270 ? Accepted: ?1920 Description Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:? { 0-9,A-Z
NUMBER BASE CONVERSION
Time Limit:?1000MS ? Memory Limit:?10000K
Total Submissions:?4270 ? Accepted:?1920

Description

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:?
{ 0-9,A-Z,a-z }?
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next,when you get back to the original base,you should get the original number.?

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10,B = 11,...,Z = 35,a = 36,b = 37,z = 61 (0-9 have their usual meanings).?

Output

The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.?

Sample Input

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz
2 11011100000100010222220010010110022222001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890

Source

Greater New York 2002

参考了大神的代码自己敲了一遍:=-=

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;


char str[1000];//输入字符串
int start[1000],ans[1000],res[10000]; //被除数,商,余数
int oldbase,newbase;

void change()
{//各个数位还原为数字形式
    int i,len = strlen(str);
    start[0] = len;
    for(i=1;i<= len;i++)
    {
        if(str[i-1] >= '0' && str[i-1] <= '9')
        {
            start[i] = str[i-1] - '0';
        }
        else if (str[i-1]>='a'&&str[i-1]<='z')
            start[i]=str[i-1]-'a'+36;
        else
            start[i]=str[i-1]-'A'+10;
    }
}

void solve()
{
    memset(res,sizeof(res));//余数初始化为空
    int y,i,j;
    //模n取余法,(总体规律是先余为低位,后余为高位)
    while(start[0] >= 1)
    {//只要被除数仍然大于等于1,那就继续“模newbase取余”
        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,sizeof(start));
        //本轮得到的商变为下一轮的被除数
        for(j = i;j <= ans[0];j++)
            start[++start[0]] = ans[j];
        memset(ans,sizeof(ans)); //清除这一轮的商,为下一轮运算做准备
    }
}

void output()
{
    printf("%d %sn%d ",oldbase,str,newbase);
    int i;
    for(i = res[0];i >= 1;--i)
    {
        if(res[i]>=0&&res[i]<=9)
            printf("%d",res[i]);
        else if(res[i]>=10&&res[i]<=35)
            printf("%c",'A'+res[i]-10);
        else printf("%c",'a'+res[i]-36);
    }
    printf("nn");
}

int main()
{
    int n;
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d%d%s",&oldbase,&newbase,str);
        change();
        solve();
        output();
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读