Advertisement
Guest User

Untitled

a guest
Apr 21st, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace P09.KaminoFactory
  5. {
  6.     class KaminoFactory
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int lengthOfDNA = int.Parse(Console.ReadLine());
  11.             string command = Console.ReadLine();
  12.  
  13.             int counter = 0;
  14.             int maxCountOfOnes = 0;
  15.             int bestSequenceIndex = 0;
  16.             int bestSequenceSum = 0;
  17.             int[] bestDNA = new int[lengthOfDNA];
  18.  
  19.             while (command != "Clone them!")
  20.             {
  21.                 int countOfOnes = 0;
  22.                 int maxCountOfOnesOfcurrentSequenceOfDNA = 0;
  23.                 int bestIndexOfcurrentSequenceOfDNA = 0;
  24.                 int bestSumOfcurrentSequenceOfDNA = 0;
  25.  
  26.                 int[] currentSequenceOfDNA = new int[lengthOfDNA];
  27.  
  28.                 string digits = string.Empty;
  29.  
  30.                 for (int currentIndex = 0; currentIndex < command.Length; currentIndex++)
  31.                 {
  32.                     if (command[currentIndex] != '0' && command[currentIndex] != '1')
  33.                     {
  34.                         continue;
  35.                     }
  36.  
  37.                     digits += command[currentIndex];
  38.                 }
  39.  
  40.                 for (int currentIndex = 0; currentIndex < currentSequenceOfDNA.Length; currentIndex++)
  41.                 {
  42.                     currentSequenceOfDNA[currentIndex] = int.Parse(digits[currentIndex].ToString());
  43.                 }
  44.  
  45.                 for (int currentIndex = 0; currentIndex < lengthOfDNA; currentIndex++)
  46.                 {
  47.                     if (currentSequenceOfDNA[currentIndex] == 1)
  48.                     {
  49.                         countOfOnes++;
  50.  
  51.                         if (countOfOnes > maxCountOfOnesOfcurrentSequenceOfDNA)
  52.                         {
  53.                             maxCountOfOnesOfcurrentSequenceOfDNA = countOfOnes;
  54.  
  55.                             if (currentIndex >= bestIndexOfcurrentSequenceOfDNA)
  56.                             {
  57.                                 bestIndexOfcurrentSequenceOfDNA = currentIndex;
  58.                             }
  59.                         }
  60.  
  61.                         bestSumOfcurrentSequenceOfDNA++;
  62.                     }
  63.                     else
  64.                     {
  65.                         countOfOnes = 0;
  66.                     }
  67.                 }
  68.  
  69.                 if (maxCountOfOnesOfcurrentSequenceOfDNA > maxCountOfOnes)
  70.                 {
  71.                     maxCountOfOnes = maxCountOfOnesOfcurrentSequenceOfDNA;
  72.                     bestSequenceIndex = bestIndexOfcurrentSequenceOfDNA;
  73.                     bestSequenceSum = bestSumOfcurrentSequenceOfDNA;
  74.                     bestDNA = currentSequenceOfDNA;
  75.                 }
  76.                 else if (maxCountOfOnesOfcurrentSequenceOfDNA == maxCountOfOnes)
  77.                 {
  78.                     if (bestIndexOfcurrentSequenceOfDNA < bestSequenceIndex)
  79.                     {
  80.                         maxCountOfOnes = maxCountOfOnesOfcurrentSequenceOfDNA;
  81.                         bestSequenceIndex = bestIndexOfcurrentSequenceOfDNA;
  82.                         bestSequenceSum = bestSumOfcurrentSequenceOfDNA;
  83.                         bestDNA = currentSequenceOfDNA;
  84.  
  85.                         bestSequenceIndex = bestIndexOfcurrentSequenceOfDNA;
  86.                     }
  87.                     else if (bestIndexOfcurrentSequenceOfDNA == bestSequenceIndex)
  88.                     {
  89.                         if (bestSumOfcurrentSequenceOfDNA > bestSequenceSum)
  90.                         {
  91.                             maxCountOfOnes = maxCountOfOnesOfcurrentSequenceOfDNA;
  92.                             bestSequenceIndex = bestIndexOfcurrentSequenceOfDNA;
  93.                             bestSequenceSum = bestSumOfcurrentSequenceOfDNA;
  94.                             bestDNA = currentSequenceOfDNA;
  95.                         }
  96.                     }
  97.                 }
  98.                 counter++;
  99.  
  100.                 command = Console.ReadLine();
  101.             }
  102.  
  103.             Console.WriteLine($"Best DNA sample {bestSequenceIndex} with sum: {bestSequenceSum}.");
  104.             Console.Write(string.Join(' ', bestDNA));
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement