Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2019
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. namespace P09_KaminoFactory
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class P09_KaminoFactory
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int sequenceLength = int.Parse(Console.ReadLine());
  11.             string input = Console.ReadLine();
  12.  
  13.             int[] DNA = new int[sequenceLength];
  14.             int DNASum = 0;
  15.             int DNACount = -1;
  16.             int DNAStartIndex = -1;
  17.             int DNASample = 0;
  18.  
  19.             int sample = 0;
  20.             while (input != "Clone them!")
  21.             {
  22.                 //----------------------------- CURRENT DNA INFO -----------------------------
  23.                 sample++;
  24.                 int[] currDNA = input.Split("!", StringSplitOptions.RemoveEmptyEntries)
  25.                                     .Select(int.Parse)
  26.                                     .ToArray();
  27.                 int currCount = 0;
  28.                 int currStartIndex = 0;
  29.                 int currEndIndex = 0;
  30.                 int currDNASum = 0;
  31.                 bool isCurrDNABetter = false;
  32.  
  33.                 int count = 0;
  34.                 for (int i = 0; i < currDNA.Length; i++)
  35.                 {
  36.                     if (currDNA[i] != 1)
  37.                     {
  38.                         count = 0;
  39.                         continue;
  40.                     }
  41.  
  42.                     count++;
  43.                     if (count > currCount)
  44.                     {
  45.                         currCount = count;
  46.                         currEndIndex = i;
  47.                     }
  48.                 }
  49.  
  50.                 currStartIndex = currEndIndex - currCount + 1;
  51.                 currDNASum = currDNA.Sum();
  52.  
  53.                 //-------------------- CHECK CURRENT DNA WITH BEST DNA -----------------------
  54.                 if (currCount > DNACount)
  55.                 {
  56.                     isCurrDNABetter = true;
  57.                 }
  58.                 else if (currCount == DNACount)
  59.                 {
  60.                     if (currStartIndex < DNAStartIndex)
  61.                     {
  62.                         isCurrDNABetter = true;
  63.                     }
  64.                     else if (currStartIndex == DNAStartIndex)
  65.                     {
  66.                         if (currDNASum > DNASum)
  67.                         {
  68.                             isCurrDNABetter = true;
  69.                         }
  70.                     }
  71.                 }
  72.  
  73.                 if (isCurrDNABetter)
  74.                 {
  75.                     DNA = currDNA;
  76.                     DNACount = currCount;
  77.                     DNAStartIndex = currStartIndex;
  78.                     DNASum = currDNASum;
  79.                     DNASample = sample;
  80.                 }
  81.  
  82.                 //----------------------------------------------------------------------------
  83.                 input = Console.ReadLine();
  84.             }
  85.  
  86.             Console.WriteLine($"Best DNA sample {DNASample} with sum: {DNASum}.");
  87.             Console.WriteLine(String.Join(" ", DNA));
  88.         }    
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement