Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class PopulationCounterZad14 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayList<String> countries = new ArrayList<String>(); // every country
- ArrayList<String> citiesPlusPopulation = new ArrayList<String>(); // keeping the cities and the population together !
- 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!
- while(true){
- String input [] = scanner.nextLine().split("[|]+");
- if(input[0].equalsIgnoreCase("report")){
- break;
- }
- if(!countries.contains(input[1]) && Long.parseLong(input[2]) > 0){
- countries.add(input[1]);
- citiesPlusPopulation.add(input[0]+ "|" + input[2]);
- allPopulation.add(Long.parseLong(input[2]));
- }else if(Integer.parseInt(input[2]) > 0){
- for(int i = 0; i < countries.size(); i++){
- if(input[1].equals(countries.get(i))){
- // Adding the extra city + population in format city|population to "citiesPlusPopulation" at the a position according to city population size !
- String split[] = citiesPlusPopulation.get(i).split("[|]+");
- String temp = "";
- for(int j = 0; j < split.length; j+=2){
- 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
- temp = input[0] + "|" + input[2] + "|" + citiesPlusPopulation.get(i);
- citiesPlusPopulation.remove(i);
- citiesPlusPopulation.add(i, temp);
- break;
- }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
- temp = citiesPlusPopulation.get(i) + "|" + input[0] + "|" + input[2];
- citiesPlusPopulation.remove(i);
- citiesPlusPopulation.add(i, temp);
- break;
- }else if(Long.parseLong(input[2]) <= Long.parseLong(split[j + 1]) && Long.parseLong(input[2]) > Long.parseLong(split[j + 3])){
- for(int k = 0; k <= j + 1; k++){
- temp = temp + split[k] + "|";
- }
- temp = temp + input[0] + "|" + input[2];
- for(int k = j + 2; k < split.length; k ++){
- temp = temp + "|" + split[k];
- }
- citiesPlusPopulation.remove(i);
- citiesPlusPopulation.add(i, temp);
- break;
- }else if(j + 3 == split.length - 1 && Long.parseLong(input[2]) <= Long.parseLong(split[j+3])){
- temp = citiesPlusPopulation.get(i) + "|" + input[0] + "|" + input[2] ;
- citiesPlusPopulation.remove(i);
- citiesPlusPopulation.add(i, temp);
- break;
- }
- }
- //Adding total population sum to "allPopulation"
- long sum = allPopulation.get(i) + Long.parseLong(input[2]);
- allPopulation.remove(i);
- allPopulation.add(i, sum);
- }
- }
- }
- }
- // Sorting them by population descendingly
- for(int j = 0; j < allPopulation.size() - 1; j++){
- int detect = j; // detects the position of the biggest element so that we can swap them
- Long maxNumber = Long.MIN_VALUE;
- for(int k = j; k < allPopulation.size(); k++){
- if(maxNumber < allPopulation.get(k)){
- maxNumber = allPopulation.get(k);
- detect = k;
- }
- }
- // Arranging descendingly by "allPopulation"
- Long temp1 = allPopulation.get(j);
- Long temp2 = allPopulation.get(detect);
- allPopulation.remove(j);
- allPopulation.add(j, temp2);
- allPopulation.remove(detect);
- allPopulation.add(detect, temp1);
- // Swapping the "countries"
- String temp3 = countries.get(j);
- String temp4 = countries.get(detect);
- countries.remove(j);
- countries.add(j, temp4);
- countries.remove(detect);
- countries.add(detect, temp3);
- // Swapping the "citiesPlusPopulation"
- temp3 = citiesPlusPopulation.get(j);
- temp4 = citiesPlusPopulation.get(detect);
- citiesPlusPopulation.remove(j);
- citiesPlusPopulation.add(j, temp4);
- citiesPlusPopulation.remove(detect);
- citiesPlusPopulation.add(detect, temp3);
- }
- //Printing the answer
- for(int i = 0; i < countries.size(); i++){
- String split[] = citiesPlusPopulation.get(i).split("[|]+");
- if(split.length <= 2){
- System.out.printf("%s (total population: %d)%n=>%s: %s%n"
- ,countries.get(i), allPopulation.get(i),split[0],split[1]);
- }else{
- System.out.printf("%s (total population: %d)%n"
- ,countries.get(i), allPopulation.get(i));
- for(int j = 0; j < split.length; j+=2){
- System.out.printf("=>%s: %s%n", split[j],split[j + 1]);
- }
- }
- }
- // 100% on jundge the problem was that i was working with Integers instead of Long numbers!
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment