📌  相关文章
📜  通过给定的X和Y操作将N减少到0或更小

📅  最后修改于: 2021-04-27 17:31:54             🧑  作者: Mango

给定三个整数NXY,任务是检查是否可以通过以下操作将N减小到0或更小:

  • N更新为⌊N/2⌋+ 10,最多X
  • 更新NN – 10,最多y倍的情况

例子:

方法:可以根据以下观察结果解决此问题:

  • 情况1:如果⌊N/2⌋+ 10> = N ,则仅执行第二次运算,因为第一次运算将增加值N。
  • 情况2:如果先执行第一个操作,然后执行第二个操作,则结果为:
  • N的值减小为(⌊N/2⌋)
  • 情况3:如果先执行第二项操作,然后执行第一项操作,则结果为:
  • N的值减小为(⌊N/2⌋+ 5)

从以上两个观察,如果N> N / 2 +10 ,则可以得出结论,为了将N的值减小到0或更小,必须在执行第二个操作以减小N的值之前执行第一个操作。
请按照以下步骤解决问题:

  1. N的值更新为⌊N/2⌋+ 10如果N> N / 2 + 10和X> 0。
  2. 更新的NN的值– 10最多y倍的情况。
  3. 检查N的更新值是否小于等于0。
  4. 如果N≤0,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if N can be
// reduced to <= 0 or not
bool NegEqu(int N, int X, int Y)
{
 
    // Update N to N/2 + 10 at most X times
    while (X-- and N > N/2 + 10) {
        N = N / 2 + 10;
    }
 
    // Update N to N - 10 Y times
    while (Y--) {
        N = N - 10;
    }
 
    if (N <= 0)
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int N = 100;
    int X = 3;
    int Y = 4;
 
    if (NegEqu(N, X, Y)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to check if N can be
// reduced to <= 0 or not
static boolean NegEqu(int N, int X, int Y)
{
     
    // Update N to N/2 + 10 at most X times
    while (X > 0 && (N > N / 2 + 10))
    {
        N = N / 2 + 10;
        X -= 1;
    }
 
    // Update N to N - 10 Y times
    while (Y > 0)
    {
        N = N - 10;
        Y -= 1;
    }
 
    if (N <= 0)
        return true;
 
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 100;
    int X = 3;
    int Y = 4;
 
    if (NegEqu(N, X, Y))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program to implement
# the above approach
 
# Function to check if N can be
# reduced to <= 0 or not
def NegEqu(N, X, Y):
 
    # Update N to N/2 + 10 at most X times
    while (X and N > N // 2 + 10):
        N = N // 2 + 10
        X -= 1
 
    # Update N to N - 10 Y times
    while (Y):
        N = N - 10
        Y -= 1
 
    if (N <= 0):
        return True
 
    return False
 
# Driver Code
if __name__ == '__main__':
     
    N = 100
    X = 3
    Y = 4
 
    if (NegEqu(N, X, Y)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check if N can be
// reduced to <= 0 or not
public static bool NegEqu(int N, int X,
                          int Y)
{
     
    // Update N to N/2 + 10 at most X times
    while (X > 0 && (N > N / 2 + 10))
    {
        N = N / 2 + 10;
        X -= 1;
    }
 
    // Update N to N - 10 Y times
    while (Y > 0)
    {
        N = N - 10;
        Y -= 1;
    }
 
    if (N <= 0)
        return true;
 
    return false;
}
     
// Driver Code
public static void Main(String[] args)
{
    int N = 100;
    int X = 3;
    int Y = 4;
 
    if (NegEqu(N, X, Y))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by jana_sayantan


Javascript


输出:
Yes

时间复杂度: O(X + Y)
辅助空间:O(1)