fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int maxUnitCalculate(vector<int> req, vector<int> stock, vector<int> cost, int budget){
  5. int n = req.size();
  6.  
  7. int i = 1, answer = 0;
  8. bool val = true;
  9. while(val){
  10. int sum = 0;
  11. for (int j = 0; j < n; j++){
  12. int temp = (req[j] * i )-stock[j];
  13. if(temp < 0) temp = 0;
  14.  
  15. sum += (temp * cost[j]);
  16. }
  17.  
  18. if(sum <= budget){
  19. answer = i;
  20. i++;
  21. if(sum == budget) break;
  22. }
  23. else {
  24. val = false;
  25. break;
  26. }
  27. }
  28.  
  29. return answer;
  30.  
  31.  
  32.  
  33. }
  34.  
  35. int main() {
  36. // your code goes here
  37. vector<int> req = {1, 2};
  38. vector<int> stock = {0, 1};
  39. vector<int> cost = {1, 1};
  40.  
  41. cout << maxUnitCalculate(req, stock, cost, 5);
  42.  
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
2