Java 7, 541 byte
import java.util.*;List l=new ArrayList(),L=new ArrayList();String c(int n){l.add(x(n));return a(n+" ",l,n);}String a(String r,List q,Integer n){boolean e=q.equals(l),E=q.equals(L);if(e)L.clear();else l.clear();for(String i:new ArrayList<String>(q)){int s=i.length()/2,a=n.parseInt(i.substring(0,s),2),z=n.parseInt(i.substring(s),2);r+=a+" "+z+" ";if(e&a>1)L.add(x(a));if(e&z>1)L.add(x(z));if(E&a>1)l.add(x(a));if(E&z>1)l.add(x(z));}if(e&L.size()>0)r=a(r,L,n);if(E&l.size()>0)r=a(r,l,n);return r;}String x(Integer n){return n.toString(n,2);}
Mantenere l'ordine originale mi ha fregato alla grande, altrimenti sarebbe solo un semplice ciclo e principio di chiamata ricorsiva. Tuttavia, sfida divertente da capire mantenendo l'ordine.
Spiegazione:
import java.util.*; // Required import for List and Array List
List l=new ArrayList(),L=new ArrayList();
// Two Lists on class-level
String c(int n){ // Method (1) with integer parameter and String return-type
l.add(x(n)); // Start by adding the binary-String of the input integer to list `l`
return a(n+" ",l,n); // And let the magic begin in method `a` (2)
} // End of method (1)
String a(String r,List q,Integer n){ // Method (2) with a bunch of parameters and String return-type
boolean e=q.equals(l),E=q.equals(L); // Determine which of the two class-level Lists the parameter-List is
if(e) // If it's `l`:
L.clear(); // Empty `L`
else // If it's `L` instead:
l.clear(); // Empty `l`
for(String i:new ArrayList<String>(q)){
// Loop over the input list (as new ArrayList to remove the reference)
int s=i.length()/2, // Get the length of the current item in the list divided by 2
// NOTE: Java automatically floors on integer division,
// which is exactly what we want for the splitting of odd-length binary-Strings
a=n.parseInt(i.substring(0,s),2), // Split the current binary-String item in halve, and convert the first halve to an integer
z=n.parseInt(i.substring(s),2); // And do the same for the second halve
r+=a+" "+z+" "; // Append the result-String with these two integers
if(e&a>1) // If the parameter List is `l` and the first halve integer is not 0:
L.add(x(a)); // Add this integer as binary-String to list `L`
if(e&z>1) // If the parameter List is `l` and the second halve integer is not 0:
L.add(x(z)); // Add this integer as binary-String to List `L`
if(E&a>1) // If the parameter List is `L` and the first halve integer is not 0:
l.add(x(a)); // Add this integer as binary-String to List `l`
if(E&z>1) // If the parameter List is `L` and the second halve integer is not 0:
l.add(x(z)); // Add this integer as binary-String to List `l`
} // End of loop
if(e&L.size()>0) // If the parameter List is `l` and List `L` now contains any items:
r=a(r,L,n); // Recursive call with List `L` as parameter
if(E&l.size()>0) // If the parameter List is `L` and List `l` now contains any items:
r=a(r,l,n); // Recursive call with List `l` as parameter
return r; // Return the result-String with the now appended numbers
} // End of method (2)
String x(Integer n){ // Method (3) with Integer parameter and String return-type
return n.toString(n,2); // Convert the integer to its Binary-String
} // End of method (3)
Codice di prova:
Provalo qui.
import java.util.*;
class M{
List l=new ArrayList(),L=new ArrayList();String c(int n){l.add(x(n));return a(n+" ",l,n);}String a(String r,List q,Integer n){boolean e=q.equals(l),E=q.equals(L);if(e)L.clear();else l.clear();for(String i:new ArrayList<String>(q)){int s=i.length()/2,a=n.parseInt(i.substring(0,s),2),z=n.parseInt(i.substring(s),2);r+=a+" "+z+" ";if(e&a>1)L.add(x(a));if(e&z>1)L.add(x(z));if(E&a>1)l.add(x(a));if(E&z>1)l.add(x(z));}if(e&L.size()>0)r=a(r,L,n);if(E&l.size()>0)r=a(r,l,n);return r;}String x(Integer n){return n.toString(n,2);}
public static void main(String[] a){
M m=new M();
System.out.println(m.c(255));
m.l.clear();
m.L.clear();
System.out.println(m.c(225));
m.l.clear();
m.L.clear();
System.out.println(m.c(32));
}
}
Produzione:
255 15 15 3 3 3 3 1 1 1 1 1 1 1 1
225 14 1 3 2 1 1 1 0
32 4 0 1 0
0
s quando la lunghezza è dispari?