Last active
August 29, 2015 14:14
-
-
Save 02015678/a5f8bc34146af628a310 to your computer and use it in GitHub Desktop.
Translation between different Positional notations 负进制转换 NOIP 2000-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://joeyaochou.wordpress.com/2015/01/23/translation-between-different-positional-notations-%E8%B4%9F%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2-noip-2000-1/ | |
const | |
maxn=50;{数组限制} | |
ch:string[20]='0123456789ABCDEFGHIJ';{A=10 B=11 ...} | |
var | |
i,j,n,m,k:longint; | |
bit:array[0..maxn] of longint; | |
{纪录每一位的 纯数值(A B...暂用数值表示)} | |
begin | |
while not eof(input) do | |
begin | |
readln(n,m); | |
i:=0; k:=n; | |
while (k<0)or(k>=-m) do | |
{如果0<k<-m,那么该十进制数字直接是m进制数当位数字} | |
begin | |
bit[i]:=k mod m;{求m进制数的第i位数值} | |
k:=k div m; | |
if bit[i]<0 then | |
begin | |
bit[i]:=bit[i]-m;{该位数值不足0...} | |
k:=k+1;{...从上一位借位} | |
end; | |
inc(i); | |
end; | |
bit[i]:=k; | |
for j:=i downto 0 do | |
write(ch[bit[j]+1]); | |
{将每一位的数值转换成符号 1=1 2=2 .. A=10 B=11..} | |
writeln; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment