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

📅  最后修改于: 2021-10-25 09:21:59             🧑  作者: 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)

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