Java implements n sum decomposition of several 2

Source: Internet
Author: User

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 );}}

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.