MilenaPetkanova

MaxSequenceOfEqualElements

Oct 24th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. class MaxSequenceOfEqualElements
  6. {
  7. static void Main(string[] args)
  8. {
  9. string inputTest1 = File.ReadAllText("inputTest1.txt");
  10. string inputTest2 = File.ReadAllText("inputTest2.txt");
  11. string inputTest3 = File.ReadAllText("inputTest3.txt");
  12.  
  13. string[] allinputTests = { inputTest1, inputTest2, inputTest3 };
  14.  
  15. for (int test = 0; test < allinputTests.Length; test++)
  16. {
  17. int[] nums = allinputTests[test]
  18. .Split(' ').Select(int.Parse).ToArray();
  19.  
  20. int maxSequence = 1;
  21. int currSequence = 1;
  22. int bestStartPos = 0;
  23.  
  24. for (int i = 0; i < nums.Length - 1; i++)
  25. {
  26. int currPos = i;
  27. while (i < nums.Length - 1
  28. && nums[i] == nums[i + 1])
  29. {
  30. currSequence++;
  31. if (currSequence > maxSequence)
  32. {
  33. maxSequence = currSequence;
  34. bestStartPos = currPos;
  35. }
  36. i++;
  37. }
  38. currSequence = 1;
  39. }
  40.  
  41. string output = null;
  42. for (int i = bestStartPos; i < bestStartPos + maxSequence; i++)
  43. {
  44. File.AppendAllText($"outputTest{test + 1}.txt",
  45. nums[i].ToString() + " ");
  46.  
  47. Console.Write("{0} ", nums[i]);
  48. }
  49. Console.WriteLine();
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment