Advertisement
Guest User

KaminoFactory

a guest
Feb 3rd, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class KaminoFactory {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int length = Integer.parseInt(scanner.nextLine());
  9.         String input = scanner.nextLine();
  10.  
  11.         int bestTotalSum = 0;
  12.         int bestIndex = Integer.MAX_VALUE;
  13.         int bestAmountUnits = 0;
  14.         int arrNumber = 0;
  15.         int bestArrNumber = 0;
  16.         int currentNumber = 0;
  17.  
  18.         int[] bestNumbersDNA = new int[length];
  19.  
  20.         while (!input.equals("Clone them!")) {
  21.  
  22.             String[] StringDNA = input.split("!");
  23.             int[] numbersDNA = new int[length];
  24.  
  25.             for (int i = 0; i < StringDNA.length; i++) {
  26.                 numbersDNA[i] = Integer.parseInt(StringDNA[i]);
  27.             }
  28.  
  29.             arrNumber++;
  30.             int totalSum = 0;
  31.             int unitsAmount = 0;
  32.             int currentUnitsAmount = 0;
  33.             int currentIndex = Integer.MAX_VALUE;
  34.             int currentArrIndex = 0;
  35.  
  36.             for (int i = 0; i < numbersDNA.length; i++) {
  37.                 currentNumber = numbersDNA[i];
  38.  
  39.                 if (currentNumber == 1) {
  40.                     currentUnitsAmount += currentNumber;
  41.                     if (currentUnitsAmount > unitsAmount) {
  42.                         unitsAmount = currentUnitsAmount;
  43.                     }
  44.                     if (i < numbersDNA.length - 1) {
  45.                         if (numbersDNA[i + 1] == 1) {
  46.                             currentArrIndex = i;
  47.                             if (currentArrIndex < currentIndex) {
  48.                                 currentIndex = i;
  49.                             }
  50.                         }
  51.                     }
  52.                 } else {
  53.                     currentUnitsAmount = 0;
  54.                 }
  55.                 totalSum += numbersDNA[i];
  56.             }
  57.  
  58.             if ((unitsAmount > bestAmountUnits) || (unitsAmount == 0) || (unitsAmount == bestAmountUnits && currentIndex < bestIndex) || (unitsAmount == bestAmountUnits && currentIndex == bestIndex && totalSum > bestTotalSum)) {
  59.                 for (int i = 0; i < bestNumbersDNA.length; i++) {
  60.                     bestNumbersDNA[i] = Integer.parseInt(StringDNA[i]);
  61.                 }
  62.                 bestTotalSum = totalSum;
  63.                 bestArrNumber = arrNumber;
  64.                 bestAmountUnits = unitsAmount;
  65.                 bestIndex = currentIndex;
  66.             }
  67.  
  68.             input = scanner.nextLine();
  69.         }
  70.  
  71.         System.out.printf("Best DNA sample %d with sum: %d.%n", bestArrNumber, bestTotalSum);
  72.  
  73.         for (int i = 0; i < bestNumbersDNA.length; i++) {
  74.             System.out.print(bestNumbersDNA[i] + " ");
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement