fork download
  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main() {
  5. int num,s,e,target;
  6. cin>>num;
  7. int a[1000];
  8. for(int i=0;i<num;i++)
  9. {
  10. cin>>a[i];
  11. }
  12. sort(a,a+num);
  13. cin>>target;
  14. for(int j=0;j<=(num-3);j++)
  15. {s=j;
  16. e=num-1;
  17. while(s<=e)
  18. {
  19. if((a[s]+a[e])==(target-a[j]))
  20. {
  21. cout<<a[j]<<", "<<a[s]<<" and "<<a[e]<<endl;
  22. s++;
  23. e--;
  24. }
  25. else if((a[s]+a[e])>(target-a[j]))
  26. e--;
  27. else
  28. s++;
  29. }
  30.  
  31.  
  32.  
  33.  
  34. }
  35. return 0;
  36. }
Success #stdin #stdout 0s 4716KB
stdin
9
5 7 9 1 2 4 6 8 3
10
stdout
1, 1 and 8
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 2 and 6
2, 3 and 5
2, 4 and 4
3, 3 and 4