commit 6b94ffff53fdfcc8cf62def628a8d5cf5a3ef4de Author: NaiJi Date: Tue Apr 9 02:30:18 2024 +0400 Init diff --git a/cpp/even_payroll/even_payroll.cpp b/cpp/even_payroll/even_payroll.cpp new file mode 100644 index 0000000..6e7dc1a --- /dev/null +++ b/cpp/even_payroll/even_payroll.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +/* +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 calculatePayments(std::vector debts, int budget) +{ + int remaining_budget = budget; + int remaining_collectors = debts.size(); + std::vector 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 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'; + } +} \ No newline at end of file diff --git a/cpp/progressive_taxes/progressive_taxes.cpp b/cpp/progressive_taxes/progressive_taxes.cpp new file mode 100644 index 0000000..3b82da1 --- /dev/null +++ b/cpp/progressive_taxes/progressive_taxes.cpp @@ -0,0 +1,50 @@ +#include +#include + +//{ + //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& 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 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; +} \ No newline at end of file