📌  相关文章
📜  将增量或减量最小化2,以将给定值转换为理想的平方

📅  最后修改于: 2021-04-21 21:22:41             🧑  作者: Mango

给定整数N ,任务是计算需要将N递增或递减2的最小次数,以将其转换为理想平方。

例子:

方法:请按照以下步骤解决此问题:

  • 计数运行的总数,说需要cntDecr2递减的N的值,使N作为一个完美的平方数。
  • 计数运行的总数,说需要cntIncr2递增N的值,使N作为一个完美的平方数。
  • 最后,打印min(cntIncr,cntDecr)的值。

下面是上述方法的实现。

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to make
// N a perfect square
int MinimumOperationReq(int N)
{
    // Stores count of operations
    // by performing decrements
    int cntDecr = 0;
 
    // Stores value of N
    int temp = N;
 
    // Decrement the value of temp
    while (temp > 0) {
 
        // Stores square root of temp
        int X = sqrt(temp);
 
        // If temp is a perfect square
        if (X * X == temp) {
            break;
        }
 
        // Update temp
        temp = temp - 2;
        cntDecr += 1;
    }
 
    // Store count of operations
    // by performing increments
    int cntIncr = 0;
 
    // Increment the value of N
    while (true) {
 
        // Stores sqrt of N
        int X = sqrt(N);
 
        // If N is a perfect square
        if (X * X == N) {
            break;
        }
 
        // Update temp
        N = N + 2;
        cntIncr += 1;
    }
 
    // Return the minimum count
    return min(cntIncr, cntDecr);
}
 
// Driver Code
int main()
{
 
    int N = 15;
    cout << MinimumOperationReq(N);
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to find the minimum number
// of operations required to make
// N a perfect square
static int MinimumOperationReq(int N)
{
     
    // Stores count of operations
    // by performing decrements
    int cntDecr = 0;
 
    // Stores value of N
    int temp = N;
     
    // Decrement the value of temp
    while (temp > 0)
    {
         
        // Stores square root of temp
        int X = (int)Math.sqrt(temp);
 
        // If temp is a perfect square
        if (X * X == temp)
        {
            break;
        }
         
        // Update temp
        temp = temp - 2;
        cntDecr += 1;
    }
 
    // Store count of operations
    // by performing increments
    int cntIncr = 0;
 
    // Increment the value of N
    while (true)
    {
         
        // Stores sqrt of N
        int X = (int)Math.sqrt(N);
 
        // If N is a perfect square
        if (X * X == N)
        {
            break;
        }
 
        // Update temp
        N = N + 2;
        cntIncr += 1;
    }
     
    // Return the minimum count
    return Math.min(cntIncr, cntDecr);
}
 
// Driver code
public static void main (String args[])
{
    int N = 15;
     
    System.out.print(MinimumOperationReq(N)); 
}
}
 
// This code is contributed by ajaykr00kj


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum number
# of operations required to make
# N a perfect square
def MinimumOperationReq(N):
   
    # Stores count of operations
    # by performing decrements
    cntDecr = 0;
 
    # Stores value of N
    temp = N;
 
    # Decrement the value of
    # temp
    while (temp > 0):
 
        # Stores square root of
        # temp
        X = int(pow(temp, 1 / 2))
         
        # If temp is a perfect
        # square
        if (X * X == temp):
            break;
 
        # Update temp
        temp = temp - 2;
        cntDecr += 1;
 
    # Store count of operations
    # by performing increments
    cntIncr = 0;
 
    # Increment the value of N
    while (True):
 
        # Stores sqrt of N
        X = int(pow(N, 1 / 2))
 
 
        # If N is a perfect
        # square
        if (X * X == N):
            break;
 
        # Update temp
        N = N + 2;
        cntIncr += 1;
 
    # Return the minimum
    # count
    return min(cntIncr,
               cntDecr);
 
# Driver code
if __name__ == '__main__':
   
    N = 15;
    print(MinimumOperationReq(N));
 
# This code is contributed by Rajput-Ji


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the minimum number
// of operations required to make
// N a perfect square
static int MinimumOperationReq(int N)
{
  // Stores count of operations
  // by performing decrements
  int cntDecr = 0;
 
  // Stores value of N
  int temp = N;
 
  // Decrement the value of
  // temp
  while (temp > 0)
  {
 
    // Stores square root of temp
    int X = (int)Math.Sqrt(temp);
 
    // If temp is a perfect square
    if (X * X == temp)
    {
      break;
    }
 
    // Update temp
    temp = temp - 2;
    cntDecr += 1;
  }
 
  // Store count of operations
  // by performing increments
  int cntIncr = 0;
 
  // Increment the value of N
  while (true)
  {
    // Stores sqrt of N
    int X = (int)Math.Sqrt(N);
 
    // If N is a perfect square
    if (X * X == N)
    {
      break;
    }
 
    // Update temp
    N = N + 2;
    cntIncr += 1;
  }
 
  // Return the minimum count
  return Math.Min(cntIncr,
                  cntDecr);
}
 
// Driver code
public static void Main(String []args)
{
  int N = 15;
  Console.Write(MinimumOperationReq(N)); 
}
}
 
// This code is contributed by shikhasingrajput


输出:
3











时间复杂度: O(N * log 2 (N))
辅助空间: O(1)