code-snippets/cpp/even_payroll/even_payroll.cpp

82 lines
1.6 KiB
C++

#include <iostream>
#include <vector>
#include <algorithm>
/*
collectors:
{
a: 10,
b: 25,
c: 50,
d: 100
}
after selling kidney: 49
so it's:
1. 49 - 10 = 39
2. 39 - 25 = 14
3. 14 - 50 ??? NO. its just = (14 / remaining which is 2) = 7
4. also just 7
*/
struct Collector
{
int money = 0;
std::string name;
bool operator<(const Collector &collector) const
{
return money < collector.money;
}
};
std::vector<Collector> calculatePayments(std::vector<Collector> debts, int budget)
{
int remaining_budget = budget;
int remaining_collectors = debts.size();
std::vector<Collector> payments;
if (budget < remaining_collectors)
{
return payments;
}
std::sort(debts.begin(), debts.end());
payments.reserve(remaining_collectors);
for (const auto& debt_record : debts)
{
const auto& debt_amount = debt_record.money;
const auto& debt_collector = debt_record.name;
const int even_distribution = remaining_budget / remaining_collectors;
int spending = (even_distribution <= debt_amount)
? even_distribution
: debt_amount;
payments.push_back({spending, debt_collector});
remaining_budget -= spending;
--remaining_collectors;
}
return payments;
}
int main()
{
const std::vector<Collector> debts =
{
{ 100, "Suika" },
{ 250, "Remilia" },
{ 1, "Reimu" },
{ 20, "Sakuya" }
};
const int budget = 67;
for (const auto& payment : calculatePayments(debts, budget))
{
std::cout << payment.name << ": " << payment.money << '\n';
}
}