Advertisement
veronikaaa86

09. Kamino Factory

Oct 15th, 2021
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class P09KaminoFactory {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int length = Integer.parseInt(scanner.nextLine());
  9.  
  10. int[] bestDNA = new int[length];
  11. String input = scanner.nextLine();
  12. int sample = 1;
  13.  
  14. int bestSample = 0;
  15. int maxSum = 0;
  16. int bestIndex = 0;
  17. int maxSequenceLength = 0;
  18. while (!input.equals("Clone them!")) {
  19. int[] dna = Arrays
  20. .stream(input.split("!+"))
  21. .mapToInt(Integer::parseInt)
  22. .toArray();
  23.  
  24. int currentSum = 0;
  25. int sequenceLength = 1;
  26. int currentIndex = 0;
  27. for (int i = 0; i < dna.length; i++) {
  28. currentSum += dna[i];
  29.  
  30. if (i != dna.length - 1) {
  31. if (dna[i] == 1 && dna[i + 1] == 1) {
  32. sequenceLength++;
  33. currentIndex = i + 1;
  34. }
  35. }
  36. }
  37.  
  38. if (sequenceLength > maxSequenceLength ||
  39. (sequenceLength == maxSequenceLength && currentIndex < bestIndex) ||
  40. (sequenceLength == maxSequenceLength && currentIndex == bestIndex && currentSum > maxSum)) {
  41. maxSequenceLength = sequenceLength;
  42. bestIndex = currentIndex;
  43. maxSum = currentSum;
  44. bestSample = sample;
  45.  
  46. for (int i = 0; i < length; i++) {
  47. bestDNA[i] = dna[i];
  48. }
  49. }
  50.  
  51. input = scanner.nextLine();
  52. sample++;
  53. }
  54.  
  55. System.out.printf("Best DNA sample %d with sum: %d.%n", bestSample, maxSum);
  56. for (int i : bestDNA) {
  57. System.out.print(i + " ");
  58. }
  59. }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement