📌  相关文章
📜  通过反复从一堆硬币中取出 2 个硬币和从另一个中取出 1 个硬币来检查是否可以清空两堆硬币

📅  最后修改于: 2021-10-26 05:59:27             🧑  作者: Mango

给定两堆硬币,由NM枚硬币组成,任务是通过反复从一堆硬币中取出2 个硬币和从另一堆硬币中取出1 个硬币来检查是否可以同时清空这两堆硬币。如果两堆都可以清空,则打印“是” 。否则,打印“否”

例子:

方法:根据以下观察可以解决给定的问题:

  • 有两种方法可以从桩除去硬币,即,无论是从桩2移除桩1 2枚硬币和从桩2 1枚硬币或从桩1 1枚硬币2枚硬币
  • X为第一次移除的移动次数, Y为第二次移除的移动次数,则两堆剩余的硬币数量为(N – 2*X – Y)(M – 2 *Y – X)分别。
  • 执行(X + Y)移动后,必须清空桩,即,
  • 两堆硬币的总数是(N + M) = (2*X + Y) + (X + 2*Y) = 3(X + Y)
  • 因此,要使两个桩都为空,以下是条件:
    • 两堆硬币的总和应该是 3 的倍数。
    • NM最大必须小于最小的NM的两倍一个一大堆将成为空和一些硬币仍然会在另外一个不留。因此,较大桩的尺寸不应超过较小桩尺寸的两倍。

因此,想法是检查硬币总数的总和是否为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)