Dado un número entero N, la tarea es encontrar los números que, elevados a la potencia de 2 y finalmente sumados, dan el número entero N.
Ejemplo :
C++
// CPP program to find the // blocks for given number. #include <bits/stdc++.h> using namespace std; void block(long int x) { vector<long int> v; // Converting the decimal number // into its binary equivalent. cout << "Blocks for " << x << " : "; while (x > 0) { v.push_back(x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.size(); i++) { if (v[i] == 1) { cout << i; if (i != v.size() - 1) cout << ", "; } } cout << endl; } // Driver Function int main() { block(71307); block(1213); block(29); block(100); return 0; }
Java
// Java program to find the // blocks for given number. import java.util.*; class GFG { static void block(long x) { ArrayList<Integer> v = new ArrayList<Integer>(); // Convert decimal number to // its binary equivalent System.out.print("Blocks for "+x+" : "); while (x > 0) { v.add((int)x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.size(); i++) { if (v.get(i) == 1) { System.out.print(i); if (i != v.size() - 1) System.out.print( ", "); } } System.out.println(); } // Driver Code public static void main(String args[]) { block(71307); block(1213); block(29); block(100); } } // This code is contributed by Arnab Kundu.
Python3
# Python3 program to find the # blocks for given number. def block(x): v = [] # Converting the decimal number # into its binary equivalent. print ("Blocks for %d : " %x, end="") while (x > 0): v.append(int(x % 2)) x = int(x / 2) # Displaying the output when # the bit is '1' in binary # equivalent of number. for i in range(0, len(v)): if (v[i] == 1): print (i, end = "") if (i != len(v) - 1): print (", ", end = "") print ("\n") block(71307) block(1213) block(29) block(100) # This code is contributed by Manish # Shaw (manishshaw1)
C#
// C# program to find the // blocks for given number. using System; using System.Collections.Generic; class GFG { static void block(long x) { List<int> v = new List<int>(); // Convert decimal number to // its binary equivalent Console.Write("Blocks for " + x + " : "); while (x > 0) { v.Add((int)x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.Count; i++) { if (v[i] == 1) { Console.Write(i); if (i != v.Count - 1) Console.Write(", "); } } Console.WriteLine(); } // Driver Code here public static void Main() { block(71307); block(1213); block(29); block(100); } } // This code is contributed by Ajit.
PHP
<?php // PHP program to find the // blocks for given number. function block($x) { $v = array(); // Convert decimal number to // its binary equivalent echo 'Blocks for ' .$x.' : '; while ($x > 0) { array_push($v,intval($x % 2)); $x = intval($x / 2); } // Displaying the output when // the bit is '1' in binary // equivalent of number. for ($i = 0; $i < sizeof($v); $i++) { if ($v[$i] == 1) { print $i; if ($i != sizeof($v) - 1) echo ', '; } } echo "\n"; } // Driver Code block(71307); block(1213); block(29); block(100); // This code is contributed // by Manish Shaw (manishshaw1) ?>
Javascript
<script> // Javascript program to find the // blocks for given number. function block(x) { let v = []; // Convert decimal number to // its binary equivalent document.write("Blocks for " + x + " : "); while (x > 0) { v.push(x % 2); x = parseInt(x / 2, 10); } // Displaying the output when // the bit is '1' in binary // equivalent of number. for(let i = 0; i < v.length; i++) { if (v[i] == 1) { document.write(i); if (i != v.length - 1) document.write(", "); } } document.write("</br>"); } // Driver code block(71307); block(1213); block(29); block(100); // This code is contributed by mukesh07 </script>
Publicación traducida automáticamente
Artículo escrito por sumit.chauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA