fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.  
  5.  
  6. int n;
  7. cin>>n;
  8. int arr[n];
  9.  
  10. for(int i =0 ;i < n;i++)
  11. {
  12. cin>>arr[i];
  13.  
  14. }
  15. int target;
  16. cin>>target;
  17. sort(arr,arr+n);
  18.  
  19.  
  20. for(int i =0;i<n-2;i++)
  21. {
  22.  
  23. int l = i+1;
  24. int r = n-1;
  25. while(l<r)
  26. {
  27. if((arr[i] + arr[l] + arr[r]) == target)
  28. {
  29. cout<<arr[i]<<","<<" "<<arr[l]<<" "<<"and"<<" "<<arr[r]<<endl;
  30. l++;
  31. r--;
  32. }
  33.  
  34. else if((arr[i] + arr[l] + arr[r]) < target)
  35. {
  36. l++;
  37. }
  38. else //((arr[i] + arr[l] + arr[r]) > target)
  39. {
  40. r--;
  41. }
  42. }
  43.  
  44. }
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 4508KB
stdin
9
5 7 9 1 2 4 6 8 3
10
stdout
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5