Advertisement
petar088

Kamino

Mar 22nd, 2019
360
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Fundamentals._10_Exercise_ARRAYS;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class _9_SecondTry {
  7. public static void main(String[] args) {
  8.  
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. int length = Integer.parseInt(sc.nextLine());
  12.  
  13. String input = sc.nextLine();
  14.  
  15. String [] bestDna = new String[length];
  16.  
  17. int numberDNAtest = 0;
  18. int counterSequnce = 0;
  19. int sum = 0;
  20. int i =0;
  21.  
  22. int maxSequence = 0;
  23. int firstPositionOfSequence = 0;
  24. int bestNumber = 0;
  25. int bestSum = 0;
  26.  
  27. while (!input.equals("Clone them!")) {
  28. sum = 0;
  29. String [] dna = input.split("!+");
  30. numberDNAtest++;
  31. counterSequnce=0;
  32. for ( i = 0; i < dna.length ; i++) {
  33. if (dna[i].equals("1")) {
  34. counterSequnce++;
  35. sum++;
  36. } else if (dna[i].equals("0")) {
  37. if (counterSequnce > maxSequence) {
  38. maxSequence = counterSequnce;
  39. firstPositionOfSequence = i+2-counterSequnce;
  40. bestSum=sum;
  41. counterSequnce = 0;
  42. bestNumber = numberDNAtest;
  43. bestDna = dna;
  44. } else if(counterSequnce == maxSequence && maxSequence!=0) {
  45. if (bestNumber != numberDNAtest) {
  46. if (firstPositionOfSequence == i+2-counterSequnce) {
  47. if (bestSum > sum) {
  48. continue;
  49. } else if (bestSum < sum) {
  50. bestDna = dna;
  51. bestNumber = numberDNAtest;
  52. }
  53. }
  54. }
  55. }else if (maxSequence==0 && counterSequnce==0) {
  56. maxSequence = counterSequnce;
  57. firstPositionOfSequence = i+2-maxSequence ;
  58. bestNumber = numberDNAtest;
  59. bestDna = dna;
  60. bestSum=sum;
  61.  
  62.  
  63. }
  64. counterSequnce=0;
  65. }
  66. if (counterSequnce > maxSequence) {
  67. maxSequence = counterSequnce;
  68. firstPositionOfSequence = i+2-maxSequence ;
  69. bestNumber = numberDNAtest;
  70. bestDna = dna;
  71. bestSum=sum;
  72. } else if(counterSequnce == maxSequence) {
  73. if (bestNumber != numberDNAtest) {
  74. if (firstPositionOfSequence == i+2-counterSequnce) {
  75. if (bestSum > sum) {
  76. continue;
  77. } else if (bestSum < sum) {
  78. bestDna = dna;
  79. bestNumber = numberDNAtest;
  80. bestSum=sum;
  81. firstPositionOfSequence= i+2-counterSequnce;
  82. maxSequence = counterSequnce;
  83. }
  84. } else if(firstPositionOfSequence>i+2-counterSequnce){
  85. bestDna = dna;
  86. bestNumber = numberDNAtest;
  87. bestSum=sum;
  88. firstPositionOfSequence= i+2-counterSequnce;
  89. maxSequence = counterSequnce;
  90. }
  91. }
  92. }
  93. }
  94. if (sum>bestSum){
  95. bestSum=sum;
  96. }
  97.  
  98. input = sc.nextLine();
  99. }
  100.  
  101.  
  102. System.out.println("Best DNA sample " + bestNumber +" with sum: "+bestSum+".");
  103. for (int j = 0; j < bestDna.length; j++) {
  104. System.out.print(bestDna[j] + " ");
  105. }
  106. }
  107. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement