给定数字“ n”,找到最小的数字“ p”,这样,如果我们将“ p”的所有数字相乘,便得到“ n”。结果“ p”应至少包含两位数。
例子:
Input: n = 36
Output: p = 49
// Note that 4*9 = 36 and 49 is the smallest such number
Input: n = 100
Output: p = 455
// Note that 4*5*5 = 100 and 455 is the smallest such number
Input: n = 1
Output:p = 11
// Note that 1*1 = 1
Input: n = 13
Output: Not Possible
对于给定的n,以下是要考虑的两种情况。
情况1:n <10当n小于10时,输出始终为n + 10。例如,对于n = 7,输出为17。对于n = 9,输出为19。
情况2:n> = 10求n的所有因子都在2到9之间(包括2和9)。想法是从9开始搜索,以使结果中的位数最小化。例如,9比33更可取,8比24更可取。
将所有找到的因子存储在数组中。该数组将以非递增顺序包含数字,因此最终以相反顺序打印该数组。
以下是上述概念的实现。
C++
#include
using namespace std;
// Maximum number of digits in output
#define MAX 50
// prints the smallest number
// whose digits multiply to n
void findSmallest(int n)
{
int i, j = 0;
// To store digits of result
// in reverse order
int res[MAX];
// Case 1: If number is smaller than 10
if (n < 10)
{
cout << n + 10;
return;
}
// Case 2: Start with 9 and
// try every possible digit
for (i = 9; i > 1; i--)
{
// If current digit divides n, then store all
// occurrences of current digit in res
while (n % i == 0)
{
n = n / i;
res[j] = i;
j++;
}
}
// If n could not be broken
// in form of digits (prime factors
// of n are greater than 9)
if (n > 10)
{
cout << "Not possible";
return;
}
// Print the result array in reverse order
for (i = j - 1; i >= 0; i--)
cout << res[i];
}
// Driver Code
int main()
{
findSmallest(7);
cout << "\n";
findSmallest(36);
cout << "\n";
findSmallest(13);
cout << "\n";
findSmallest(100);
return 0;
}
// This code is contributed by Code_Mech
C
#include
// Maximum number of digits in output
#define MAX 50
// prints the smallest number whose digits multiply to n
void findSmallest(int n)
{
int i, j=0;
int res[MAX]; // To sore digits of result in reverse order
// Case 1: If number is smaller than 10
if (n < 10)
{
printf("%d", n+10);
return;
}
// Case 2: Start with 9 and try every possible digit
for (i=9; i>1; i--)
{
// If current digit divides n, then store all
// occurrences of current digit in res
while (n%i == 0)
{
n = n/i;
res[j] = i;
j++;
}
}
// If n could not be broken in form of digits (prime factors of n
// are greater than 9)
if (n > 10)
{
printf("Not possible");
return;
}
// Print the result array in reverse order
for (i=j-1; i>=0; i--)
printf("%d", res[i]);
}
// Driver program to test above function
int main()
{
findSmallest(7);
printf("\n");
findSmallest(36);
printf("\n");
findSmallest(13);
printf("\n");
findSmallest(100);
return 0;
}
Java
// Java program to find the smallest number whose
// digits multiply to a given number n
import java.io.*;
class Smallest
{
// Function to prints the smallest number whose
// digits multiply to n
static void findSmallest(int n)
{
int i, j=0;
int MAX = 50;
// To sore digits of result in reverse order
int[] res = new int[MAX];
// Case 1: If number is smaller than 10
if (n < 10)
{
System.out.println(n+10);
return;
}
// Case 2: Start with 9 and try every possible digit
for (i=9; i>1; i--)
{
// If current digit divides n, then store all
// occurrences of current digit in res
while (n%i == 0)
{
n = n/i;
res[j] = i;
j++;
}
}
// If n could not be broken in form of digits (prime factors of n
// are greater than 9)
if (n > 10)
{
System.out.println("Not possible");
return;
}
// Print the result array in reverse order
for (i=j-1; i>=0; i--)
System.out.print(res[i]);
System.out.println();
}
// Driver program
public static void main (String[] args)
{
findSmallest(7);
findSmallest(36);
findSmallest(13);
findSmallest(100);
}
}
// Contributed by Pramod Kumar
Python
# Python code to find the smallest number
# whose digits multiply to give n
# function to print the smallest number whose
# digits multiply to n
def findSmallest(n):
# Case 1 - If the number is smaller than 10
if n < 10:
print n+10
return
# Case 2 - Start with 9 and try every possible digit
res = [] # to sort digits
for i in range(9,1,-1):
# If current digit divides n, then store all
# occurrences of current digit in res
while n % i == 0:
n = n / i
res.append(i)
# If n could not be broken in the form of digits
# prime factors of n are greater than 9
if n > 10:
print "Not Possible"
return
# Print the number from result array in reverse order
n = res[len(res)-1]
for i in range(len(res)-2,-1,-1):
n = 10 * n + res[i]
print n
# Driver Code
findSmallest(7)
findSmallest(36)
findSmallest(13)
findSmallest(100)
# This code is contributed by Harshit Agrawal
C#
// C# program to find the smallest number whose
// digits multiply to a given number n
using System;
class GFG {
// Function to prints the smallest number
// whose digits multiply to n
static void findSmallest(int n)
{
int i, j=0;
int MAX = 50;
// To sore digits of result in
// reverse order
int []res = new int[MAX];
// Case 1: If number is smaller than 10
if (n < 10)
{
Console.WriteLine(n + 10);
return;
}
// Case 2: Start with 9 and try every
// possible digit
for (i = 9; i > 1; i--)
{
// If current digit divides n, then
// store all occurrences of current
// digit in res
while (n % i == 0)
{
n = n / i;
res[j] = i;
j++;
}
}
// If n could not be broken in form of
// digits (prime factors of n
// are greater than 9)
if (n > 10)
{
Console.WriteLine("Not possible");
return;
}
// Print the result array in reverse order
for (i = j-1; i >= 0; i--)
Console.Write(res[i]);
Console.WriteLine();
}
// Driver program
public static void Main ()
{
findSmallest(7);
findSmallest(36);
findSmallest(13);
findSmallest(100);
}
}
// This code is contributed by nitin mittal.
PHP
1; $i--)
{
// If current digit divides
// n, then store all
// occurrences of current
// digit in res
while ($n % $i == 0)
{
$n = $n / $i;
$res[$j] = $i;
$j++;
}
}
// If n could not be broken
// in form of digits
// (prime factors of n
// are greater than 9)
if ($n > 10)
{
echo "Not possible";
return;
}
// Print the result
// array in reverse order
for ($i = $j - 1; $i >= 0; $i--)
echo $res[$i];
}
// Driver Code
findSmallest(7);
echo "\n";
findSmallest(36);
echo "\n";
findSmallest(13);
echo "\n";
findSmallest(100);
// This code is contributed by ajit
?>
Javascript
输出:
17
49
Not possible
455