Advertisement
MrMarvel

Who shall die?

Dec 16th, 2019
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package s_est_la_examinie;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.Date;
  13. import java.util.Iterator;
  14.  
  15. import javax.management.modelmbean.RequiredModelMBean;
  16.  
  17. public class D {
  18.     private static final File fileIn = new File("input.txt");
  19.     private static final File fileOut = new File("output.txt");
  20.     private static final ArrayList<Person> list = new ArrayList<Person>();
  21.     private static boolean noPeopleWithLessThanRequiredWeight = true;
  22.    
  23.     public static void main(String[] args) throws IOException {
  24.         long time = new Date().getTime();
  25.         //
  26.         run();
  27.         //
  28.         System.out.println("----------------------------");
  29.         System.out.println(new Date().getTime() - time + " ms");
  30.     }
  31.    
  32.     private static void run() throws IOException {
  33.         BufferedReader in = new BufferedReader(new FileReader(fileIn));
  34.         PrintWriter out = new PrintWriter(new FileWriter(fileOut));
  35.        
  36.         byte N = Byte.parseByte(in.readLine());
  37.        
  38.         for (int i = 0; i < N; i++) {
  39.             String[] msg = in.readLine().split(" ");
  40.             int weight = Integer.parseInt(msg[3]);
  41.             if (weight <= Person.REQIERED_WEIGHT) noPeopleWithLessThanRequiredWeight = false;
  42.             Person man = new Person(msg[0], msg[1], Integer.parseInt(msg[2]), weight);
  43.             list.add(man);
  44.         }
  45.         in.close();
  46.        
  47.         Collections.sort(list, Person.HEIGHT_WEIGHT_NAME_SURNAME_COMPARATOR);
  48.         for (int i = 0; i < list.size(); i++) {
  49.             Person p = list.get(i);
  50.             if (noPeopleWithLessThanRequiredWeight) {
  51.                 out.println(p);
  52.             } else {
  53.                 if (p.weight <= Person.REQIERED_WEIGHT) out.println(p);
  54.             }
  55.         }
  56.         out.close();
  57.     }
  58. }
  59.  
  60. class Person {
  61.     public static final int REQIERED_HEIGHT = 180;
  62.     public static final int REQIERED_WEIGHT = 75;
  63.     public static final Comparator<Person> HEIGHT_WEIGHT_NAME_SURNAME_COMPARATOR = new Comparator<Person>() {
  64.  
  65.         @Override
  66.         public int compare(Person o1, Person o2) {
  67.             if (Math.abs(o1.height - REQIERED_HEIGHT) > Math.abs(o2.height - REQIERED_HEIGHT)) return 1;
  68.             else if (o1.height != o2.height) return -1;
  69.             else if (Math.abs(o1.weight - REQIERED_WEIGHT) > Math.abs(o2.weight - REQIERED_WEIGHT)) return 1;
  70.             else if (o1.weight != o2.weight) return -1;
  71.             else if (o1.name.compareTo(o2.name) > 0) return 1;
  72.             else if (!o1.name.equals(o2.name)) return -1;
  73.             else if (o1.surname.compareTo(o2.surname) > 0) return 1;
  74.             else if (!o1.surname.equals(o2.surname)) return -1;
  75.             else return 0;
  76.         }
  77.        
  78.     };
  79.    
  80.    
  81.     String name, surname;
  82.     int height, weight;
  83.    
  84.     public Person(String name, String surname, int height, int weight) {
  85.         this.name = name;
  86.         this.surname = surname;
  87.         this.height = height;
  88.         this.weight = weight;
  89.     }
  90.    
  91.     public String toString() {
  92.         return name + " " + surname + " " + height + " " + weight;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement