给定两堆由N和M数量的硬币组成的硬币,任务是检查是否可以通过反复从一堆中取出2个硬币和从另一堆中取出1个硬币来同时清空两堆硬币。如果两堆都可以放空,则打印“是” 。否则,打印“否” 。
例子:
Input: N = 1, M = 2
Output: Yes
Explanation:
Remove 1 coin from 1st pile and 2 coins from the other pile. Therefore, the number of coins in both the piles is 0.
Input: N = 2, M = 2
Output: No
方法:可以根据以下观察结果解决给定问题:
- 有两种方法可以从桩除去硬币,即,无论是从桩2移除桩1 2枚硬币和从桩2 1枚硬币或从桩1 1枚硬币和2枚硬币。
- 令X为第一次移出的移动次数, Y为第二次移出的移动次数,则两堆硬币的剩余数量分别为(N – 2 * X – Y)和(M – 2 * Y – X)分别。
- 执行(X + Y)移动后,必须清空桩,即
(N – 2*X – Y) = 0 and (M – 2*Y – X) = 0
Rearranging terms in the above equations generates the equations:
N = 2*X + Y and M = 2*Y + X
- 两堆中的硬币总数为(N + M) = (2 * X + Y)+(X + 2 * Y)= 3(X + Y) 。
- 因此,要使两个桩都变空,请满足以下条件:
- 两堆硬币的总和应为3的倍数。
- N和M最大必须小于最小的N和M的两倍一个一大堆将成为空和一些硬币仍然会在另外一个不留。因此,较大桩的尺寸不应大于较小桩的尺寸的两倍。
因此,该想法是检查硬币总数的总和是否为3的倍数,并且最大硬币数目最多为最小硬币数目的两倍,然后打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if two given piles
// can be emptied by repeatedly removing
// 2 coins from a pile and 1 coin from the other
void canBeEmptied(int A, int B)
{
// If maximum of A & B exceeds
// the twice of minimum of A & B
if (max(A, B) > 2 * min(A, B)) {
// Not possible to
// empty the piles
cout << "No";
return;
}
// If sum of both the coins is
// divisible by 3, then print Yes
if ((A + B) % 3 == 0)
cout << "Yes";
// Otherwise, print "No"
else
cout << "No";
}
// Driver Code
int main()
{
int A = 1, B = 2;
canBeEmptied(A, B);
return 0;
}
Java
// Java program for above approach
import java.util.*;
class GFG{
// Function to check if two given piles
// can be emptied by repeatedly removing
// 2 coins from a pile and 1 coin from the other
static void canBeEmptied(int A, int B)
{
// If maximum of A & B exceeds
// the twice of minimum of A & B
if (Math.max(A, B) > 2 * Math.min(A, B))
{
// Not possible to
// empty the piles
System.out.println("No");
return;
}
// If sum of both the coins is
// divisible by 3, then print Yes
if ((A + B) % 3 == 0)
System.out.println("Yes");
// Otherwise, print "No"
else
System.out.println("No");
}
// Driver Code
public static void main (String[] args)
{
int A = 1, B = 2;
canBeEmptied(A, B);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to check if two given piles
# can be emptied by repeatedly removing
# 2 coins from a pile and 1 coin from the other
def canBeEmptied(A, B):
# If maximum of A & B exceeds
# the twice of minimum of A & B
if (max(A, B) > 2 * min(A, B)):
# Not possible to
# empty the piles
print("No")
return
# If sum of both the coins is
# divisible by 3, then print Yes
if ((A + B) % 3 == 0):
print("Yes")
# Otherwise, print "No"
else:
print("No")
# Driver Code
if __name__ == '__main__':
A = 1
B = 2
canBeEmptied(A, B)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if two given piles
// can be emptied by repeatedly removing
// 2 coins from a pile and 1 coin from the other
static void canBeEmptied(int A, int B)
{
// If maximum of A & B exceeds
// the twice of minimum of A & B
if (Math.Max(A, B) > 2 * Math.Min(A, B))
{
// Not possible to
// empty the piles
Console.WriteLine("No");
return;
}
// If sum of both the coins is
// divisible by 3, then print Yes
if ((A + B) % 3 == 0)
Console.WriteLine("Yes");
// Otherwise, print "No"
else
Console.WriteLine("No");
}
// Driver Code
public static void Main ()
{
int A = 1, B = 2;
canBeEmptied(A, B);
}
}
// This code is contributed by mohit kumar 29
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)