📌  相关文章
📜  打印所有整数,它们是两个给定数字的幂的和

📅  最后修改于: 2021-04-26 04:55:19             🧑  作者: Mango

给定三个非负整数xybound ,任务是打印所有有力整数?按排序顺序绑定
一个功能强大的整数的形式为X I + Y j表示所有I,J? 0
例子:

方法:对外部循环初始化i = 0 ,对内部循环初始化j = 0 ,如果x i =绑定,则跳出外部循环(因为将y的任意幂加到边界将使其超出边界)。如果x i + y j > bound,则跳出内部循环,并在内部循环的所有其他迭代中,将x i + y j保存在一组中,以维护强大的整数的不同且已排序的列表。最后打印套的内容。
为了避免一次又一次地计算y的幂,可以预先计算y的所有幂并将其存储在向量中。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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 s;
    vector 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::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 s = new HashSet<>();
        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((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 s = new HashSet(); 
    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
= $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
?>


输出:
2 3 4 5 7 9 10