Java implements several n and decomposition of 2, for example, 15 is decomposed into 2 three times + 2 2 times + 2 1 times + 2 0 times, for example 47 is decomposed: 2 5 times + 2 3 times + 2 2 times + 2 1 times + 2 0 times this is a problem encountered in permission allocation, such as permission is 47, for the above decomposition, I want 0, 1, 2, 3, 5, for example 9. I want 0, 3 to be the Npower sum of 2, and find a program to implement the n of any given 2 (n is not repeated) the sub-vertex splits the sub-vertex and extracts its number of times into an array. Thank you for your urgent use! For example, 17 is only [] 0 times of 2 and 4 times of 2 respectively.
Question added:
Lzhuan665: Could you elaborate on your ideas? I just want to convert them into binary files. How can I do it next? Can you tell me if I am a beginner? Could you help me?
Best Answer
Import java. util. collections;
Public class Binary {
Public static void main (String [] args ){
System. out. println ("Enter the number you want to convert :");
Running in = new Processing (System. in );
Int num = in. nextInt ();
String numStr = Integer. toBinaryString (num );
System. out. println (numStr );
StringBuffer bf = new StringBuffer ();
For (int I = 0; I <numStr. length (); I ++ ){
If (numStr. charAt (I )! = '0') {bf. append (numStr. length ()-1-i );
}
}
Int arr [] = new int [bf. length ()];
For (int I = 0; I <bf. length (); I ++)
{
Arr [I] = bf. charAt (I)-'0 ';
System. out. print (arr [I]);
}
System. out. println (bf. toString ());}}
Let me give you an idea. You can convert it to a binary system to public class TestBin {public static void main (String [] args) {String s = Integer. toBinaryString (47); System. out. println (s );}}