problemscpp
A collection of my answers to algorithm problems in c++.
函数
pat::b::b1020 命名空间参考

1020 月饼 更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1020, case1)
 

详细描述

1020 月饼

函数说明

◆ main()

int pat::b::b1020::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp691 行定义.

691 {
692 int n;
693 int d;
694 cin >> n >> d;
695 vector<long double> storage(n);
696 vector<long double> sales(n);
697 vector<pair<long double, int>> unit_price(n);
698 for(int i = 0; i < n; i++) {
699 cin >> storage[i];
700 }
701 for(int i = 0; i < n; i++) {
702 cin >> sales[i];
703 }
704 for(int i = 0; i < n; i++) {
705 unit_price[i] = make_pair(sales[i] / storage[i], i);
706 }
707 sort(unit_price.begin(), unit_price.end(), [](const pair<long double, int> &a, const pair<long double, int> &b) { return a.first > b.first; });
708 int current_amount = 0;
709 long double ans = 0;
710 for(int i = 0; i < n && current_amount < d; i++) {
711 int amount = 0;
712 if(current_amount + storage[unit_price[i].second] > d) {
713 amount = d - current_amount;
714 current_amount = d;
715 ans += amount * sales[unit_price[i].second] / storage[unit_price[i].second];
716 } else {
717 amount = storage[unit_price[i].second];
718 ans += sales[unit_price[i].second];
719 current_amount += storage[unit_price[i].second];
720 }
721 }
722 cout << fixed << setprecision(2) << ans;
723 return 0;
724 }

被这些函数引用 TEST().

◆ TEST()

pat::b::b1020::TEST ( b1020  ,
case1   
)

在文件 pat_test.cpp302 行定义.

302 {
303 istringstream in("3 20\n18 15 10\n75 72 45\n");
304 auto out = ostringstream();
305 main(in, out);
306 const auto ans = out.str();
307 ASSERT_EQ("94.50", out.str());
308 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().