Guest User

Untitled

a guest
Jul 22nd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <bits/stdc++.h>
  6. #include <unistd.h>
  7.  
  8. using namespace std;
  9.  
  10. struct BirdData {
  11. int price;
  12. double dailyEarn;
  13. };
  14.  
  15. struct EarnForPeriod {
  16. double earn;
  17. double money;
  18. double dailyEarn;
  19. std::vector<string> active;
  20. };
  21.  
  22. BirdData birds[6];
  23. int maxDays = 0;
  24.  
  25. EarnForPeriod getEarnForPeriod(int day, double dailyEarn, double money, int level = 1);
  26.  
  27. int main(int argc, char *argv[]) {
  28.  
  29. maxDays = stoi(argv[1]);
  30. int money = stoi(argv[2]);
  31.  
  32. birds[0] = {1000, 23.352 / 2};
  33. birds[1] = {500, 11.16 / 2};
  34. birds[2] = {100, 2.16 / 2};
  35. birds[3] = {50, 0.816 / 2};
  36. birds[4] = {10, 0.096 / 2};
  37. birds[5] = {3, 0.024 / 2};
  38.  
  39. int dailyEarn = 0;
  40.  
  41. EarnForPeriod period = getEarnForPeriod(1, dailyEarn, money);
  42.  
  43. std::cout << "Earn = " << (period.earn) << std::endl;
  44. std::cout << "Money = " << (period.money) << std::endl;
  45. std::cout << "Daily earn = " << (period.dailyEarn) << std::endl;
  46.  
  47. for (const auto &chars: period.active) {
  48. std::cout << chars << std::endl;
  49. }
  50.  
  51. return 0;
  52. }
  53.  
  54. EarnForPeriod getEarnForPeriod(int day, double dailyEarn, double money, int level) {
  55.  
  56. std::vector<string> active;
  57.  
  58. int wait = 0; // что ждёт купить
  59.  
  60. double earn = 0;
  61.  
  62. for (int i = day; i <= maxDays && level < 7; i++) {
  63. if (level == 1) {
  64. std::cout << "Day " << i << std::endl;
  65. } else {//if (level == i) {
  66. // std::cout << std::string(static_cast<unsigned long>(level), ' ') << " Day " << i << " in level " << level
  67. // << std::endl;
  68. // usleep(10000);
  69. }
  70.  
  71. bool buy;
  72. if (wait <= 0) { // если еще ничего не ждет, то определяется с тем чего ждать
  73. int currentDay = i;
  74. double currentMoney = money;
  75.  
  76. int calculatedPrice = 0;
  77. double calculatedEarn = 0;
  78.  
  79. if (currentDay < maxDays) {
  80. int remainingDays = maxDays - currentDay;
  81. double earnedMoney =
  82. currentMoney + dailyEarn * remainingDays;
  83.  
  84. for (BirdData &bird : birds) {
  85. if (earnedMoney >= bird.price) {
  86. // if (bird.price < 100) {
  87. // continue;
  88. // }
  89. int remainingForBird;
  90. if (dailyEarn > 0) {
  91. remainingForBird = std::max(0, (int) (std::ceil(bird.price - currentMoney) / dailyEarn));
  92. } else {
  93. remainingForBird = 0;
  94. }
  95.  
  96. double earnByDays = dailyEarn * remainingForBird;
  97. auto countBirds = (int) std::floor((earnByDays + currentMoney) / bird.price);
  98. if (countBirds <= 0) {
  99. continue;
  100. }
  101.  
  102. EarnForPeriod result = getEarnForPeriod(
  103. currentDay + remainingForBird,
  104. dailyEarn + bird.dailyEarn * countBirds,
  105. earnByDays + currentMoney - bird.price * countBirds,
  106. level + 1
  107. );
  108.  
  109. double d = earnByDays + result.earn;
  110. if (d > calculatedEarn) {
  111. calculatedPrice = bird.price;
  112. calculatedEarn = d;
  113. if (remainingForBird <= 0) {
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if (calculatedPrice > 0) {
  121. wait = calculatedPrice;
  122. }
  123. }
  124. do {
  125. buy = false;
  126. if (wait < 0) {
  127. throw "Cannot wait nothing.";
  128. }
  129. for (BirdData &bird : birds) {
  130. if (bird.price == wait && money >= bird.price) {
  131. active.emplace_back(
  132. std::string("day ") + std::to_string(i)
  133. + ", money " + std::to_string(money)
  134. + ", buy bird " + std::to_string(bird.price)
  135. + ", earn " + std::to_string(earn)
  136. + ", daily earn " + std::to_string(dailyEarn)
  137. );
  138.  
  139. money -= bird.price;
  140. dailyEarn += bird.dailyEarn;
  141. buy = true;
  142. break;
  143. } else if (wait == bird.price) {
  144. wait = 0;
  145. break;
  146. }
  147. }
  148. } while (buy);
  149.  
  150.  
  151. earn += dailyEarn;
  152. money += dailyEarn;
  153. }
  154.  
  155. return {earn, money, dailyEarn, active};
  156. }
Add Comment
Please, Sign In to add comment