Advertisement
Guest User

Untitled

a guest
Feb 8th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _09.KaminoFactory
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int sequencesLength = int.Parse(Console.ReadLine());
  11. string command = Console.ReadLine();
  12.  
  13. int[] bestDNA = new int[sequencesLength];
  14.  
  15. int bestDNASum = 0;
  16. int DNACount = -1;
  17. int smallerStartIndex = -1;
  18. int DNASample = 0;
  19. int sample = 0;
  20.  
  21.  
  22. while (command != "Clone them!")
  23. {
  24. sample++;
  25. int[] currentDNA = command.Split("!", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  26.  
  27. bool betterDNA = false;
  28. int bestCount = 0;
  29. int currentCount = 0;
  30. int currentEndIndex = 0;
  31.  
  32. for (int i = 0; i < currentDNA.Length; i++)
  33. {
  34. if (currentDNA[i] == 0)
  35. {
  36. currentCount = 0;
  37. continue;
  38. }
  39. currentCount++;
  40.  
  41. if (bestCount < currentCount)
  42. {
  43. bestCount = currentCount;
  44. currentEndIndex = i;
  45. }
  46. }
  47. int currentSum = currentDNA.Sum();
  48. int currentStartIndex = currentEndIndex - bestCount + 1;
  49.  
  50. if (bestCount > DNACount)
  51. {
  52. betterDNA = true;
  53. }
  54. else if (bestCount == DNACount)
  55. {
  56. if (currentStartIndex < smallerStartIndex)
  57. {
  58. betterDNA = true;
  59. }
  60. else if (currentStartIndex == currentEndIndex)
  61. {
  62. if (currentSum > bestDNASum)
  63. {
  64. betterDNA = true;
  65. }
  66. }
  67. }
  68.  
  69. if (betterDNA == true)
  70. {
  71. bestDNA = currentDNA;
  72. DNACount = bestCount;
  73. smallerStartIndex = currentStartIndex;
  74. bestDNASum = currentSum;
  75. DNASample = sample;
  76. }
  77.  
  78. command = Console.ReadLine();
  79. }
  80. Console.WriteLine($"Best DNA sample {DNASample} with sum: {bestDNASum}.");
  81. Console.WriteLine(string.Join(" ", bestDNA));
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement