Converts a decimal non-negative integer with a length of more than 100 digits to a binary number (big data processing)

Source: Internet
Author: User
Tags decimal to binary

Description:
Converts a decimal non-negative integer with a length of more than 100 digits into a binary number.

Input:
Multiple groups of data, each behavior has a decimal non-negative integer of no more than 30 digits.
(Note that the number of decimal numbers may be 30, not an integer of 30bits)

Output:
The number of binary numbers output for each row.

Sample input:
0
1
3
8 sample output:
0
1
11
1000 analysis: this number should not be stored in an int type variable. Like the previous posts, it is also processed using vectors. The processed results are stored in binary, the final output is the result we want.

The C ++ code is as follows:


[Cpp]
// W397090770
// Wyphao.2007@163.com
// 2012.07.14
# Include <iostream>
# Include <string>
# Include <vector>
 
Using namespace std;
 
// Convert decimal to Binary
Void dec2bin (string s ){
Int sum (0 );
Vector <char> v;
Int I, j;
String binary;
Char ch;

// Store each digit in v
For (int I (s. length ()-1); I> = 0; -- I ){
V. insert (v. begin (), s [I]-'0 ');
}

// Calculate binary
While (1 ){
J = v. size ()-1;
Ch = v [j] % 2 + '0 ';
Binary. insert (binary. begin (), 1, ch );
For (sum = 0, I = 0; I <= j; I ++ ){
If (I <j ){
// V [I + 1] + = (v [I] % 2) * 10;
V [I + 1] + = (v [I]-(v [I]> 1) <1) * 10;
}
// V [I]/= 2;
V [I]> = 1;
Sum + = v [I];
}
If (sum = 0 ){
Break;
}
}
Cout <binary <endl;
}
 
Int main (){
String dec;
While (cin> dec ){
Dec2bin (dec );
}
Return 0;
}

// W397090770
// Wyphao.2007@163.com
// 2012.07.14
# Include <iostream>
# Include <string>
# Include <vector>

Using namespace std;

// Convert decimal to Binary
Void dec2bin (string s ){
Int sum (0 );
Vector <char> v;
Int I, j;
String binary;
Char ch;
 
// Store each digit in v
For (int I (s. length ()-1); I> = 0; -- I ){
V. insert (v. begin (), s [I]-'0 ');
}
 
// Calculate binary
While (1 ){
J = v. size ()-1;
Ch = v [j] % 2 + '0 ';
Binary. insert (binary. begin (), 1, ch );
For (sum = 0, I = 0; I <= j; I ++ ){
If (I <j ){
// V [I + 1] + = (v [I] % 2) * 10;
V [I + 1] + = (v [I]-(v [I]> 1) <1) * 10;
}
// V [I]/= 2;
V [I]> = 1;
Sum + = v [I];
}
If (sum = 0 ){
Break;
}
}
Cout <binary <endl;
}

Int main (){
String dec;
While (cin> dec ){
Dec2bin (dec );
}
Return 0;
}
Running result:

 

I searched the internet and saw someone wrote a version in java. The function is very simple:


[Java]
Import java. math. BigInteger;
Import java. util. collections;
 
Public class Q1138 {
Public static void main (String [] args ){
Cin = new partition (System. in );
While (cin. hasNext ()){
System. out. println (new BigInteger (cin. next (). toString (2 ));
}
}
}

Import java. math. BigInteger;
Import java. util. collections;

Public class Q1138 {
Public static void main (String [] args ){
Cin = new partition (System. in );
While (cin. hasNext ()){
System. out. println (new BigInteger (cin. next (). toString (2 ));
}
}
} Result:

It can be seen from the above that the java language is indeed very convenient and powerful, but the java code Runtime is obviously longer than the implementation of C ++.


Author: w397090770
 

Related Article

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.