Binary conversion
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 30304 Accepted Submission (s): 16811
Problem description Enter a decimal number n to convert it to the R-binary output.
Input data contains multiple test instances, each containing two integer n (32-bit integers) and R (2<=r<=16, r<>10).
Output outputs the converted number for each test instance, with one row for each output. If R is greater than 10, then the corresponding number rules refer to 16 binary (for example, 10 is represented by a, and so on).
Sample Input7 2 23 12-4 3
Sample output1111b-11 This topic mainly examines the decimal conversion to other binary, using the decimal number is short except the binary to be converted each time the record remainder, the final reverse output remainder
#include <stdio.h> #include <string.h>int main () {int N,m,j,i,s,y,l2;char b[100];while (scanf ("%d%d",& N,&M)!=eof) {Y=0;l2=0;j=1;s=0;while (n!=0) {if (n<0) //Consider n as a negative case {s=n;n=-n;} y=n%m; The remainder n=n/m after each short m ; Ask for the quotient if (y>=10) {l2=j) after each short removal of m; Record string length b[j]=y+55;//converts a number to character J + +;} else { l2=j; b[j]=y+48; j + +; }}if (s<0) printf ("-"); for (i=l2;i>=1;i--) {
HDOJ 2031 Binary Conversion