Advertisement
Guest User

Untitled

a guest
Apr 27th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 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 maxCountOfOnes = 0;
  14.             int[] bestDNA = new int[lengthOfDNA];
  15.             int bestSequenceIndex = 0;
  16.             int bestSequenceSum = 0;
  17.             int currentDNAs = 0;
  18.             int bestDNARow = 0;
  19.  
  20.             while (command != "Clone them!")
  21.             {
  22.                 int[] currentArr = command
  23.                     .Split('!', StringSplitOptions.RemoveEmptyEntries)
  24.                     .Select(int.Parse)
  25.                     .ToArray();
  26.  
  27.                 int countOfOnes = 0;
  28.                 int currentMaxCountOfOnes = 0;
  29.                 int index = 0;
  30.                 int currentBestIndex = -1;
  31.  
  32.                 for (int currentIndex = 0; currentIndex < currentArr.Length; currentIndex++)
  33.                 {
  34.                     if (currentArr[currentIndex] == 1)
  35.                     {
  36.                         countOfOnes++;
  37.  
  38.                         if (countOfOnes == 1)
  39.                         {
  40.                             index = currentIndex;
  41.                         }
  42.                     }
  43.                     else
  44.                     {
  45.                         if (countOfOnes > currentMaxCountOfOnes)
  46.                         {
  47.                             currentMaxCountOfOnes = countOfOnes;
  48.                             currentBestIndex = index;
  49.                         }
  50.  
  51.                         countOfOnes = 0;
  52.                     }
  53.                 }
  54.  
  55.                 currentDNAs++;
  56.  
  57.                 if (currentDNAs == 1 || currentMaxCountOfOnes > maxCountOfOnes || currentBestIndex < bestSequenceIndex || currentArr.Sum() > bestSequenceSum)
  58.                 {
  59.                     maxCountOfOnes = currentMaxCountOfOnes;
  60.                     bestSequenceIndex = currentBestIndex;
  61.                     bestSequenceSum = currentArr.Sum();
  62.                     bestDNARow = currentDNAs;
  63.                     bestDNA = currentArr;
  64.                 }
  65.  
  66.                 command = Console.ReadLine();
  67.             }
  68.  
  69.             Console.WriteLine($"Best DNA sample {bestDNARow} with sum: {bestSequenceSum}.");
  70.             Console.WriteLine(string.Join(' ', bestDNA));
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement