fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <set>
  5. #include <tuple>
  6.  
  7. int main()
  8. {
  9. std::string line;
  10. while(getline(std::cin, line))
  11. {
  12. std::stringstream ss(line);
  13. std::multiset<int> mset;
  14. int n;
  15. while(ss >> n)
  16. mset.insert(n);
  17.  
  18. std::set<std::tuple<int, int, int>> triplets;
  19. const std::vector<int> numbers(mset.begin(), mset.end());
  20. for(size_t i = 0; i < numbers.size() - 3; i++)
  21. {
  22. size_t start = i + 1, end = numbers.size() - 1;
  23. while(start < end)
  24. {
  25. int sum = numbers[i] + numbers[start] + numbers[end];
  26. if(sum == 0)
  27. {
  28. triplets.insert(std::make_tuple(numbers[i], numbers[start], numbers[end]));
  29. end--;
  30. continue;
  31. }
  32. sum > 0 ? end-- : start++;
  33. }
  34. }
  35.  
  36. for(const auto& t: triplets)
  37. {
  38. std::cout << std::get<0>(t) << ' ';
  39. std::cout << std::get<1>(t) << ' ';
  40. std::cout << std::get<2>(t) << std::endl;
  41. }
  42. std::cout << std::endl;
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 15248KB
stdin
9 -6 -5 9 8 3 -4 8 1 7 -4 9 -9 1 9 -9 9 4 -6 -8
	4 5 -1 -2 -7 2 -5 -3 -7 -3 1
	-1 -6 -3 -7 5 -8 2 -8 1
	-5 -1 -4 2 9 -9 -6 -1 -7
stdout
-9 1 8
-8 1 7
-5 -4 9
-5 1 4
-4 -4 8
-4 1 3

-7 2 5
-5 1 4
-3 -2 5
-3 -1 4
-3 1 2

-7 2 5
-6 1 5
-3 1 2

-5 -4 9
-1 -1 2