fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _3Sum_323
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. while (true)
  12. {
  13. string input = Console.ReadLine();
  14. if (input.Equals("exit"))
  15. {
  16. return;
  17. }
  18. List<int> inArr = input.Split(' ').Select(n => Convert.ToInt32(n)).ToList();
  19.  
  20. var results = new List<List<int>>();
  21.  
  22. for (int i = 0; i < inArr.Count; i++)
  23. {
  24. // Creating duplicate list minus the outer number
  25. var inArr2 = new List<int>(inArr);
  26. inArr2.Remove(inArr[i]);
  27.  
  28. for (int j = 0; j < inArr2.Count; j++)
  29. {
  30. // Creating duplicate list minus the outer number
  31. var inArr3 = new List<int>(inArr2);
  32. inArr3.Remove(inArr2[j]);
  33.  
  34. for (int k = 0; k < inArr3.Count; k++)
  35. {
  36. // Checks if they all add up to zero
  37. if ((inArr[i] + inArr2[j] + inArr3[k]) == 0)
  38. {
  39. // Checks if the cominantion already exists in the list as a separate permutation
  40. bool alreadyUsed = false;
  41. foreach (var set in results)
  42. {
  43. if (set.Contains(inArr[i]) && set.Contains(inArr2[j]) && set.Contains(inArr3[k]))
  44. {
  45. alreadyUsed = true;
  46. }
  47. }
  48. if (!alreadyUsed)
  49. {
  50. results.Add(new List<int> { inArr[i], inArr2[j], inArr3[k] });
  51. }
  52. }
  53. }
  54. }
  55. }
  56. foreach (var trip in results)
  57. {
  58. Console.WriteLine(trip[0] + " " + trip[1] + " " + trip[2]);
  59. }
  60. Console.WriteLine("");
  61. }
  62. }
  63. }
  64. }
Success #stdin #stdout 0.01s 131456KB
stdin
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
exit
stdout
4 -1 -3
4 -5 1
5 -2 -3
5 -7 2
2 -3 1

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

-5 -4 9
-1 2 -1