fork(1) download
  1. //! (c) WhiZTiM __ionogu(<_at_)>acm.org
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct Triples {
  8. Triples() = default;
  9. Triples(unsigned aa, unsigned bb, unsigned cc): a(aa), b(bb), c(cc) {}
  10. unsigned a=0; unsigned b=0; unsigned c=0;
  11. };
  12. using vTriples = vector<Triples>;
  13.  
  14. vTriples triples(size_t lower, size_t max){
  15. vTriples ans;
  16. for(size_t x=lower; x<max; x++)
  17. for(size_t y=x; y<max; y++)
  18. for(size_t z=y; z<max; z++)
  19. if(x*x + y*y == z*z)
  20. ans.emplace_back(Triples(x, y, z));
  21. return ans;
  22. }
  23.  
  24. Triples triples_for(const vTriples& vt, size_t num){
  25. auto rtn = find_if(vt.begin(), vt.end(),
  26. [&num](const Triples& t){ return num == (t.a + t.b + t.c); });
  27. return rtn != vt.end() ? *rtn : Triples();
  28. }
  29.  
  30.  
  31. int main() {
  32. auto tp = triples(1, 1000);
  33. auto t = triples_for(tp, 1000);
  34. cout << t.a << ", " << t.b << ", " << t.c << '\n';
  35. cout << "Product: " << t.a * t.b * t.c << '\n';
  36. return 0;
  37. }
Success #stdin #stdout 0.31s 3228KB
stdin
Standard input is empty
stdout
200, 375, 425
Product: 31875000