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