2259: N-Converter
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
5s | 32768K | 249 | 127 | Standard |
You must be familiar with conversion of an integer from base 10 to another base. For example, 8064 of base 10 is equivalent to 17600 of base 8, because
8064 = 1*8^4 + 7*8^3 + 6*8^2 + 0*8^1 + 0*8^0
However, this problem is not as easy as the one that is to convert an integer of base 10 to other positive bases.
Your task is to convert an integer of base 10 to negative bases. For example, 15 of base 10 can be converted to 10011 of base -2, because
15 = 1*(-2)^4 + 0*(-2)^3 + 0*(-2)^2 + 1*(-2)^1 + 1*(-2)^0
Similarly, -15 of base 10 can be converted to 110001 of base -2, because
-15 = 1*(-2)^5 + 1*(-2)^4 + 0*(-2)^3 + 0*(-2)^2 + 0*(-2)^1 + 1*(-2)^0
Given an integer of base 10 and a negative integer B, you are to write a program to convert the first integer to the equivalent integer of base B. If B is more than 10, use A, B, C...Z to represent the additional digits.
It is guaranteed that the absolute of N is maneuverable by a 30-bit integer, and the absolute of B is less than 37.
8064 = 1*8^4 + 7*8^3 + 6*8^2 + 0*8^1 + 0*8^0
However, this problem is not as easy as the one that is to convert an integer of base 10 to other positive bases.
Your task is to convert an integer of base 10 to negative bases. For example, 15 of base 10 can be converted to 10011 of base -2, because
15 = 1*(-2)^4 + 0*(-2)^3 + 0*(-2)^2 + 1*(-2)^1 + 1*(-2)^0
Similarly, -15 of base 10 can be converted to 110001 of base -2, because
-15 = 1*(-2)^5 + 1*(-2)^4 + 0*(-2)^3 + 0*(-2)^2 + 0*(-2)^1 + 1*(-2)^0
Given an integer of base 10 and a negative integer B, you are to write a program to convert the first integer to the equivalent integer of base B. If B is more than 10, use A, B, C...Z to represent the additional digits.
INPUT
The input contains many test cases, each of which occupies a single line consisting of two integers N and B, representing the integer to be converted and the negative base N. You should continue to proceed until your program encounter the end-of-file character.It is guaranteed that the absolute of N is maneuverable by a 30-bit integer, and the absolute of B is less than 37.
OUTPUT
For each test case, print in a single line the equivalent integer of base B with N.SAMPLE INPUT
15 -2 -15 -2 -25000 -16
SAMPLE OUTPUT
10011 110001 7FB8
Problem Source: 4th JLU Programming Contest
#include<stdio.h>
int abs(int x){return x<0?-x:x;}
int a[10000];
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)==2)//m<0
{
int l=0;
while(n)
{
if(n%m&&n<0)
{
a[l++]=n-(n/m+1)*m;
n=n/m+1;
}
else
{
a[l++]=abs(n%m);
n/=m;
}
}
for(i=l-1;i>=0;i--)
{
if(a[i]>=10) printf("%c",a[i]-10+'A');
else printf("%d",a[i]);
}
printf("/n");
}
return 0;
}