Binary conversion (large number of decimal conversions into binary)

Source: Internet
Author: User

title describes converting a decimal non-negative integer up to 30 digits to a binary output. Input Description:

Multiple sets of data, each behaving as a decimal non-negative integer with a length not exceeding 30 bits.
(Note that the number of 10 binary digits may be 30, not a 30bits integer)
Output Description:
The binary number that corresponds to the output of each row.
Example 1 input
0
1
3
8
Output
0
1
1000

#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;

int main () {
	char str[32];
    int Input[32],len;
    int output[100];
    while (~SCANF ("%s", &str)) {
		len=strlen (str);
        for (int i=0;i<len;i++) {
            input[i] = str[i]-' 0 ';
        }
        memset (output,0,sizeof (output));
        int sum = 1,d = 0, k = 0;
        while (sum) {
            sum = 0;
            for (int i=0;i<len;i++) {
                d = input[i]/2;
                sum + = D;
                if (i = = (len-1)) {
                    output[k++] = input[i]% 2;
                }
                else
                    input[i+1] + = (input[i]%2) *10;
                Input[i] = D;
            }
        }
        if (k = = 0) 
            cout<<0;
        else{for
            (int i=k-1;i>=0;i--)
                cout<<output[i];
        }
        cout<<endl;
    }
    return 0;
}



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.