📌  相关文章
📜  通过将最大位数和最小位数K乘以乘积而形成的数字

📅  最后修改于: 2021-04-23 07:51:00             🧑  作者: Mango

给定两个整数NK ,任务是打印通过将最大和最小数字的乘积K乘以形成的数字。
M(N+1) = M(N) + maxDigit(M(N)) * minDigit(M(N))

例子

方法

  1. 直觉是将循环运行K次并不断更新N的值。
  2. 但是可以观察到的一种观察结果是,经过一轮迭代,digit的最小值可能为零,并且在此之后,N永远都不会更新,因为:
  3. 因此,我们只需要弄清楚最小数字何时变为0。

以下是上述方法的C++实现:

C++
// C++ Code for the above approach
  
#include 
using namespace std;
  
// function that returns the product of
// maximum and minimum digit of N number.
int prod_of_max_min(int n)
{
    int largest = 0;
    int smallest = 10;
  
    while (n) {
  
        // finds the last digit.
        int r = n % 10;
  
        largest = max(r, largest);
        smallest = min(r, smallest);
  
        // Moves to next digit
        n = n / 10;
    }
  
    return largest * smallest;
}
  
// Function to find the formed number
int formed_no(int N, int K)
{
  
    if (K == 1) {
        return N;
    }
    K--; // M(1) = N
  
    int answer = N;
    while (K--) {
  
        int a_current
            = prod_of_max_min(answer);
  
        // check if minimum digit is 0
        if (a_current == 0)
            break;
  
        answer += a_current;
    }
  
    return answer;
}
  
// Driver Code
int main()
{
  
    int N = 487, K = 100000000;
  
    cout << formed_no(N, K) << endl;
  
    return 0;
}


Java
// Java program for the above approach
import java.io.*; 
import java.util.*;
  
class GFG { 
      
// Function to find the formed number 
public static int formed_no(int N, int K)
{
    if (K == 1) 
    {
        return N;
    }
    K--; // M(1) = N 
  
    int answer = N;
    while(K != 0) 
    {
        int a_current = prod_of_max_min(answer);
  
        // Check if minimum digit is 0
        if (a_current == 0)
            break;
          
        answer += a_current;
    }
    return answer;
}
  
// Function that returns the product of 
// maximum and minimum digit of N number.
static int prod_of_max_min(int n)
{
    int largest = 0;
    int smallest = 10;
  
    while(n != 0) 
    {
  
        // Finds the last digit.
        int r = n % 10;
  
        largest = Math.max(r, largest);
        smallest = Math.min(r, smallest);
  
        // Moves to next digit
        n = n / 10;
    }
    return largest * smallest;
}
      
// Driver code 
public static void main(String[] args) 
{ 
    int N = 487, K = 100000000;
  
    System.out.println(formed_no(N, K));
} 
} 
  
// This code is contributed by coder001


Python3
# Python3 program for the above approach
  
# Function to find the formed number
def formed_no(N, K):
  
    if (K == 1):
        return N
    K -= 1 # M(1) = N
      
    answer = N
    while (K != 0):
  
        a_current = prod_of_max_min(answer)
  
        # Check if minimum digit is 0
        if (a_current == 0):
            break
  
        answer += a_current 
        K -= 1
  
    return answer
  
# Function that returns the product of
# maximum and minimum digit of N number.
def prod_of_max_min(n):
  
    largest = 0
    smallest = 10
  
    while (n != 0):
  
        # Find the last digit.
        r = n % 10
  
        largest = max(r, largest)
        smallest = min(r, smallest)
  
        # Moves to next digit
        n = n // 10
  
    return largest * smallest
  
# Driver Code
if __name__ == "__main__":
  
    N = 487
    K = 100000000
  
    print(formed_no(N, K))
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG { 
      
// Function to find the formed number 
public static int formed_no(int N, int K)
{
    if (K == 1) 
    {
        return N;
    }
    K--; // M(1) = N 
  
    int answer = N;
    while(K != 0) 
    {
        int a_current = prod_of_max_min(answer);
  
        // Check if minimum digit is 0
        if (a_current == 0)
            break;
          
        answer += a_current;
    }
    return answer;
}
  
// Function that returns the product of 
// maximum and minimum digit of N number.
static int prod_of_max_min(int n)
{
    int largest = 0;
    int smallest = 10;
  
    while(n != 0) 
    {
  
        // Finds the last digit.
        int r = n % 10;
  
        largest = Math.Max(r, largest);
        smallest = Math.Min(r, smallest);
  
        // Moves to next digit
        n = n / 10;
    }
    return largest * smallest;
}
      
// Driver code 
public static void Main(String[] args) 
{ 
    int N = 487, K = 100000000;
  
    Console.WriteLine(formed_no(N, K));
} 
} 
  
// This code is contributed by Rohit_ranjan


输出:
950

时间复杂度: O(K)
辅助空间: O(1)