Advertisement
petar088

Kamino Factory 100/100

Mar 23rd, 2019
1,514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package Fundamentals._10_Exercise_ARRAYS;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class _9_Try3 {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8.  
  9. int sizeField = Integer.parseInt(sc.nextLine());
  10.  
  11. String input = sc.nextLine();
  12. String forAllZeros = input;
  13.  
  14.  
  15.  
  16. int numberDNAtest = 0;
  17. int localNumber = 0;
  18. int bestSum = 0;
  19. int bestLength = 0;
  20. int bestNumber = 0;
  21. int bestIndex = 0;
  22. String bestSequence = "";
  23. String bestInput = "";
  24. boolean zeros = false;
  25.  
  26.  
  27. String localnput = "";
  28. while (!input.equals("Clone them!")) {
  29.  
  30. String noChars = input.replaceAll("!+", "");
  31. String[] noZeros = noChars.split("0");
  32. numberDNAtest++;
  33. int localLength = 0;
  34. String localSequence = "";
  35. int sum = 0;
  36.  
  37.  
  38. for (int j = 0; j < noZeros.length; j++) {
  39. if (noZeros[j].length() > localLength) {
  40. localLength = noZeros[j].length();
  41. localNumber = numberDNAtest;
  42. localSequence = noZeros[j];
  43. localnput = noChars;
  44.  
  45. }
  46. sum += noZeros[j].length();
  47. if (sum > 0) {
  48. zeros = true;
  49. }
  50. }
  51. int localIndex = noChars.indexOf(localSequence);
  52.  
  53. if (localLength > bestLength) {
  54. bestLength = localLength;
  55. bestNumber = numberDNAtest;
  56. bestInput = localnput;
  57. bestSum = sum;
  58. bestIndex = localIndex;
  59.  
  60.  
  61. } else if (localLength == bestLength) {
  62. if (localIndex < bestIndex) {
  63. bestLength = localLength;
  64. bestNumber = numberDNAtest;
  65. bestInput = localnput;
  66. bestSum = sum;
  67. bestIndex = localIndex;
  68. } else if (localIndex == bestIndex) {
  69. if (bestSum < sum) {
  70. bestLength = localLength;
  71. bestNumber = numberDNAtest;
  72. bestInput = localnput;
  73. bestSum = sum;
  74. bestIndex = localIndex;
  75. }
  76. } else {
  77. continue;
  78. }
  79. }
  80.  
  81. localLength = 0;
  82. localNumber = 0;
  83. localSequence = "";
  84. localnput = "";
  85. sum = 0;
  86.  
  87.  
  88. input = sc.nextLine();
  89.  
  90.  
  91. }
  92. if (zeros == false) {
  93. String output1 = forAllZeros.replaceAll("!+",""); //Проверка когато входа е 0!0!0!0!0
  94. String [] bestAllZero = output1.split("");
  95.  
  96. System.out.println("Best DNA sample 1" + " with sum: " + 0 + ".");
  97. for (int i = 0; i < bestAllZero.length; i++) {
  98. System.out.print(bestAllZero[i] + " ");
  99. }
  100.  
  101. } else {
  102. String[] output = bestInput.split("");
  103.  
  104. System.out.println("Best DNA sample " + bestNumber + " with sum: " + bestSum + ".");
  105. for (int i = 0; i < output.length; i++) {
  106. System.out.print(output[i] + " ");
  107. }
  108. }
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement