Advertisement
TonyTroev

KaminoFactory

Mar 6th, 2018
3,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02_SecondExercise
  6. {
  7.     class KaminoFactory
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = string.Empty;
  12.             int length = int.Parse(Console.ReadLine());
  13.             int[] lss = new int[length];
  14.             int lssLength = int.MinValue, lssIndex = int.MinValue, lssSum = int.MinValue, lssStart = -1;
  15.             int index = 1;
  16.  
  17.             while ((input = Console.ReadLine()) != "Clone them!")
  18.             {
  19.                 int[] data = input
  20.                     .Split(new char[] { '!' }, StringSplitOptions.RemoveEmptyEntries)
  21.                     .Select(int.Parse)
  22.                     .ToArray();
  23.  
  24.                 int currentLength = int.MinValue, currentIndex = int.MinValue, currentSubLength = 0, currentSubIndex = 0;
  25.                 bool isOne = false;
  26.  
  27.                 for (int i = 0; i < length; i++)
  28.                 {
  29.                     if (data[i] == 1 && isOne)
  30.                     {
  31.                         currentSubLength++;
  32.                     }
  33.                     else if (data[i] == 1)
  34.                     {
  35.                         isOne = true;
  36.                         currentSubIndex = i;
  37.                         currentSubLength = 1;
  38.                     }
  39.                     else if (data[i] == 0 && isOne)
  40.                     {
  41.                         if (currentSubLength > currentLength)
  42.                         {
  43.                             currentLength = currentSubLength;
  44.                             currentIndex = currentSubIndex;
  45.                         }
  46.                         isOne = false;
  47.                         currentSubLength = 0;
  48.                         currentSubIndex = 0;
  49.                     }
  50.                 }
  51.  
  52.                 if (isOne)
  53.                 {
  54.                     if (currentSubLength > currentLength)
  55.                     {
  56.                         currentLength = currentSubLength;
  57.                         currentIndex = currentSubIndex;
  58.                     }
  59.                 }
  60.  
  61.                 if (currentLength > lssLength)
  62.                 {
  63.                     lssLength = currentLength;
  64.                     lssIndex = currentIndex;
  65.                     lssSum = data.Sum();
  66.                     lss = data;
  67.                     lssStart = index;
  68.                 }
  69.                 else if (currentLength == lssLength)
  70.                 {
  71.                     if (currentIndex < lssIndex)
  72.                     {
  73.                         lssLength = currentLength;
  74.                         lssIndex = currentIndex;
  75.                         lssSum = data.Sum();
  76.                         lss = data;
  77.                         lssStart = index;
  78.                     }
  79.                     else if (currentIndex == lssIndex)
  80.                     {
  81.                         if (data.Sum() > lssSum)
  82.                         {
  83.                             lssLength = currentLength;
  84.                             lssIndex = currentIndex;
  85.                             lssSum = data.Sum();
  86.                             lss = data;
  87.                             lssStart = index;
  88.                         }
  89.                     }
  90.                 }
  91.                 index++;
  92.             }
  93.  
  94.             Console.WriteLine($"Best DNA sample {lssStart} with sum: {lssSum}.");
  95.             Console.WriteLine(string.Join(" ", lss));
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement