📜  仅将数字转换为3和8的数字

📅  最后修改于: 2021-04-24 21:36:37             🧑  作者: Mango

迷人数字是仅由3和8组成的数字。我们为您提供了一个n位数字,您可以执行以下三种操作:

  1. 给定数字加1。
  2. 将1减去给定的数字。
  3. 选择一个数字位数,然后将其替换为任何其他所需的数字。

我们需要找到将给定数字更改为迷人数字的操作总数。

例子:

Input : num = 343
Output : Minimum Operation = 1

Input : num = 88
Output : Minimum operation = 0

在寻求适当的解决方案之前,让我们仔细研究给定的操作。

  1. 将1加到给定的数字上,它将计数1次操作,并且可以执行的操作是除非最后一位为9,否则将最后一位数字加1;如果最后一位为9,则它还将更改最后一位之前的数字。因此,在最佳情况下,如果最后一位数字是2或7,则此操作将以1次操作为代价将其更改为3或8。
  2. 减去1到给定的数字,也将1算作运算,除非最后一位数字为0,否则仅减少最后一位数字。因此,在最佳情况下,如果最后一位数字为4或9,则此运算会将1运算的成本更改为3或8 。
  3. 选择任何数字并更改其值将视为一次操作,但可以肯定的是,它将把数字更改为迷人的数字,即3或8。因此,在最佳和最差情况下,此操作都会一次更改一个数字。

因此,为了找到最小的运算,加法和减法对我们将无用,并且仅将选择和更改不等于3或8的位数。因此,总位数不等于3或8将是我们的答案。

C++
// CPP to find min operations required to
// convert into charming number
#include 
using namespace std;
  
// function for minimum operation
int minOp(long long int num)
{
    // remainder and operations count
    int rem;
    int count = 0;
  
    // count digits not equal to 3 or 8
    while (num) {
        rem = num % 10;
        if (!(rem == 3 || rem == 8))
            count++;
        num /= 10;
    }
    return count;
}
  
// driver function
int main()
{
    long long int num = 234198;
    cout << "Minimum Operations =" << minOp(num);
    return 0;
}


Java
// Java to find min operations required to
// convert into charming number
class GFG
{
      
    // function for minimum operation
    static int minOp(int num)
    {
          
        // remainder and operations count
        int rem;
        int count = 0;
      
        // count digits not equal to 3 or 8
        while (num>0) {
            rem = num % 10;
            if (!(rem == 3 || rem == 8))
                count++;
            num /= 10;
        }
          
        return count;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int num = 234198;
          
        System.out.print("Minimum Operations ="+minOp(num));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python to find min
# operations required to
# convert into charming number
  
# function for minimum operation
def minOp(num):
  
    # remainder and operations count
    count = 0
   
    #count digits not equal to 3 or 8
    while (num):
        rem = num % 10
        if (not(rem == 3 or rem == 8)):
            count=count + 1
        num = num // 10
      
    return count
  
# Driver code
  
num = 234198
print("Minimum Operations =" ,minOp(num))
  
# This code is contributed
# by Anant Agarwal.


C#
// C# to find min operations required to
// convert into charming number
using System;
  
class GFG
{
      
    // function for minimum operation
    static int minOp(int num)
    {
          
        // remainder and operations count
        int rem;
        int count = 0;
      
        // count digits not equal to 3 or 8
        while (num > 0) 
        {
            rem = num % 10;
            if (! (rem == 3 || rem == 8))
                count++;
            num /= 10;
        }
          
        return count;
    }
      
    // Driver code
    public static void Main ()
    {
        int num = 234198;
      
        Console.WriteLine("Minimum Operations =" + 
                           minOp(num));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

Minimum Operations = 4