vdjalov

Untitled

Jul 20th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class PopulationCounterZad14 {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. ArrayList<String> countries = new ArrayList<String>(); // every country
  11. ArrayList<String> citiesPlusPopulation = new ArrayList<String>(); // keeping the cities and the population together !
  12. ArrayList<Long> allPopulation = new ArrayList<Long>(); // summing all countries population separately according to the indexes so that i can sort the three(3) ArrayLists at the end!
  13.  
  14. while(true){
  15. String input [] = scanner.nextLine().split("[|]+");
  16. if(input[0].equalsIgnoreCase("report")){
  17. break;
  18. }
  19.  
  20. if(!countries.contains(input[1]) && Long.parseLong(input[2]) > 0){
  21. countries.add(input[1]);
  22. citiesPlusPopulation.add(input[0]+ "|" + input[2]);
  23. allPopulation.add(Long.parseLong(input[2]));
  24. }else if(Integer.parseInt(input[2]) > 0){
  25.  
  26. for(int i = 0; i < countries.size(); i++){
  27. if(input[1].equals(countries.get(i))){
  28. // Adding the extra city + population in format city|population to "citiesPlusPopulation" at the a position according to city population size !
  29. String split[] = citiesPlusPopulation.get(i).split("[|]+");
  30.  
  31. String temp = "";
  32. for(int j = 0; j < split.length; j+=2){
  33. if(Long.parseLong(input[2]) > Long.parseLong(split[j + 1]) && j == 0){ // case one input[2] > split[j+1] and there is only one city|population
  34. temp = input[0] + "|" + input[2] + "|" + citiesPlusPopulation.get(i);
  35. citiesPlusPopulation.remove(i);
  36. citiesPlusPopulation.add(i, temp);
  37. break;
  38. }else if(Long.parseLong(input[2]) <= Long.parseLong(split[j + 1]) && j == 0 && split.length == 2){ // case two input[2] <= split[j+1] and there is only one city|population
  39. temp = citiesPlusPopulation.get(i) + "|" + input[0] + "|" + input[2];
  40. citiesPlusPopulation.remove(i);
  41. citiesPlusPopulation.add(i, temp);
  42. break;
  43. }else if(Long.parseLong(input[2]) <= Long.parseLong(split[j + 1]) && Long.parseLong(input[2]) > Long.parseLong(split[j + 3])){
  44. for(int k = 0; k <= j + 1; k++){
  45. temp = temp + split[k] + "|";
  46. }
  47. temp = temp + input[0] + "|" + input[2];
  48. for(int k = j + 2; k < split.length; k ++){
  49. temp = temp + "|" + split[k];
  50. }
  51.  
  52. citiesPlusPopulation.remove(i);
  53. citiesPlusPopulation.add(i, temp);
  54. break;
  55. }else if(j + 3 == split.length - 1 && Long.parseLong(input[2]) <= Long.parseLong(split[j+3])){
  56. temp = citiesPlusPopulation.get(i) + "|" + input[0] + "|" + input[2] ;
  57. citiesPlusPopulation.remove(i);
  58. citiesPlusPopulation.add(i, temp);
  59. break;
  60. }
  61. }
  62.  
  63. //Adding total population sum to "allPopulation"
  64. long sum = allPopulation.get(i) + Long.parseLong(input[2]);
  65. allPopulation.remove(i);
  66. allPopulation.add(i, sum);
  67.  
  68. }
  69. }
  70. }
  71. }
  72.  
  73.  
  74. // Sorting them by population descendingly
  75.  
  76. for(int j = 0; j < allPopulation.size() - 1; j++){
  77. int detect = j; // detects the position of the biggest element so that we can swap them
  78. Long maxNumber = Long.MIN_VALUE;
  79. for(int k = j; k < allPopulation.size(); k++){
  80. if(maxNumber < allPopulation.get(k)){
  81. maxNumber = allPopulation.get(k);
  82. detect = k;
  83. }
  84. }
  85. // Arranging descendingly by "allPopulation"
  86. Long temp1 = allPopulation.get(j);
  87. Long temp2 = allPopulation.get(detect);
  88.  
  89. allPopulation.remove(j);
  90. allPopulation.add(j, temp2);
  91. allPopulation.remove(detect);
  92. allPopulation.add(detect, temp1);
  93.  
  94. // Swapping the "countries"
  95. String temp3 = countries.get(j);
  96. String temp4 = countries.get(detect);
  97.  
  98. countries.remove(j);
  99. countries.add(j, temp4);
  100. countries.remove(detect);
  101. countries.add(detect, temp3);
  102.  
  103. // Swapping the "citiesPlusPopulation"
  104. temp3 = citiesPlusPopulation.get(j);
  105. temp4 = citiesPlusPopulation.get(detect);
  106.  
  107. citiesPlusPopulation.remove(j);
  108. citiesPlusPopulation.add(j, temp4);
  109. citiesPlusPopulation.remove(detect);
  110. citiesPlusPopulation.add(detect, temp3);
  111.  
  112. }
  113. //Printing the answer
  114. for(int i = 0; i < countries.size(); i++){
  115. String split[] = citiesPlusPopulation.get(i).split("[|]+");
  116. if(split.length <= 2){
  117. System.out.printf("%s (total population: %d)%n=>%s: %s%n"
  118. ,countries.get(i), allPopulation.get(i),split[0],split[1]);
  119. }else{
  120. System.out.printf("%s (total population: %d)%n"
  121. ,countries.get(i), allPopulation.get(i));
  122.  
  123. for(int j = 0; j < split.length; j+=2){
  124. System.out.printf("=>%s: %s%n", split[j],split[j + 1]);
  125. }
  126. }
  127. }
  128.  
  129. // 100% on jundge the problem was that i was working with Integers instead of Long numbers!
  130.  
  131. scanner.close();
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment