给定三个整数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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。