2259: N-Converter

2259: N-Converter


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
5s32768K249127Standard
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.

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值