Dados tres enteros no negativos x , y y bind , la tarea es imprimir todos los enteros poderosos ? encuadernado en orden ordenado.
Un entero poderoso es de la forma x i + y j para todo i, j ? 0 _
Ejemplos:
Entrada: x = 3, y = 5, límite = 10
Salida: 2 4 6 8 10
3 0 + 5 0 = 1 + 1 = 2
3 0 + 5 1 = 1 + 5 = 6
3 1 + 5 0 = 3 + 1 = 4
3 1 + 5 1 = 3 + 5 = 8
3 2 + 5 0 = 9 + 1 = 10Entrada: x = 2, y = 3, límite = 10
Salida: 2 3 4 5 7 9 10
Enfoque: inicialice i = 0 para el ciclo externo y j = 0 para el ciclo interno, si x i = límite , entonces salga del ciclo externo (ya que agregar cualquier potencia de y lo hará fuera del límite). Si x i + y j > enlazado , salga del ciclo interno y, en cualquier otra iteración del ciclo interno, guarde x i + y j en un conjunto para mantener una lista distinta y ordenada de los enteros poderosos. Imprime el contenido del conjunto al final.
Para evitar calcular las potencias de y una y otra vez, todas las potencias de yse puede precalcular y almacenar en un vector .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print powerful integers void powerfulIntegers(int x, int y, int bound) { // Set is used to store distinct numbers // in sorted order set<int> s; vector<int> powersOfY; int i; // Store all the powers of y < bound in a vector // to avoid calculating them again and again powersOfY.push_back(1); for (i = y; i < bound && y!= 1; i = i * y) powersOfY.push_back(i); i = 0; while (true) { // x^i int xPowI = pow(x, i); for (auto j = powersOfY.begin(); j != powersOfY.end(); ++j) { int num = xPowI + *j; // If num is within limits // insert it into the set if (num <= bound) s.insert(num); // Break out of the inner loop else break; } // Adding any number to it // will be out of bounds if (xPowI >= bound || x == 1) break; // Increment i i++; } // Print the contents of the set set<int>::iterator itr; for (itr = s.begin(); itr != s.end(); itr++) { cout << *itr << " "; } } // Driver code int main() { int x = 2, y = 3, bound = 10; // Print powerful integers powerfulIntegers(x, y, bound); return 0; }
Java
// Java implementation of the approach import java.util.*; import java.lang.Math; class GfG { // Function to print powerful integers static void powerfulIntegers(int x, int y, int bound) { // Set is used to store distinct numbers // in sorted order Set<Integer> s = new HashSet<>(); ArrayList<Integer> powersOfY = new ArrayList<>(); int i; // Store all the powers of y < bound in a vector // to avoid calculating them again and again powersOfY.add(1); for (i = y; i < bound && y != 1; i = i * y) powersOfY.add(i); i = 0; while (true) { // x^i int xPowI = (int)Math.pow((double)x, (double)i); for (int j = 0; j < powersOfY.size(); ++j) { int num = xPowI + powersOfY.get(j); // If num is within limits // insert it into the set if (num <= bound) s.add(num); // Break out of the inner loop else break; } // Adding any number to it // will be out of bounds if (xPowI >= bound || x == 1) break; // Increment i i++; } // Print the contents of the set Iterator itr = s.iterator(); while(itr.hasNext()) { System.out.print(itr.next() + " "); } } // Driver code public static void main(String []args) { int x = 2, y = 1, bound = 10; // Print powerful integers powerfulIntegers(x, y, bound); } }
Python3
# Python3 implementation of the approach # Function to print powerful integers def powerfulIntegers(x, y, bound) : # Set is used to store distinct # numbers in sorted order s = set() powersOfY = [] # Store all the powers of y < bound # in a vector to avoid calculating # them again and again powersOfY.append(1) i = y while i < bound and y!=1 : powersOfY.append(i) i *= y i = 0 while (True) : # x^i xPowI = pow(x, i) for j in powersOfY : num = xPowI + j # If num is within limits # insert it into the set if (num <= bound) : s.add(num) # Break out of the inner loop else : break # Adding any number to it # will be out of bounds if (xPowI >= bound or x == 1) : break # Increment i i += 1 # Print the contents of the set for itr in s : print(itr, end = " ") # Driver code if __name__ == "__main__" : x = 2 y = 3 bound = 10 # Print powerful integers powerfulIntegers(x, y, bound) # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; using System.Linq; using System.Collections.Generic; using System.Collections; class GFG { // Function to print powerful integers static void powerfulIntegers(int x, int y, int bound) { // Set is used to store distinct numbers // in sorted order HashSet<int> s = new HashSet<int>(); ArrayList powersOfY = new ArrayList(); int i; // Store all the powers of y < bound in a vector // to avoid calculating them again and again powersOfY.Add(1); for (i = y; i < bound && y != 1; i = i * y) powersOfY.Add(i); i = 0; while (true) { // x^i int xPowI = (int)Math.Pow(x, i); for (int j = 0; j != powersOfY.Count; ++j) { int num = xPowI + (int)powersOfY[j]; // If num is within limits // insert it into the set if (num <= bound) s.Add(num); // Break out of the inner loop else break; } // Adding any number to it // will be out of bounds if (xPowI >= bound || x == 1) break; // Increment i i++; } int[] ar = s.ToArray(); Array.Sort(ar); s.Clear(); s.UnionWith(ar); // Print the contents of the set foreach (int t in s) { Console.Write( t + " "); } } // Driver code static void Main() { int x = 2, y = 3, bound = 10; // Print powerful integers powerfulIntegers(x, y, bound); } } // This code is contributed by mits
PHP
<?php // PHP implementation of the approach // Function to print powerful integers function powerfulIntegers($x, $y, $bound) { // Set is used to store distinct // numbers in sorted order $s = array(); $powersOfY = array(); // Store all the powers of y < bound // in a vector to avoid calculating // them again and again array_push($powersOfY, 1); $i = $y; while($i < $bound && $y != 1) { array_push($powersOfY, $i); $i *= $y; } $i = 0; while (true) { // x^i $xPowI = pow($x, $i); for ($j = 0; $j < count($powersOfY); $j++) { $num = $xPowI + $powersOfY[$j]; // If num is within limits // insert it into the set if ($num <= $bound) array_push($s, $num); // Break out of the inner loop else break; } // Adding any number to it // will be out of bounds if ($xPowI >= $bound || $x == 1) break; // Increment i $i += 1; } $s = array_unique($s); sort($s); // Print the contents of the set foreach ($s as &$itr) print($itr . " "); } // Driver code $x = 2; $y = 3; $bound = 10; // Print powerful integers powerfulIntegers($x, $y, $bound); // This code is contributed by chandan_jnu ?>
Javascript
<script> // Javascript implementation of the approach // Function to print powerful integers function powerfulIntegers(x, y, bound) { // Set is used to store distinct numbers // in sorted order var s = new Set(); var powersOfY = []; var i; // Store all the powers of y < bound in a vector // to avoid calculating them again and again powersOfY.push(1); for(i = y; i < bound && y!= 1; i = i * y) powersOfY.push(i); i = 0; while (true) { // x^i var xPowI = Math.pow(x, i); powersOfY.forEach(j => { var num = xPowI + j; // If num is within limits // insert it into the set if (num <= bound) s.add(num); }); // Adding any number to it // will be out of bounds if (xPowI >= bound || x == 1) break; // Increment i i++; } // Print the contents of the set [...s].sort((a, b) => a - b).forEach(itr => { document.write(itr + " ") }); } // Driver code var x = 2, y = 3, bound = 10; // Print powerful integers powerfulIntegers(x, y, bound); // This code is contributed by itsok </script>
2 3 4 5 7 9 10
Publicación traducida automáticamente
Artículo escrito por Sumit bangar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA