Question one hundred and forty-one: hexadecimal conversion

Source: Internet
Author: User

PS: when n = 0, I did not consider this as an exception!


Description

Input a decimal number N and convert it to the R hexadecimal number output.

Input

The input data contains multiple test instances. Each test instance contains two integers, N (32-bit integer) and R (2 <= R <= 16, R <> 10 ).

Output

Number of converted outputs for each test instance. Each output occupies one row. If the R value is greater than 10, the corresponding digital rule is in hexadecimal notation (for example, 10 is represented by A, and so on ).

Sample Input


7 2
23 12
-4 3
Sample Output


111
1B
-11


[Plain]
# Include <stdio. h>
 
Int main ()
{
Int I;
Int j;
Int n;
Int m;
Int l;
Int flag;
Char num [1001];
 
While (scanf ("% d", & n, & m )! = EOF & (m> = 2 & m <= 16 ))
{
J = 0;
Flag = 1;
If (n <0)
{
Flag = 0;
N =-n;
}
 
If (n = 0)
{
Num [j ++] = n + '0 ';
}
If (m> = 2 & m <10)
{
While (n)
{
L = n % m;
Num [j ++] = l + '0 ';
N = n/m;
}
}
Else if (m> = 10 & m <= 16)
{
While (n)
{
L = n % m;
If (l> = 10 & l <= 15)
{
Num [j ++] = 'A' + l-10;
}
Else
{
Num [j ++] = l + '0 ';
}
N = n/m;
}
}
 
If (flag = 0)
{
Printf ("-");
}
 
For (I = J-1; I> = 0; I --)
{
Printf ("% c", num [I]);
}
 
Printf ("\ n ");
}
}

# Include <stdio. h>

Int main ()
{
Int I;
Int j;
Int n;
Int m;
Int l;
Int flag;
Char num [1001];

While (scanf ("% d", & n, & m )! = EOF & (m> = 2 & m <= 16 ))
{
J = 0;
Flag = 1;
If (n <0)
{
Flag = 0;
N =-n;
}

If (n = 0)
{
Num [j ++] = n + '0 ';
}
If (m> = 2 & m <10)
{
While (n)
{
L = n % m;
Num [j ++] = l + '0 ';
N = n/m;
}
}
Else if (m> = 10 & m <= 16)
{
While (n)
{
L = n % m;
If (l> = 10 & l <= 15)
{
Num [j ++] = 'A' + l-10;
}
Else
{
Num [j ++] = l + '0 ';
}
N = n/m;
}
}

If (flag = 0)
{
Printf ("-");
}

For (I = J-1; I> = 0; I --)
{
Printf ("% c", num [I]);
}

Printf ("\ n ");
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.