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

📅  最后修改于: 2021-10-25 08:12:15             🧑  作者: 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

蟒蛇3

# 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程