📌  相关文章
📜  将N减少到1所需的最少操作数

📅  最后修改于: 2021-05-04 21:39:13             🧑  作者: Mango

给定一个整数元素“ N”,任务是找到使“ N”等于1所需执行的最小操作数。
允许执行的操作是:

  1. 将N减1。
  2. N递增1。
  3. 如果N是3的倍数,则可以将N除以3。

例子:

方法:

  • 如果数字是3的倍数,则将其除以3。
  • 如果模3的数字为1,则将其减1。
  • 如果模3的数字为2,则将其加1。
  • 当数字等于2时会有一个例外,在这种情况下,数字应减1。
  • 重复上述步骤,直到数字大于1,并打印最后执行的操作计数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include
using namespace std;
 
// Function that returns the minimum
// number of operations to be performed
// to reduce the number to 1
int count_minimum_operations(long long n)
{
 
    // To stores the total number of
    // operations to be performed
    int count = 0;
    while (n > 1) {
 
        // if n is divisible by 3
        // then reduce it to n / 3
        if (n % 3 == 0)
            n /= 3;
 
        // if n modulo 3 is 1
        // decrement it by 1
        else if (n % 3 == 1)
            n--;
        else {
            if (n == 2)
                n--;
             
            // if n modulo 3 is 2
            // then increment it by 1
            else
                n++;
        }
 
        // update the counter
        count++;
    }
    return count;
}
 
// Driver code
int main()
{
 
    long long n = 4;
    long long ans = count_minimum_operations(n);
    cout<


Java
// Java implementation of above approach
 
class GFG {
 
    // Function that returns the minimum
    // number of operations to be performed
    // to reduce the number to 1
    static int count_minimum_operations(long n)
    {
 
        // To stores the total number of
        // operations to be performed
        int count = 0;
        while (n > 1) {
 
            // if n is divisible by 3
            // then reduce it to n / 3
            if (n % 3 == 0)
                n /= 3;
 
            // if n modulo 3 is 1
            // decrement it by 1
            else if (n % 3 == 1)
                n--;
            else {
                if (n == 2)
                    n--;
 
                // if n modulo 3 is 2
                // then increment it by 1
                else
                    n++;
            }
 
            // update the counter
            count++;
        }
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        long n = 4;
        long ans = count_minimum_operations(n);
        System.out.println(ans);
    }
}


Python3
# Python3 implementation of above approach
 
# Function that returns the minimum
# number of operations to be performed
# to reduce the number to 1
def count_minimum_operations(n):
 
    # To stores the total number of
    # operations to be performed
    count = 0
    while (n > 1) :
 
        # if n is divisible by 3
        # then reduce it to n / 3
        if (n % 3 == 0):
            n //= 3
 
        # if n modulo 3 is 1
        # decrement it by 1
        elif (n % 3 == 1):
            n -= 1
        else :
            if (n == 2):
                n -= 1
             
            # if n modulo 3 is 2
            # then increment it by 1
            else:
                n += 1
         
        # update the counter
        count += 1
     
    return count
 
# Driver code
if __name__ =="__main__":
    n = 4
    ans = count_minimum_operations(n)
    print (ans)
 
# This code is contributed
# by ChitraNayal


C#
// C# implementation of above approach
using System;
 
public class GFG{
     
        // Function that returns the minimum
    // number of operations to be performed
    // to reduce the number to 1
    static int count_minimum_operations(long n)
    {
 
        // To stores the total number of
        // operations to be performed
        int count = 0;
        while (n > 1) {
 
            // if n is divisible by 3
            // then reduce it to n / 3
            if (n % 3 == 0)
                n /= 3;
 
            // if n modulo 3 is 1
            // decrement it by 1
            else if (n % 3 == 1)
                n--;
            else {
                if (n == 2)
                    n--;
 
                // if n modulo 3 is 2
                // then increment it by 1
                else
                    n++;
            }
 
            // update the counter
            count++;
        }
        return count;
    }
 
    // Driver code
    static public void Main (){
     
        long n = 4;
        long ans = count_minimum_operations(n);
        Console.WriteLine(ans);
    }
}


PHP
 1)
    {
 
        // if n is divisible by 3
        // then reduce it to n / 3
        if ($n % 3 == 0)
            $n /= 3;
 
        // if n modulo 3 is 1
        // decrement it by 1
        else if ($n % 3 == 1)
            $n--;
        else
        {
            if ($n == 2)
                $n--;
             
            // if n modulo 3 is 2
            // then increment it by 1
            else
                $n++;
        }
 
        // update the counter
        $count++;
    }
    return $count;
}
 
// Driver code
$n = 4;
 
$ans = count_minimum_operations($n);
echo $ans, "\n";
 
// This code is contributed by akt_mit
?>


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function that returns the minimum
// number of operations to be performed
// to reduce the number to 1
int count_minimum_operations(long long n)
{
 
    // Base cases
    if (n == 2) {
        return 1;
    }
    else if (n == 1) {
        return 0;
    }
    if (n % 3 == 0) {
        return 1 + count_minimum_operations(n / 3);
    }
    else if (n % 3 == 1) {
        return 1 + count_minimum_operations(n - 1);
    }
    else {
        return 1 + count_minimum_operations(n + 1);
    }
}
 
