📌  相关文章
📜  从(1,1)开始达到N的最小移动次数

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

给定一个整数N和一个无限表,其中i行和j列包含值i * j 。任务是找到从单元(1、1)开始到达包含N的单元的最小移动次数。

注意:(i,j)开始,只有有效的移动是(i + 1,j)(i,j + 1)

例子:

方法:请注意,可以通过i + j – 2个步骤来到达任何单元格(i,j) 。因此,只需要i * j = N的对(i,j) ,即可使i + j最小。可以通过找到所有可能的对(i,j)并在O(√ N)中进行检查来找到它。要做到这一点,不失一般性,可以假设i≤ji≤&的Sqrt; N,因为N = I *Ĵ≥I 2。因此√ N≥i 2,√ N≥i
因此,迭代i的所有可能值从1√ N,并在所有可能的对(i,j)中,选择i + j – 2的最小值,这是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum number
// of moves required to reach the cell
// containing N starting from (1, 1)
int min_moves(int n)
{
    // To store the required answer
    int ans = INT_MAX;
  
    // For all possible values of divisors
    for (int i = 1; i * i <= n; i++) {
  
        // If i is a divisor of n
        if (n % i == 0) {
  
            // Get the moves to reach n
            ans = min(ans, i + n / i - 2);
        }
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    int n = 10;
  
    cout << min_moves(n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the minimum number
// of moves required to reach the cell
// containing N starting from (1, 1)
static int min_moves(int n)
{
    // To store the required answer
    int ans = Integer.MAX_VALUE;
  
    // For all possible values of divisors
    for (int i = 1; i * i <= n; i++) 
    {
  
        // If i is a divisor of n
        if (n % i == 0)
        {
  
            // Get the moves to reach n
            ans = Math.min(ans, i + n / i - 2);
        }
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10;
  
    System.out.println(min_moves(n));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach 
import sys
  
from math import sqrt
  
# Function to return the minimum number 
# of moves required to reach the cell 
# containing N starting from (1, 1) 
def min_moves(n) :
  
    # To store the required answer 
    ans = sys.maxsize; 
  
    # For all possible values of divisors 
    for i in range(1, int(sqrt(n)) + 1) :
  
        # If i is a divisor of n 
        if (n % i == 0) :
  
            # Get the moves to reach n 
            ans = min(ans, i + n // i - 2);
  
    # Return the required answer 
    return ans; 
  
# Driver code 
if __name__ == "__main__" :
  
    n = 10; 
  
    print(min_moves(n)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
      
// Function to return the minimum number
// of moves required to reach the cell
// containing N starting from (1, 1)
static int min_moves(int n)
{
    // To store the required answer
    int ans = int.MaxValue;
  
    // For all possible values of divisors
    for (int i = 1; i * i <= n; i++) 
    {
  
        // If i is a divisor of n
        if (n % i == 0)
        {
  
            // Get the moves to reach n
            ans = Math.Min(ans, i + n / i - 2);
        }
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 10;
  
    Console.WriteLine(min_moves(n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
5