原文:

给定一个数字 n ,任务是找到不同的 n 数字,这样他们的产品就是一个。 举例:

输入: n = 3 输出: 1,8,27 解释: 输出数的乘积= 1 * 8 * 27 = 216,是 6 的完美立方(6 3 = 216) 输入: n = 2 输出: 1 8 解释:t20

方法:pg电子试玩链接的解决方案基于 的事实

第一个‘n’个完美立方数的乘积永远是一个完美立方。

因此,前 n 个自然数的将被打印为输出。 例如:

for n = 1 => [1]
    product is 1
    and cube root of 1 is also 1
for n = 2 => [1, 8]
    product is 8
    and cube root of 8 is 2
for n = 3 => [1, 8, 27]
    product is 216
    and cube root of 216 is 6
and so on

以下是上述方法的实现:

c

// c   program to find n numbers such that
// their product is a perfect cube
#include 
using namespace std;
// function to find the n numbers such
//that their product is a perfect cube
void findnumbers(int n)
{
    int i = 1;
    // loop to traverse each
//number from 1 to n
    while (i <= n) {
// print the cube of i
//as the ith term of the output
        cout << (i * i * i)
             << " ";
        i  ;
    }
}
// driver code
int main()
{
    int n = 4;
    // function call
    findnumbers(n);
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find n numbers such that
// their product is a perfect cube
import java.util.*;
class gfg
{
// function to find the n numbers such
//that their product is a perfect cube
static void findnumbers(int n)
{
    int i = 1;
    // loop to traverse each
    //number from 1 to n
    while (i <= n) {
        // print the cube of i
        //as the ith term of the output
        system.out.print( (i * i * i)
              " ");
        i  ;
    }
}
// driver code
public static void main (string []args)
{
    int n = 4;
    // function call
    findnumbers(n);
}
}
// this code is contributed by chitranayal

python 3

# python3 program to find n numbers such that
# their product is a perfect cube
# function to find the n numbers such
# that their product is a perfect cube
def findnumbers(n):
    i = 1
    # loop to traverse each
    # number from 1 to n
    while (i <= n):
        # print the cube of i
        # as the ith term of the output
        print((i * i * i), end=" ")
        i  = 1
# driver code
if __name__ == '__main__':
    n = 4
    # function call
    findnumbers(n)
# this code is contributed by mohit kumar 29

c

// c# program to find n numbers such that
// their product is a perfect cube
using system;
class gfg
{
// function to find the n numbers such
//that their product is a perfect cube
static void findnumbers(int n)
{
    int i = 1;
    // loop to traverse each
    //number from 1 to n
    while (i <= n) {
        // print the cube of i
        //as the ith term of the output
        console.write( (i * i * i)
              " ");
        i  ;
    }
}
// driver code
public static void main (string []args)
{
    int n = 4;
    // function call
    findnumbers(n);
}
}
// this code is contributed by yash_r

java 描述语言


output: 

1 8 27 64

业绩分析:

  • 时间复杂度:和上面的方法一样,我们正在寻找 n 个数的完美立方,因此需要 o(n) 时间。
  • 辅助空间复杂度:和上面的方法一样,没有使用额外的空间;因此辅助空间的复杂性将是 o(1)