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