[洛谷1143]进制转换

第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16)

第二行是一个n进制数,若n>10则用大写字母A~F表示数码10~15,并且该n进制数对应的十进制的值不超过1000000000

第三行也是一个正整数,表示转换之后的数的进制m(2≤m≤16)。

 

先把一个数n进制的数转化成10进制的数,再把一个10进制的数转化成一个m进制的数

 

 

把一个n进制数x转换成10进制,x以字符串形式出现,以数值形式返回

function p(n:longint;s:string):longint;
var i:longint;
begin
    p:=0;
    for i:=1 to length(s) do 
       begin
           p:=p*n;
           if s[i]='A''B'C'D'E'F'.... then Inc(p,ord(s[i])-ord('A')+10)
           else inc(p,ord(s[i])-48);
       end;
end;

 

把一个10进制数x转换成n进制,f是早打好的该进制中的数表

while x<>0 do 
    begin
        inc(k);
        a[k]:=x mod n;
        x:=x div n;
    end;
for i:=1 to k do 
    write(f[a[i]]);

 

 

 

巨恶心无比的代码

const
    two:array[0..1] of char=('0','1');
    three:array[0..2] of char=('0','1','2');
    four:array[0..3] of char=('0','1','2','3');
    five:array[0..4] of char=('0','1','2','3','4');
    six:array[0..5] of char=('0','1','2','3','4','5');
    seven:array[0..6] of char=('0','1','2','3','4','5','6');
    eight:array[0..7] of char=('0','1','2','3','4','5','6','7');
    nine:array[0..8] of char=('0','1','2','3','4','5','6','7','8');
    eleven:array[0..10] of char=('0','1','2','3','4','5','6','7','8','9','A');
    twelve:array[0..11] of char=('0','1','2','3','4','5','6','7','8','9','A','B');
    thirteen:array[0..12] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C');
    fourteen:array[0..13] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D');
    fifteen:array[0..14] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E');
    sixteen:array[0..15] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

var a:array[0..100000] of longint;
    i,j,k,n,x,m:longint;
    y:string;
    ch:char;

procedure shiliu(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 16;
            n:=n div 16;
        end;
    for i:=k downto 1 do
        write(sixteen[a[i]]);
end;
procedure shiwu(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 15;
            n:=n div 15;
        end;
    for i:=k downto 1 do
        write(fifteen[a[i]]);
end;
procedure shisi(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 14;
            n:=n div 14;
        end;
    for i:=k downto 1 do
        write(fourteen[a[i]]);
end;
procedure shisan(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 13;
            n:=n div 13;
        end;
    for i:=k downto 1 do
        write(thirteen[a[i]]);
end;
procedure shier(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 12;
            n:=n div 12;
        end;
    for i:=k downto 1 do
        write(twelve[a[i]]);
end;
procedure shiyi(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 11;
            n:=n div 11;
        end;
    for i:=k downto 1 do
        write(eleven[a[i]]);
end;
procedure jiu(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 9;
            n:=n div 9;
        end;
    for i:=k downto 1 do
        write(nine[a[i]]);
end;
procedure ba(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 8;
            n:=n div 8;
        end;
    for i:=k downto 1 do
        write(eight[a[i]]);
end;
procedure qi(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 7;
            n:=n div 7;
        end;
    for i:=k downto 1 do
        write(seven[a[i]]);
end;
procedure liu(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 6;
            n:=n div 6;
        end;
    for i:=k downto 1 do
        write(six[a[i]]);
end;
procedure wu(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 5;
            n:=n div 5;
        end;
    for i:=k downto 1 do
        write(five[a[i]]);
end;
procedure si(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 4;
            n:=n div 4;
        end;
    for i:=k downto 1 do
        write(four[a[i]]);
end;
procedure san(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 3;
            n:=n div 3;
        end;
    for i:=k downto 1 do
        write(three[a[i]]);
end;
procedure er(n:longint);
begin
    while (n<>0) do
        begin
            inc(k);
            a[k]:=n mod 2;
            n:=n div 2;
        end;
    for i:=k downto 1 do
        write(two[a[i]]);
end;
procedure shi(n:longint);
begin
    writeln(n);
end;
function change(n:longint;s:string):longint;
var i:longint;
begin
    change:=0;
    for i:=1 to length(s) do
        begin
            change:=change*n;
            if (s[i]='A')or(s[i]='B')OR(S[I]='C')OR(S[I]='D')OR(S[I]='E')OR(S[I]='F') then
                change:=change+(ord(s[i])-ord('A')+10)
            else change:=change+(ord(s[i])-48);
        end;
end;
begin
    readln(n);
    readln(y);
    readln(m); 
    if y='0' then
        begin
            writeln(0);
            halt;
        end;
    x:=change(n,y);
    case m of
        2:er(x);
        3:san(x);
        4:si(x);
        5:wu(x);
        6:liu(x);
        7:qi(x);
        8:ba(x);
        9:jiu(x);
        10:shi(x);
        11:shiyi(x);
        12:shier(x);
        13:shisan(x);
        14:shisi(x);
        15:shiwu(x);
        16:shiliu(x);
    end;
end.

 

posted on 2015-11-05 15:34  川汉唐  阅读(768)  评论(0编辑  收藏  举报

导航