📜  数字的总和和乘积的最大值,直到数字减少到一位数字为止

📅  最后修改于: 2021-05-04 15:45:59             🧑  作者: Mango

给定数字N,任务是打印给定数字的总和与乘法之间的最大值,直到该数字减少到一位为止。

注意:数字的总和与乘法直到数字减少到一位为止。

让我们举一个N = 19的例子

Input: N = 631
Output: 8

Input: 110
Output: 2

方法:

  1. 检查数字是否小于10,然后总和与乘积将相同。因此,返回该数字。
  2. 别的,
    • 使用查找数字的数字总和的方法2重复查找数字的总和,直到总和成为一位数字为止。
    • 并且,使用查找数字的数字和的方法1重复查找数字的乘积,直到和成为单个数字。
  3. 返回两者的最大值。

下面是上述方法的实现:

C++
// CPP implementation of above approach
#include
using namespace std;
    // Function to sum the digits until it
    // becomes a single digit
    long repeatedSum(long n)
    {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
  
    // Function to product the digits until it
    // becomes a single digit
    long repeatedProduct(long n)
    {
        long prod = 1;
  
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || prod > 9) {
            if (n == 0) {
                n = prod;
                prod = 1;
            }
            prod *= n % 10;
            n /= 10;
        }
        return prod;
    }
  
    // Function to find the maximum among
    // repeated sum and repeated product
    long maxSumProduct(long N)
    {
  
        if (N < 10)
            return N;
  
        return max(repeatedSum(N), repeatedProduct(N));
    }
  
    // Driver code
    int main()
    {
  
        long n = 631;
        cout << maxSumProduct(n)<


Java
// Java implementation of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG {
  
    // Function to sum the digits until it
    // becomes a single digit
    public static long repeatedSum(long n)
    {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
  
    // Function to product the digits until it
    // becomes a single digit
    public static long repeatedProduct(long n)
    {
        long prod = 1;
  
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || prod > 9) {
            if (n == 0) {
                n = prod;
                prod = 1;
            }
            prod *= n % 10;
            n /= 10;
        }
        return prod;
    }
  
    // Function to find the maximum among
    // repeated sum and repeated product
    public static long maxSumProduct(long N)
    {
  
        if (N < 10)
            return N;
  
        return Math.max(repeatedSum(N), repeatedProduct(N));
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        long n = 631;
        System.out.println(maxSumProduct(n));
    }
}


Python3
# Python 3 implementation of above approach
  
# Function to sum the digits until 
# it becomes a single digit
def repeatedSum(n):
    if (n == 0):
        return 0
    return 9 if(n % 9 == 0) else (n % 9)
  
# Function to product the digits 
# until it becomes a single digit
def repeatedProduct(n):
    prod = 1
  
    # Loop to do sum while
    # sum is not less than
    # or equal to 9
    while (n > 0 or prod > 9) :
        if (n == 0) :
            n = prod
            prod = 1
              
        prod *= n % 10
        n //= 10
      
    return prod
  
# Function to find the maximum among
# repeated sum and repeated product
def maxSumProduct(N):
  
    if (N < 10):
        return N
  
    return max(repeatedSum(N), 
               repeatedProduct(N))
  
# Driver code
if __name__ == "__main__":
  
    n = 631
    print(maxSumProduct(n))
  
# This code is contributed 
# by ChitraNayal


C#
// C# implementation of 
// above approach
using System;
class GFG 
{
  
// Function to sum the digits 
// until it becomes a single digit
public static long repeatedSum(long n)
{
    if (n == 0)
        return 0;
    return (n % 9 == 0) ? 
                      9 : (n % 9);
}
  
// Function to product the digits 
// until it becomes a single digit
public static long repeatedProduct(long n)
{
    long prod = 1;
  
    // Loop to do sum while
    // sum is not less than
    // or equal to 9
    while (n > 0 || prod > 9) 
    {
        if (n == 0) 
        {
            n = prod;
            prod = 1;
        }
        prod *= n % 10;
        n /= 10;
    }
    return prod;
}
  
// Function to find the maximum among
// repeated sum and repeated product
public static long maxSumProduct(long N)
{
  
    if (N < 10)
        return N;
  
    return Math.Max(repeatedSum(N), 
                    repeatedProduct(N));
}
  
// Driver code
public static void Main()
{
    long n = 631;
    Console.WriteLine(maxSumProduct(n));
}
}
  
// This code is contributed
// by inder_verma


输出:
8