Advertisement
roronoa

strings comparator, bibliothèque

Apr 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. class Solution
  5. {
  6.  
  7.     public static void main(String args[])
  8.     {
  9.         Scanner in = new Scanner(System.in);
  10.         int N = in.nextInt();
  11.         if (in.hasNextLine())
  12.         {
  13.             in.nextLine();
  14.         }
  15.         int sum = 0;
  16.         String books[] = new String[N];
  17.         int time[] = new int[N];
  18.         for (int i = 0; i < N; i++)
  19.         {
  20.             books[i] = in.nextLine();
  21.             time[i] = Integer.parseInt(books[i].split(",")[1]);
  22.         }
  23.         boolean sorted = false;
  24.         int totalTime = 0;
  25.         while(!sorted)
  26.         {
  27.             sorted = true;
  28.             for(int i = 1; i < books.length; i++)
  29.             {
  30.                 if(stringsComparator(books[i-1], books[i]) == 1)
  31.                 {
  32.                     String tmp = books[i];
  33.                     books[i] = books[i-1];
  34.                     books[i-1] = tmp;
  35.                     totalTime+=Integer.parseInt(books[i].split(",")[1]);
  36.                     totalTime+=Integer.parseInt(books[i-1].split(",")[1]);
  37.                     sorted = false;
  38.                 }
  39.             }
  40.         }
  41.         System.out.println(totalTime);
  42.     }
  43.     public static int stringsComparator(String s1, String s2) // returns 1 if s1 greater, -1 si s1 lesser ?(is this a word?), 0 si equals
  44.     {
  45.         for(int i = 0; i < Math.min(s1.length(), s2.length()); i++)
  46.         {
  47.             if(s1.charAt(i) > s2.charAt(i))
  48.                 return 1;
  49.             else if(s1.charAt(i) < s2.charAt(i))
  50.                 return -1;
  51.         }
  52.         return 0;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement