1160: decimal-16 binary problem solving experience

Source: Internet
Author: User

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

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.