📌  相关文章
📜  第N个项,其中K + 1个项是第K个项与第K个项的最大位数和最小位数之差的乘积

📅  最后修改于: 2021-04-22 06:12:22             🧑  作者: Mango

给定两个整数ND ,任务是找到F(N)的值,其中F(1)的值为D,其中F(K)给出为:

F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))

例子:

方法:这个想法是在循环的帮助下迭代计算F(2)到F(N)的值。同样,每个数字中的最大和最小数字可通过循环的帮助来计算,方法是将数字除以10,同时取模取数字。
下面是上述方法的实现:

C++
// C++ implementation to find the value
// of the given function for the value
 
#include 
using namespace std;
 
// Function to find minimum digit
// in the decimal representation of N
int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n) {
        ans = min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n) {
        ans = max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k--) {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    cout << n;
}
 
// Driver Code
int main()
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
 
    return 0;
}


Java
// Java implementation to find the value
// of the given function for the value
 
class GFG{
 
// Function to find minimum digit in
// the decimal representation of N
static int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n > 0)
    {
        ans = Math.min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
static int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n > 0)
    {
        ans = Math.max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
static void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k-- > 0)
    {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    System.out.print(n);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the value
# of the given function for the value
 
# Function to find minimum digit
# in the decimal representation of N
def MIN(n):
     
    ans = 11
     
    # Loop to find the minimum
    # digit in the number
    while n:
        ans = min(ans, n % 10)
        n //= 10
    return ans
 
# Function to find maximum digit in
# the decimal representation of N
def MAX(n):
     
    ans = -1
     
    # Loop to find the maximum
    # digit in the number
    while n:
        ans = max(ans, n % 10)
        n //= 10
    return ans
 
# Function to find the value
# of the given function
def Find_value(n, k):
     
    k -= 1
    (x, y) = (0, 0)
     
    # Loop to compute the values
    # of the given number
    while k:
        k -= 1
        x = MIN(n)
        y = MAX(n)
        if ((y - x) == 0):
            break
        n *= (y - x)
         
    print(n, end = ' ')
 
# Driver Code
if __name__=='__main__':
     
    (N, D) = (487, 5)
     
    # Function Call
    Find_value(N, D)
 
# This code is contributed by rutvik_56


C#
// C# implementation to find the value
// of the given function for the value
using System;
 
class GFG{
 
// Function to find minimum digit in
// the decimal representation of N
static int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n > 0)
    {
        ans = Math.Min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
static int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n > 0)
    {
        ans = Math.Max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
static void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k-- > 0)
    {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    Console.Write(n);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
981792