给定两堆硬币,由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)