给定数字N。打印三个乘积等于N的不同数字(> = 1) 。如果无法找到三个数字,则打印-1。
例子:
Input: 64
Output: 2 4 8
Explanation:
(2*4*8 = 64)
Input: 24
Output: 2 3 4
Explanation:
(2*3*4 = 24)
Input: 12
Output: -1
Explanation:
No such triplet exists
方法:
- 使用本文讨论的方法制作一个存储给定数字的所有除数的数组
- 设三个数字为a,b,c初始化为1
- 遍历除数数组并检查以下条件:
- a =值在除数数组的第一个索引处的值。
- b的值=除数数组第二和第三索引的值的乘积。如果除数数组只有一个或两个元素,则不存在这样的三元组
- 找到a和b后,c的值=除数数组中所有其余元素的乘积。
- 检查最终条件,使a,b,c的值必须不同且不等于1。
下面是实现代码:
CPP
// C++ program to find the
// three numbers
#include "bits/stdc++.h"
using namespace std;
// function to find 3 distinct number
// with given product
void getnumbers(int n)
{
// Declare a vector to store
// divisors
vector divisor;
// store all divisors of number
// in array
for (int i = 2; i * i <= n; i++) {
// store all the occurence of
// divisors
while (n % i == 0) {
divisor.push_back(i);
n /= i;
}
}
// check if n is not equals to -1
// then n is also a prime factor
if (n != 1) {
divisor.push_back(n);
}
// Initialize the variables with 1
int a, b, c, size;
a = b = c = 1;
size = divisor.size();
for (int i = 0; i < size; i++) {
// check for first number a
if (a == 1) {
a = a * divisor[i];
}
// check for second number b
else if (b == 1 || b == a) {
b = b * divisor[i];
}
// check for third number c
else {
c = c * divisor[i];
}
}
// check for all unwanted condition
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
cout << "-1" << endl;
}
else {
cout << a << ' ' << b
<< ' ' << c << endl;
}
}
// Driver function
int main()
{
int n = 64;
getnumbers(n);
}
Java
// Java program to find the
// three numbers
import java.util.*;
class GFG{
// function to find 3 distinct number
// with given product
static void getnumbers(int n)
{
// Declare a vector to store
// divisors
Vector divisor = new Vector();
// store all divisors of number
// in array
for (int i = 2; i * i <= n; i++) {
// store all the occurence of
// divisors
while (n % i == 0) {
divisor.add(i);
n /= i;
}
}
// check if n is not equals to -1
// then n is also a prime factor
if (n != 1) {
divisor.add(n);
}
// Initialize the variables with 1
int a, b, c, size;
a = b = c = 1;
size = divisor.size();
for (int i = 0; i < size; i++) {
// check for first number a
if (a == 1) {
a = a * divisor.get(i);
}
// check for second number b
else if (b == 1 || b == a) {
b = b * divisor.get(i);
}
// check for third number c
else {
c = c * divisor.get(i);
}
}
// check for all unwanted condition
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
System.out.print("-1" +"\n");
}
else {
System.out.print(a +" "+ b
+" "+ c +"\n");
}
}
// Driver function
public static void main(String[] args)
{
int n = 64;
getnumbers(n);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to find the
# three numbers
# function to find 3 distinct number
# with given product
def getnumbers(n):
# Declare a vector to store
# divisors
divisor = []
# store all divisors of number
# in array
for i in range(2, n + 1):
# store all the occurence of
# divisors
while (n % i == 0):
divisor.append(i)
n //= i
# check if n is not equals to -1
# then n is also a prime factor
if (n != 1):
divisor.append(n)
# Initialize the variables with 1
a, b, c, size = 0, 0, 0, 0
a = b = c = 1
size = len(divisor)
for i in range(size):
# check for first number a
if (a == 1):
a = a * divisor[i]
# check for second number b
elif (b == 1 or b == a):
b = b * divisor[i]
# check for third number c
else:
c = c * divisor[i]
# check for all unwanted condition
if (a == 1 or b == 1 or c == 1
or a == b or b == c or a == c):
print("-1")
else:
print(a, b, c)
# Driver function
n = 64
getnumbers(n)
# This code is contributed by mohit kumar 29
C#
// C# program to find the
// three numbers
using System;
using System.Collections.Generic;
class GFG{
// function to find 3 distinct number
// with given product
static void getnumbers(int n)
{
// Declare a vector to store
// divisors
List divisor = new List();
// store all divisors of number
// in array
for (int i = 2; i * i <= n; i++) {
// store all the occurence of
// divisors
while (n % i == 0) {
divisor.Add(i);
n /= i;
}
}
// check if n is not equals to -1
// then n is also a prime factor
if (n != 1) {
divisor.Add(n);
}
// Initialize the variables with 1
int a, b, c, size;
a = b = c = 1;
size = divisor.Count;
for (int i = 0; i < size; i++) {
// check for first number a
if (a == 1) {
a = a * divisor[i];
}
// check for second number b
else if (b == 1 || b == a) {
b = b * divisor[i];
}
// check for third number c
else {
c = c * divisor[i];
}
}
// check for all unwanted condition
if (a == 1 || b == 1 || c == 1
|| a == b || b == c || a == c) {
Console.Write("-1" +"\n");
}
else {
Console.Write(a +" "+ b
+" "+ c +"\n");
}
}
// Driver function
public static void Main(String[] args)
{
int n = 64;
getnumbers(n);
}
}
// This code is contributed by Rajput-Ji
输出:
2 4 8
时间复杂度: O( (log N)* sqrt(N) )