// Driver code
int main()
{
 
    long long n = 4;
    long long ans = count_minimum_operations(n);
    cout << ans << endl;
    return 0;
}
 
// This code is contributed by koulick_sadhu


Java
// Java implementation of above approach
import java.util.*;
 
class GFG{
     
// Function that returns the minimum
// number of operations to be performed
// to reduce the number to 1
public static int count_minimum_operations(int n)
{
     
    // Base cases
    if (n == 2)
    {
        return 1;
    }
    else if (n == 1)
    {
        return 0;
    }
    if (n % 3 == 0)
    {
        return 1 + count_minimum_operations(n / 3);
    }
    else if (n % 3 == 1)
    {
        return 1 + count_minimum_operations(n - 1);
    }
    else
    {
        return 1 + count_minimum_operations(n + 1);
    }
}
 
// Driver code
public static void main(String []args)
{
    int n = 4;
    int ans = count_minimum_operations(n);
     
    System.out.println(ans);
}
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 implementation of above approach
 
# Function that returns the minimum
# number of operations to be performed
# to reduce the number to 1
def count_minimum_operations(n):
     
    # Base cases
    if (n == 2):
        return 1
    elif (n == 1):
        return 0
    if (n % 3 == 0):
        return 1 + count_minimum_operations(n / 3)
    elif (n % 3 == 1):
        return 1 + count_minimum_operations(n - 1)
    else:
        return 1 + count_minimum_operations(n + 1)
 
# Driver Code
n = 4
ans = count_minimum_operations(n)
 
print(ans)
 
# This code is contributed by divyesh072019


C#
// C# implementation of above approach
using System;
class GFG {
     
    // Function that returns the minimum
    // number of operations to be performed
    // to reduce the number to 1
    static int count_minimum_operations(int n)
    {
      
        // Base cases
        if (n == 2) {
            return 1;
        }
        else if (n == 1) {
            return 0;
        }
        if (n % 3 == 0) {
            return 1 + count_minimum_operations(n / 3);
        }
        else if (n % 3 == 1) {
            return 1 + count_minimum_operations(n - 1);
        }
        else {
            return 1 + count_minimum_operations(n + 1);
        }
    }
   
  // Driver code
  static void Main() {
    int n = 4;
    int ans = count_minimum_operations(n);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出
2

递归方法:递归方法类似于上面使用的方法。

下面是实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// Function that returns the minimum
// number of operations to be performed
// to reduce the number to 1
int count_minimum_operations(long long n)
{
 
    // Base cases
    if (n == 2) {
        return 1;
    }
    else if (n == 1) {
        return 0;
    }
    if (n % 3 == 0) {
        return 1 + count_minimum_operations(n / 3);
    }
    else if (n % 3 == 1) {
        return 1 + count_minimum_operations(n - 1);
    }
    else {
        return 1 + count_minimum_operations(n + 1);
    }
}
 
// Driver code
int main()
{
 
    long long n = 4;
    long long ans = count_minimum_operations(n);
    cout << ans << endl;
    return 0;
}
 
// This code is contributed by koulick_sadhu

Java

// Java implementation of above approach
import java.util.*;
 
class GFG{
     
// Function that returns the minimum
// number of operations to be performed
// to reduce the number to 1
public static int count_minimum_operations(int n)
{
     
    // Base cases
    if (n == 2)
    {
        return 1;
    }
    else if (n == 1)
    {
        return 0;
    }
    if (n % 3 == 0)
    {
        return 1 + count_minimum_operations(n / 3);
    }
    else if (n % 3 == 1)
    {
        return 1 + count_minimum_operations(n - 1);
    }
    else
    {
        return 1 + count_minimum_operations(n + 1);
    }
}
 
// Driver code
public static void main(String []args)
{
    int n = 4;
    int ans = count_minimum_operations(n);
     
    System.out.println(ans);
}
}
 
// This code is contributed by avanitrachhadiya2155

Python3

# Python3 implementation of above approach
 
# Function that returns the minimum
# number of operations to be performed
# to reduce the number to 1
def count_minimum_operations(n):
     
    # Base cases
    if (n == 2):
        return 1
    elif (n == 1):
        return 0
    if (n % 3 == 0):
        return 1 + count_minimum_operations(n / 3)
    elif (n % 3 == 1):
        return 1 + count_minimum_operations(n - 1)
    else:
        return 1 + count_minimum_operations(n + 1)
 
# Driver Code
n = 4
ans = count_minimum_operations(n)
 
print(ans)
 
# This code is contributed by divyesh072019

C#

// C# implementation of above approach
using System;
class GFG {
     
    // Function that returns the minimum
    // number of operations to be performed
    // to reduce the number to 1
    static int count_minimum_operations(int n)
    {
      
        // Base cases
        if (n == 2) {
            return 1;
        }
        else if (n == 1) {
            return 0;
        }
        if (n % 3 == 0) {
            return 1 + count_minimum_operations(n / 3);
        }
        else if (n % 3 == 1) {
            return 1 + count_minimum_operations(n - 1);
        }
        else {
            return 1 + count_minimum_operations(n + 1);
        }
    }
   
  // Driver code
  static void Main() {
    int n = 4;
    int ans = count_minimum_operations(n);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by divyeshrabadiya07

Java脚本


输出:

2