📜  coinPiles - C++ (1)

📅  最后修改于: 2023-12-03 14:59:58.706000             🧑  作者: Mango

CoinPiles - C++

Introduction

CoinPiles is a C++ program that helps to solve a problem related to coin piles. Given two piles of coins, this program determines whether it is possible to take some coins from each pile so that the number of coins in both piles is equal, and if yes, what is the minimum number of coins to be removed from both the piles.

Usage
Input Format

The input contains two integers, a and b, representing the number of coins in the two piles.

Output Format

The output contains a single line with a "YES" or "NO" indicating whether it is possible to take some coins from each pile so that the number of coins in both piles is equal. If the answer is "YES", then the following line contains a single integer, representing the minimum number of coins to be removed from both piles.

Example

Input:

3 5

Output:

YES
2
Code
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    // cin>>t;
    while(t--){
        ll a,b;
        cin>>a>>b;
        if(b<a) swap(a,b);
        if((a+b)%3==0 && (2*a>=b)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

The above code uses two variables, a and b, to store the number of coins in both piles. The algorithm first checks if the sum of a and b is divisible by 3 and if 2*a is greater than or equal to b. If both conditions are true, then it is possible to take some coins from each pile so that the number of coins in both piles is equal. Otherwise, it is not possible.

Conclusion

CoinPiles is a simple yet important C++ program that helps to solve the coin piles problem. It uses a straightforward algorithm to determine whether it is possible to take some coins from each pile so that the number of coins in both piles is equal and if yes, what is the minimum number of coins to be removed from both the piles.