Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Biggest_Triple
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. /*We are given n numbers, e.g. {3, 7, 2, 8, 1, 4, 6, 9}. We split them into triples: sequences of 3 consecutive numbers
  14. * (except the last sequence that could have 1 or 2 numbers). In our example, the numbers are split into these triples:
  15. * the first three numbers {3, 7, 2}; the second three numbers {8, 1, 4}; the last two numbers {6, 9}.
  16. * Write a program that enters n numbers and finds the triple with biggest sum of numbers. In our example this is the last triple:
  17. * {6, 9}. In case there are several triples with the same biggest sum, print the leftmost of them.
  18. Input
  19. The input data should be read from the console. The input data consists of exactly one line holding the input numbers,
  20. * separated one from another by a space.
  21. The input data will always be valid and in the format described. There is no need to check it explicitly.
  22. Output
  23. You have to print at the console the leftmost biggest triple as sequence of up to 3 numbers, separated by a space.
  24. Constraints
  25. • The input numbers will be integers in range [-1000 … 1000].
  26. • The number of the input numbers n will be between 1 and 1000.
  27. • Allowed work time for your program: 0.1 seconds.
  28. • Allowed memory: 16 MB.
  29. Examples
  30. Input Output
  31. 2 5 1 4 8 2 4 8 2
  32. 1 1 1 2 2 2 2
  33. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  34. 2 3 4 3 3 3 0 0 9 7 1 1 2 2 3 9 2 3 4
  35. 5 5
  36. */
  37.  
  38.  
  39. //string[] numbers = Console.ReadLine().Split(' ');
  40.  
  41. int n = int.Parse(Console.ReadLine());
  42. List<int> numbers = new List<int>(n);
  43.  
  44. for (int i = 0; i < numbers.Count; i++)
  45. {
  46. numbers[i] = int.Parse(Console.ReadLine());
  47. }
  48.  
  49. int sum = 0;
  50.  
  51. List<int> sumOfElements = new List<int>();
  52.  
  53. for (int a = 0; a < numbers.Count; a++)
  54. {
  55. for (int b = a + 1; b < numbers.Count; b++)
  56. {
  57. for (int c = b + 1; c < numbers.Count; c++)
  58. {
  59. sum = numbers[a] + numbers[b] + numbers[c];
  60.  
  61. numbers.Remove(a);
  62. numbers.Remove(b);
  63. numbers.Remove(c);
  64.  
  65. if (sum == 0)
  66. {
  67. break;
  68. }
  69. else
  70. {
  71. sumOfElements.Add(sum);
  72. }
  73. }
  74. }
  75. }
  76.  
  77. for (int A = 0; A < sumOfElements.Count; A++)
  78. {
  79. for (int B = A + 1; B < sumOfElements.Count; B++)
  80. {
  81. for (int C = B + 1; C < sumOfElements.Count; C++)
  82. {
  83. if (sumOfElements[A] >= sumOfElements[B] && sumOfElements[A] >= sumOfElements[C])
  84. {
  85. Console.WriteLine(sumOfElements[A]);
  86. }
  87. if (sumOfElements[B] >= sumOfElements[A] && sumOfElements[B] >= sumOfElements[C])
  88. {
  89. Console.WriteLine(sumOfElements[B]);
  90. }
  91. if (sumOfElements[C] >= sumOfElements[B] && sumOfElements[C] >= sumOfElements[A])
  92. {
  93. Console.WriteLine(sumOfElements[C]);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement