Advertisement
bullit3189

Kamino Factory - Arrays

Jan 23rd, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Program
  5. {
  6. public static void Main()
  7. {
  8. int DNALength = int.Parse(Console.ReadLine());
  9.  
  10. int sum=0;
  11. int bestSum=0;
  12.  
  13. int length =0;
  14. int bestLength =0;
  15.  
  16. int startIndex =-1;
  17. int bestIndex = -1;
  18.  
  19. int line =0;
  20. int bestLine=0;
  21.  
  22. int[] bestDNA = new int [DNALength];
  23.  
  24. while (true)
  25. {
  26. string command = Console.ReadLine();
  27.  
  28. if (command == "Clone them!")
  29. {
  30. break;
  31. }
  32.  
  33. int [] currDNA = command.Split(new [] {'!'},StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  34.  
  35. startIndex =-1;
  36. length =0;
  37. sum=0;
  38. line++;
  39.  
  40. for (int i=0; i<currDNA.Length; i++)
  41. {
  42. if (currDNA[i]==1)
  43. {
  44. sum++;
  45. }
  46. }
  47.  
  48. for (int i=0; i<currDNA.Length; i++)
  49. {
  50. if (currDNA[i]==1)
  51. {
  52. length++;
  53.  
  54. if (length==1)
  55. {
  56. startIndex=i;
  57. }
  58. }
  59. else if (currDNA[i]==0)
  60. {
  61. length=0;
  62. }
  63. if (length>bestLength)
  64. {
  65. bestLength = length;
  66. bestSum = sum;
  67. bestLine=line;
  68. bestDNA=currDNA;
  69. bestIndex=startIndex;
  70. }
  71. if (length==bestLength)
  72. {
  73. if (startIndex<bestIndex)
  74. {
  75. bestLength = length;
  76. bestSum = sum;
  77. bestLine=line;
  78. bestDNA=currDNA;
  79. bestIndex=startIndex;
  80. }
  81. else if (startIndex==bestIndex)
  82. {
  83. if (sum>bestSum)
  84. {
  85. bestLength = length;
  86. bestSum = sum;
  87. bestLine=line;
  88. bestDNA=currDNA;
  89.  
  90. }
  91. }
  92.  
  93. }
  94.  
  95. }
  96. }
  97. if (bestLength==0)
  98. {
  99. bestLine=1;
  100. Console.WriteLine("Best DNA sample {0} with sum: {1}.",bestLine,bestSum);
  101. Console.WriteLine(string.Join(" ",bestDNA));
  102. return;
  103. }
  104. Console.WriteLine("Best DNA sample {0} with sum: {1}.",bestLine,bestSum);
  105. Console.WriteLine(string.Join(" ",bestDNA));
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement