fork download
  1. #include <iostream>
  2. #include <array>
  3. using namespace std;
  4.  
  5. int main() {
  6. array<float,4> hps{20, 20, 40, 50};
  7. array<float,5> damages={30, 20, 10, 24, 47};
  8. for(float d : damages) {
  9. float hpsum = 0;
  10. for(float h : hps)
  11. hpsum += h;
  12. if(d > hpsum) {
  13. cout << "all dead";
  14. return 0;
  15. }
  16. float div = hpsum / hps.size();
  17. for(int i=0; i<hps.size(); i++) {
  18. float dmg = d/(hps.size()-i);
  19. if(hps[i] < dmg)
  20. dmg = hps[i];
  21. hps[i] -= dmg;
  22. d -= dmg;
  23. }
  24. cout << "hps: ";
  25. for(float h : hps)
  26. cout << " " << h;
  27. cout << " == all alive" << endl;
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
hps:  12.5 12.5 32.5 42.5 == all alive
hps:  7.5 7.5 27.5 37.5 == all alive
hps:  5 5 25 35 == all alive
hps:  0 0 18 28 == all alive
all dead