给定三个整数N , X和Y,任务是检查是否可以通过以下操作将N减小到0或更小:
- 将N更新为⌊N/2⌋+ 10,最多X次
- 更新N到N – 10,最多y倍的情况。
例子:
Input: N = 100, X = 3, Y = 4
Output: Yes
Explanation:
Update N = 100 to ⌊100/2⌋ + 10 = 60.
Update N = 60 to 60 − 10 = 50.
Update N = 50 to ⌊50/2⌋ + 10 = 35.
Update N = 35 to ⌊35/2⌋ + 10 = 27.
Update N = 27 to 27 − 10 = 17.
Update N = 17 to 17 − 10 = 7.
Update N = 7 to 7 − 10 = −3.
Input: N = 50, X = 1, Y = 2
Output: No
方法:可以根据以下观察结果解决此问题:
- 情况1:如果⌊N/2⌋+ 10> = N ,则仅执行第二次运算,因为第一次运算将增加值N。
- 情况2:如果先执行第一个操作,然后执行第二个操作,则结果为:
Operation 1: N = ⌊N/2⌋ + 10
Operation 2: (⌊N/2⌋ + 10) – 10 = ⌊N/2⌋
- N的值减小为(⌊N/2⌋) 。
- 情况3:如果先执行第二项操作,然后执行第一项操作,则结果为:
Operation 2: N = N – 10
Operation 1: ⌊(N – 10)/2⌋ + 10 = (⌊N/2⌋ – 5 + 10) = (⌊N/2⌋ + 5)
- N的值减小为(⌊N/2⌋+ 5) 。
从以上两个观察,如果N> N / 2 +10 ,则可以得出结论,为了将N的值减小到0或更小,必须在执行第二个操作以减小N的值之前执行第一个操作。
请按照以下步骤解决问题:
- N的值更新为⌊N/2⌋+ 10如果N> N / 2 + 10和X> 0。
- 更新的N到N的值– 10最多y倍的情况。
- 检查N的更新值是否小于等于0。
- 如果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)