Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<utility>
  4. #include<fstream>
  5.  
  6. using namespace std;
  7.  
  8. class piggy_bank{
  9. private: int weight;
  10. int null_weight;
  11. int coins_cout;
  12. vector<pair<int,int>> coins;
  13.  
  14. vector<float> coins_wc;
  15. public: piggy_bank(int, int, int, vector<pair<int,int>>);
  16. void calculate_wc();
  17. void out();
  18. int result();
  19. };
  20.  
  21. using namespace std;
  22.  
  23. int main(){
  24. ifstream input("input.txt");
  25. ofstream output("output.txt");
  26.  
  27. int weight, null_weight, coins_cout;
  28. vector<pair<int,int>> coins;
  29. input >> null_weight >> weight >> coins_cout;
  30. for ( auto i =0 ; i < coins_cout; i++ ){
  31. int coin_weight, coin_cost;
  32. input >> coin_weight >> coin_cost;
  33. coins.push_back(make_pair(coin_weight,coin_cost));
  34. }
  35.  
  36. piggy_bank bank = piggy_bank(weight, null_weight, coins_cout,coins);
  37. bank.calculate_wc();
  38. //bank.out();
  39. output << bank.result();
  40.  
  41. input.close();
  42. output.close();
  43. return 0;
  44. }
  45.  
  46. piggy_bank::piggy_bank(int w, int n, int c, vector<pair<int,int>> co){
  47. weight = w; null_weight = n; coins_cout = c; coins = co;
  48. }
  49.  
  50. void piggy_bank::calculate_wc(){
  51. for ( auto i = 0 ; i < coins_cout; i++ ){
  52. coins_wc.push_back(float(coins[i].first)/float(coins[i].second));
  53. }
  54. coins_wc.push_back(10001);
  55. }
  56.  
  57. int piggy_bank::result(){
  58. int result=0;
  59. int current_weight=0;
  60. int sim = weight - null_weight;
  61. do{
  62. float min_wc = coins_cout;
  63. for ( auto i = 0 ; i < coins_cout; i++ ){
  64. if ( coins_wc[i] < coins_wc[min_wc] ) min_wc = i;
  65. }
  66. current_weight += coins[min_wc].second;
  67. result += coins[min_wc].first;
  68. //cout << current_weight << endl;
  69. //cout << result << endl;
  70. if (coins_wc[min_wc]==10001){
  71. return -1;
  72. }
  73. if (current_weight > sim){
  74. current_weight -=coins[min_wc].second;
  75. result -=coins[min_wc].first;
  76. coins_wc[min_wc] = 10001;
  77. }
  78. }while (current_weight!=sim);
  79.  
  80. return result;
  81. }
  82.  
  83. void piggy_bank::out(){
  84. cout << "Вес пустой копилки:\t" <<null_weight << endl << "Вес копилки:\t" << weight << endl <<"Количество монет\t" << coins_cout << endl;
  85. for ( auto i = 0 ; i < coins_cout; i++ )
  86. cout << "Стоимость монеты\t" << coins[i].first << "\t Вес монеты\t" << coins[i].second << "\t Стоимость на вес:\t" <<coins_wc[i]<< endl;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement