📌  相关文章
📜  用最少的给定操作数将N减少到1

📅  最后修改于: 2021-05-06 22:33:15             🧑  作者: Mango

给定整数N ,任务是通过以下两个操作将N减小为1

  1. 仅当数字大于0并且结果数字没有任何前导0时,才可以从数字的每个数字中减去1
  2. 可以从数字本身中减去1

任务是找到将N减少到1所需的此类操作的最小数量。

例子:

方法:可以观察到,如果次数为10的幂,即N = 10 p,则操作次数将为(10 * p)– 1 。例如,如果N = 10 2,则运算将为(10 * 2)– 2 = 19
100-> 99-> 88-> 77->…-> 33-> 22-> 11-> 10-> 9-> 8->…-> 2-> 1
现在,任务是首先使用给定的运算将给定的幂转换为10的幂,然后计算将10的幂减小为1所需的操作数。这些操作的总和是必需的答案。将数字转换为幂的运算所需的操作数将为max(first_digit – 1,second_digit,third_digit,…,last_digit) ,这是因为每个数字都可以减少为0,但为了使数字有效,第一个数字必须为1它是10的幂,且具有相等的位数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum number of
// given operations required to reduce n to 1
long long int minOperations(long long int n)
{
    // To store the count of operations
    long long int count = 0;
  
    // To store the digit
    long long int d = 0;
  
    // If n is already then no
    // operation is required
    if (n == 1)
        return 0;
  
    // Extract all the digits except
    // the first digit
    while (n > 9) {
  
        // Store the maximum of that digits
        d = max(n % 10, d);
        n /= 10;
  
        // for each digit
        count += 10;
    }
  
    // First digit
    d = max(d, n - 1);
  
    // Add the value to count
    count += abs(d);
  
    return count - 1;
}
  
// Driver code
int main()
{
    long long int n = 240;
  
    cout << minOperations(n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
      
    // Function to return the minimum number of 
    // given operations required to reduce n to 1 
    static long minOperations(long n) 
    { 
        // To store the count of operations 
        long count = 0; 
      
        // To store the digit 
        long d = 0; 
      
        // If n is already then no 
        // operation is required 
        if (n == 1) 
            return 0; 
      
        // Extract all the digits except 
        // the first digit 
        while (n > 9) 
        { 
      
            // Store the maximum of that digits 
            d = Math.max(n % 10, d); 
            n /= 10; 
      
            // for each digit 
            count += 10; 
        } 
      
        // First digit 
        d = Math.max(d, n - 1); 
      
        // Add the value to count 
        count += Math.abs(d); 
      
        return count - 1; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        long n = 240; 
      
        System.out.println(minOperations(n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
  
# Function to return the minimum number of 
# given operations required to reduce n to 1 
def minOperations(n): 
  
    # To store the count of operations 
    count = 0
  
    # To store the digit 
    d = 0
  
    # If n is already then no 
    # operation is required 
    if (n == 1): 
        return 0
  
    # Extract all the digits except 
    # the first digit 
    while (n > 9): 
  
        # Store the maximum of that digits 
        d = max(n % 10, d) 
        n //= 10
  
        # for each digit 
        count += 10
      
    # First digit 
    d = max(d, n - 1) 
  
    # Add the value to count 
    count += abs(d) 
  
    return count - 1
  
# Driver code 
if __name__ == '__main__': 
  
    n = 240
  
    print(minOperations(n)) 
  
# This code is contributed by ashutosh450


C#
// C# implementation of the approach
using System;
  
class GFG 
{
      
    // Function to return the minimum number of 
    // given operations required to reduce n to 1 
    static long minOperations(long n) 
    { 
        // To store the count of operations 
        long count = 0; 
      
        // To store the digit 
        long d = 0; 
      
        // If n is already then no 
        // operation is required 
        if (n == 1) 
            return 0; 
      
        // Extract all the digits except 
        // the first digit 
        while (n > 9) 
        { 
      
            // Store the maximum of that digits 
            d = Math.Max(n % 10, d); 
            n /= 10; 
      
            // for each digit 
            count += 10; 
        } 
      
        // First digit 
        d = Math.Max(d, n - 1); 
      
        // Add the value to count 
        count += Math.Abs(d); 
      
        return count - 1; 
    } 
      
    // Driver code 
    public static void Main (String[] args)
    { 
        long n = 240; 
      
        Console.WriteLine(minOperations(n)); 
    } 
}
  
// This code is contributed by Rajput-Ji


输出:
23