Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class KaminoFactory {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int length = Integer.parseInt(scanner.nextLine());
- String input = scanner.nextLine();
- int bestTotalSum = 0;
- int bestIndex = Integer.MAX_VALUE;
- int bestAmountUnits = 0;
- int arrNumber = 0;
- int bestArrNumber = 0;
- int currentNumber = 0;
- int[] bestNumbersDNA = new int[length];
- while (!input.equals("Clone them!")) {
- String[] StringDNA = input.split("!");
- int[] numbersDNA = new int[length];
- for (int i = 0; i < StringDNA.length; i++) {
- numbersDNA[i] = Integer.parseInt(StringDNA[i]);
- }
- arrNumber++;
- int totalSum = 0;
- int unitsAmount = 0;
- int currentUnitsAmount = 0;
- int currentIndex = Integer.MAX_VALUE;
- int currentArrIndex = 0;
- for (int i = 0; i < numbersDNA.length; i++) {
- currentNumber = numbersDNA[i];
- if (currentNumber == 1) {
- currentUnitsAmount += currentNumber;
- if (currentUnitsAmount > unitsAmount) {
- unitsAmount = currentUnitsAmount;
- }
- if (i < numbersDNA.length - 1) {
- if (numbersDNA[i + 1] == 1) {
- currentArrIndex = i;
- if (currentArrIndex < currentIndex) {
- currentIndex = i;
- }
- }
- }
- } else {
- currentUnitsAmount = 0;
- }
- totalSum += numbersDNA[i];
- }
- if ((unitsAmount > bestAmountUnits) || (unitsAmount == 0) || (unitsAmount == bestAmountUnits && currentIndex < bestIndex) || (unitsAmount == bestAmountUnits && currentIndex == bestIndex && totalSum > bestTotalSum)) {
- for (int i = 0; i < bestNumbersDNA.length; i++) {
- bestNumbersDNA[i] = Integer.parseInt(StringDNA[i]);
- }
- bestTotalSum = totalSum;
- bestArrNumber = arrNumber;
- bestAmountUnits = unitsAmount;
- bestIndex = currentIndex;
- }
- input = scanner.nextLine();
- }
- System.out.printf("Best DNA sample %d with sum: %d.%n", bestArrNumber, bestTotalSum);
- for (int i = 0; i < bestNumbersDNA.length; i++) {
- System.out.print(bestNumbersDNA[i] + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement