Advertisement
35657

Untitled

Apr 26th, 2024
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include <unordered_set>
  7. #include <algorithm>
  8.  
  9.  
  10.  
  11. using namespace std;
  12.  
  13. std::unordered_set<std::vector<int>> get_triple(int A, std::vector<int>& x) {
  14.     std::unordered_set<int> history;
  15.     std::sort(x.begin(), x.end());
  16.     std::unordered_set<std::vector<int>> triples;
  17.  
  18.     for (int i = 0; i < x.size(); i++) {
  19.         for (int j = i + 1; j < x.size(); j++) {
  20.             int target = A - x[i] - x[j];
  21.             if (history.find(target) != history.end()) {
  22.                 std::vector<int> triple = { target, x[i], x[j] };
  23.                 triples.insert(triple);
  24.             }
  25.         }
  26.         history.insert(x[i]);
  27.     }
  28.  
  29.     return triples;
  30. }
  31.  
  32.  
  33. int main() {
  34.  
  35.     ifstream fin("input.txt");
  36.  
  37.     int a = 10;
  38.    
  39.     std::vector<int> vec{ 5,2,8,1,1,3,4,4 };
  40.  
  41.     std::unordered_set<std::vector<int>> result = get_triple(a, vec);
  42.  
  43.     for (auto& triple : result) {
  44.         for (auto element : triple) {
  45.             cout << element << " ";
  46.         }
  47.         cout << endl;
  48.     }
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement