Advertisement
aspire12

9.KaminoFactory

Mar 24th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _9.KaminoFactory
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int dnaLength = int.Parse(Console.ReadLine());
  10.  
  11.             string input = Console.ReadLine();
  12.             int[] bestDnaArray = new int[dnaLength];
  13.             int leftmostIndex = 0;
  14.             int greaterSum = 0;
  15.             int greaterSequence = 0;
  16.             int inputCounter = 0;
  17.             int bestCounter = 0;
  18.             while (true)
  19.             {
  20.                
  21.                 if(input == "Clone them!")
  22.                 {
  23.                     break;
  24.                 }
  25.                 inputCounter++;
  26.                 int[] dnaArray = new int[dnaLength];
  27.                 int counter = 0;
  28.                
  29.                 for(int i = 0; i < input.Length; i++)
  30.                 {
  31.                     char symbol = input[i];
  32.                     if(symbol != '!' || symbol != ' ')
  33.                     {
  34.                         if(symbol == '0' || symbol == '1')
  35.                         {
  36.                             dnaArray[counter] = int.Parse(symbol.ToString());
  37.                             counter++;
  38.                         }
  39.                     }
  40.                 }
  41.                
  42.                
  43.                 int bestSum = 0;
  44.                 int sequence = 0;
  45.                 int bestIndex = 0;
  46.  
  47.                 for (int i = 0; i < dnaArray.Length; i++)
  48.                 {
  49.                     int index = 0;
  50.                     if (dnaArray[i] == 1)
  51.                     {
  52.                         int sum = 0;
  53.                         index = i;
  54.                         for (int j = i; j < dnaArray.Length; j++)
  55.                         {
  56.                             if (dnaArray[j] == 0)
  57.                             {
  58.                                 i = j;
  59.                                 break;
  60.                             }
  61.                             sum++;
  62.                             i = j;
  63.                         }
  64.                         if(sum > bestSum)
  65.                         {
  66.                             bestSum = sum;
  67.                             bestIndex = index;
  68.                             sequence = bestSum;
  69.                         }
  70.                     }
  71.                 }
  72.                 int arraySum = 0;
  73.                 for (int i = 0; i < dnaArray.Length; i++)
  74.                 {
  75.                     if (dnaArray[i] == 1)
  76.                     {
  77.                         arraySum++;
  78.                     }
  79.                 }
  80.                 if (sequence > greaterSequence)
  81.                 {
  82.                     bestDnaArray = dnaArray;
  83.                     leftmostIndex = bestIndex;
  84.                     bestCounter = inputCounter;
  85.                     greaterSum = arraySum;
  86.                     greaterSequence = sequence;
  87.                 }
  88.                 else if(sequence == greaterSequence)
  89.                 {
  90.                     if(bestIndex < leftmostIndex)
  91.                     {
  92.                         bestDnaArray = dnaArray;
  93.                         leftmostIndex = bestIndex;
  94.                         greaterSum = arraySum;
  95.                         bestCounter = inputCounter;
  96.                        
  97.                     }
  98.                     else if(bestIndex == leftmostIndex)
  99.                     {
  100.                        
  101.                         if(greaterSum < arraySum)
  102.                         {
  103.                             bestDnaArray = dnaArray;
  104.                             leftmostIndex = bestIndex;
  105.                             greaterSum = arraySum;
  106.                             bestCounter = inputCounter;
  107.                         }
  108.                     }
  109.                 }
  110.  
  111.                 input = Console.ReadLine();
  112.             }
  113.  
  114.             if (bestCounter == 0)
  115.             {
  116.                 bestCounter = 1;
  117.             }
  118.  
  119.             Console.WriteLine($"Best DNA sample {bestCounter} with sum: {greaterSum}.");
  120.             Console.WriteLine($"{string.Join(" ", bestDnaArray)}");
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement