Original question
Description
Converts a decimal integer to 16, in the form of 0x, 10~15 is represented by an uppercase letter, a~f.
Input
One integer per line x,0<= x <= 2^31.
Output
Each row outputs a corresponding eight-bit hexadecimal integer, including the leading 0.
Sample Input
01023
Sample Output
0x000000000x000003ff
Analysis: Conversion from decimal to n binary is the lowest bit of x%n
The last one for x/n%n
Another one for x/n/n&n.
So
My Code
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[8];intMain () {inti =7; Long Longb; while(Cin >>b) {i=7; Memset (A,'0',sizeof(a)); while(b! =0&& i>0){ intx = b% -; b= b/ -; if(x >=Ten) {A[i]= x + -; I--; } Else{A[i]= x + -; I--; }} cout<<"0x"; for(intj =0; J <8; J + +) {cout<<A[j]; if(J = =7) cout <<Endl; } } return 0;}View Code
1160: decimal-16 binary problem solving experience