Notes and coins

You are given an M number of coins and a P number of notes.

Write a program to separate the two forms of money without creating two separate classes for notes and coins.

(The order of the output should be the same as that of the input).

Example:

Input:  n=7, coins={{"Note",83},{"Coin",19},{"Coin",85},{"Note",8},{"Note",30},{"Coin",56},{"Coin",53}}

Output:

Coins : 19 85 56 53 Notes : 83 8 30

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void notesCoins(int nvector<pair<stringint>> &coins)
{
    string s;
    vector<intvcvn;
    for (int i = 0i < ni++)
    {
        if (coins[i].first == "Coin")
            vc.push_back(coins[i].second);
        else
            vn.push_back(coins[i].second);
    }
    if (vc.size() > 0)
    {
        cout << "Coins :\n";
        for (int i = 0i < vc.size(); i++)
            cout << vc[i] << "\n";
    }
    if (vn.size() > 0)
    {
        cout << "Notes :\n";
        for (int i = 0i < vn.size(); i++)
            cout << vn[i] << "\n";
    }
}
int main()
{
    int n = 7;
    vector<pair<stringint>> coins = {{"Note"83},
                                       {"Coin"19},
                                       {"Coin"85},
                                       {"Note"8},
                                       {"Note"30},
                                       {"Coin"56},
                                       {"Coin"53}};

    notesCoins(ncoins);

    return 0;
}


No comments:

Post a Comment