Init
This commit is contained in:
commit
6b94ffff53
|
@ -0,0 +1,82 @@
|
|||
#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';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
//{
|
||||
//0 - 10: 0.5,
|
||||
//11 - 50: 0.10,
|
||||
// 51 - 100: 0.20,
|
||||
//}
|
||||
|
||||
// NET: 75
|
||||
// 10 * 0.5 + 40 * 0.10 + 25 * 0.20 = answer
|
||||
|
||||
constexpr int NET_INFINITY = -1;
|
||||
|
||||
double calculateTax(int net_sum, const std::map<double, int>& tax_table)
|
||||
{
|
||||
double overall_tax = .0;
|
||||
int lower_bound = 0;
|
||||
|
||||
for (const auto& tax_entry : tax_table)
|
||||
{
|
||||
const auto& upper_bound = tax_entry.second;
|
||||
const auto& percentage = tax_entry.first;
|
||||
|
||||
if (net_sum < upper_bound || upper_bound == NET_INFINITY)
|
||||
{
|
||||
overall_tax += (net_sum - lower_bound) * percentage;
|
||||
break;
|
||||
}
|
||||
|
||||
overall_tax += (upper_bound - lower_bound) * percentage;
|
||||
lower_bound = upper_bound;
|
||||
}
|
||||
|
||||
return overall_tax;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto net_sum = 58000;
|
||||
const std::map<double, int> tax_table =
|
||||
{
|
||||
{ 0.10, 11000 },
|
||||
{ 0.12, 44725 },
|
||||
{ 0.22, NET_INFINITY }
|
||||
};
|
||||
|
||||
std::cout << "net: " << net_sum << std::endl;
|
||||
std::cout << calculateTax(net_sum, tax_table) << std::endl;
|
||||
}
|
Loading…
Reference in New Issue