Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<utility>
- #include<fstream>
- using namespace std;
- class piggy_bank{
- private: int weight;
- int null_weight;
- int coins_cout;
- vector<pair<int,int>> coins;
- vector<float> coins_wc;
- public: piggy_bank(int, int, int, vector<pair<int,int>>);
- void calculate_wc();
- void out();
- int result();
- };
- using namespace std;
- int main(){
- ifstream input("input.txt");
- ofstream output("output.txt");
- int weight, null_weight, coins_cout;
- vector<pair<int,int>> coins;
- input >> null_weight >> weight >> coins_cout;
- for ( auto i =0 ; i < coins_cout; i++ ){
- int coin_weight, coin_cost;
- input >> coin_weight >> coin_cost;
- coins.push_back(make_pair(coin_weight,coin_cost));
- }
- piggy_bank bank = piggy_bank(weight, null_weight, coins_cout,coins);
- bank.calculate_wc();
- //bank.out();
- output << bank.result();
- input.close();
- output.close();
- return 0;
- }
- piggy_bank::piggy_bank(int w, int n, int c, vector<pair<int,int>> co){
- weight = w; null_weight = n; coins_cout = c; coins = co;
- }
- void piggy_bank::calculate_wc(){
- for ( auto i = 0 ; i < coins_cout; i++ ){
- coins_wc.push_back(float(coins[i].first)/float(coins[i].second));
- }
- coins_wc.push_back(10001);
- }
- int piggy_bank::result(){
- int result=0;
- int current_weight=0;
- int sim = weight - null_weight;
- do{
- float min_wc = coins_cout;
- for ( auto i = 0 ; i < coins_cout; i++ ){
- if ( coins_wc[i] < coins_wc[min_wc] ) min_wc = i;
- }
- current_weight += coins[min_wc].second;
- result += coins[min_wc].first;
- //cout << current_weight << endl;
- //cout << result << endl;
- if (coins_wc[min_wc]==10001){
- return -1;
- }
- if (current_weight > sim){
- current_weight -=coins[min_wc].second;
- result -=coins[min_wc].first;
- coins_wc[min_wc] = 10001;
- }
- }while (current_weight!=sim);
- return result;
- }
- void piggy_bank::out(){
- cout << "Вес пустой копилки:\t" <<null_weight << endl << "Вес копилки:\t" << weight << endl <<"Количество монет\t" << coins_cout << endl;
- for ( auto i = 0 ; i < coins_cout; i++ )
- cout << "Стоимость монеты\t" << coins[i].first << "\t Вес монеты\t" << coins[i].second << "\t Стоимость на вес:\t" <<coins_wc[i]<< endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement