📌  相关文章
📜  检查是否可以使用最多 X 增量和最多 Y 倍增操作从 0 获得 N

📅  最后修改于: 2022-05-13 01:56:09.195000             🧑  作者: Mango

检查是否可以使用最多 X 增量和最多 Y 倍增操作从 0 获得 N

给定三个整数NXY ,任务是检查是否可以使用以下操作从0获得N

  • 当前整数可以加一(即 x = x + 1),并且最多可以这样做X 次
  • 将当前整数加倍(即 x = 2 * x),最多可以完成Y 次

如果无法到达该数字,则返回 false。

例子:

方法:这个问题也可以在完全相反的情况下考虑,其中N给定,我们需要通过使用两个操作将其减少到 0:

  • 将目标除以 2 最多为 maxDoubles 次
  • 将目标减 1 与 maxadd 次一样多。

以这种方式使用递归函数来检查是否可以得到0 。在每个递归场景中减 1 并再次调用该函数。如果数字是偶数,则除以 2 并调用递归函数。如果在给定的移动限制内可以获得 0,则返回 true。否则,返回假。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if N is reached
bool checktogetTarget(int N, int X, int Y)
{
    // If N is already zero return true
    if (N == 0)
        return true;
     
    // If can't double and increment,
    // just return false
    if (Y == 0 && X == 0)
        return false;
    int temp = N;
    while (1) {
         
        // If N is not divisible by 2,
        // then just decrement it by 1
        if (temp % 2 == 0 && Y != 0) {
            temp = temp / 2;
            Y--;
        }
        else if (X != 0) {
            temp--;
            X--;
        }
        if (temp == 0)
            break;
        if (Y == 0 && X == 0)
            break;
    }
   
    // if temp becomes 0 after
    // performing operation
    if (temp == 0) {
        return true;
    }
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    int N = 24;
    int X = 6;
    int Y = 2;
    bool ans = checktogetTarget(N, X, Y);
    if (ans)
        cout << "true";
    else
        cout << "false";
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to check if N is reached
  static Boolean checktogetTarget(int N, int X, int Y)
  {
    // If N is already zero return true
    if (N == 0)
      return true;
 
    // If can't double and increment,
    // just return false
    if (Y == 0 && X == 0)
      return false;
    int temp = N;
    while (true) {
 
      // If N is not divisible by 2,
      // then just decrement it by 1
      if (temp % 2 == 0 && Y != 0) {
        temp = temp / 2;
        Y--;
      }
      else if (X != 0) {
        temp--;
        X--;
      }
      if (temp == 0)
        break;
      if (Y == 0 && X == 0)
        break;
    }
 
    // if temp becomes 0 after
    // performing operation
    if (temp == 0) {
      return true;
    }
    else {
      return false;
    }
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 24;
    int X = 6;
    int Y = 2;
    Boolean ans = checktogetTarget(N, X, Y);
    if (ans)
      System.out.print("true");
    else
      System.out.print("false");
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function to check if N is reached
def checktogetTarget(N, X, Y):
   
    # If N is already zero return true
    if (N == 0):
        return True;
 
    # If can't double and increment,
    # just return false
    if (Y == 0 and X == 0):
        return False;
    temp = N;
    while (1):
 
        # If N is not divisible by 2,
        # then just decrement it by 1
        if (temp % 2 == 0 and Y != 0):
            temp = temp / 2;
            Y -= 1
        elif (X != 0):
            temp -= 1
            X -= 1
        if (temp == 0):
            break;
        if (Y == 0 and X == 0):
            break;
 
    # if temp becomes 0 after
    # performing operation
    if (temp == 0):
        return True
    else:
        return False
 
# Driver Code
 
N = 24;
X = 6;
Y = 2;
ans = checktogetTarget(N, X, Y);
if (ans):
    print("true");
else:
    print("false");
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for above approach
using System;
class GFG
{
 
  // Function to check if N is reached
  static bool checktogetTarget(int N, int X, int Y)
  {
 
    // If N is already zero return true
    if (N == 0)
      return true;
 
    // If can't double and increment,
    // just return false
    if (Y == 0 && X == 0)
      return false;
    int temp = N;
    while (true) {
 
      // If N is not divisible by 2,
      // then just decrement it by 1
      if (temp % 2 == 0 && Y != 0) {
        temp = temp / 2;
        Y--;
      }
      else if (X != 0) {
        temp--;
        X--;
      }
      if (temp == 0)
        break;
      if (Y == 0 && X == 0)
        break;
    }
 
    // if temp becomes 0 after
    // performing operation
    if (temp == 0) {
      return true;
    }
    else {
      return false;
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 24;
    int X = 6;
    int Y = 2;
    bool ans = checktogetTarget(N, X, Y);
    if (ans)
      Console.Write("true");
    else
      Console.Write("false");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
true

时间复杂度: 在)
辅助空间: O(1)