Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4.  
  5. // Collaborator: Dhipta Raditya Y
  6. public class UnlockNamronMothership {
  7.  
  8.     public static void main(String args[]) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         int n = Integer.parseInt(reader.readLine());
  12.         PasanganAB[] pasanganABs = new PasanganAB[n];
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String[] lines = reader.readLine().split(" ");
  16.             int a = Integer.parseInt(lines[0]);
  17.             int b = Integer.parseInt(lines[1]);
  18.             pasanganABs[i] = new PasanganAB(a, b);
  19.         }
  20.  
  21.         selectionSort(pasanganABs);
  22.         printHasilSorting(pasanganABs);
  23.     }
  24.  
  25.     private static void printHasilSorting(PasanganAB[] arr) {
  26.         int n = arr.length;
  27.         for (int i = 0; i < n; i++) {
  28.             System.out.println(arr[i].a + " " + arr[i].b);
  29.         }
  30.     }
  31.  
  32.     private static void selectionSort(PasanganAB[] arr) {
  33.         int n = arr.length;
  34.         int minimumIndeks;
  35.         for(int i=0; i < n; i++) {
  36.             minimumIndeks = i;
  37.             for(int j=i+1; j < n; j++){
  38.                 if (arr[j].hitungK() < arr[minimumIndeks].hitungK())
  39.                     minimumIndeks = j;
  40.                 else if(arr[j].hitungK() == arr[minimumIndeks].hitungK()){
  41.                     if(arr[j].compareTo(arr[minimumIndeks]) == -1) minimumIndeks = j;
  42.                     }
  43.             }
  44.             PasanganAB temp = arr[minimumIndeks];
  45.             arr[minimumIndeks] = arr[i];
  46.             arr[i] = temp;
  47.         }
  48.     }
  49. }
  50.  
  51. class PasanganAB implements Comparable<PasanganAB> {
  52.     // TODO: Lengkapi class dengan instance variable, constructor, dan method yang sesuai dan menurut Anda diperlukan
  53.     public int a, b;
  54.  
  55.     public PasanganAB(int a, int b) {
  56.         this.a = a;
  57.         this.b = b;
  58.     }
  59.  
  60.     // FIXME: Anda mungkin ingin memperbaiki cara kerja method ini
  61.     public int hitungK() {
  62.         int ret = 0;
  63.         int x;
  64.         int[] iter = new int[] {1,7,12};
  65.         if (a == 0) {
  66.             if(b == 0) ret = 0;
  67.             else if(b == 1) ret = 3;
  68.             else if(b == 2) ret = 5;
  69.             else {x = (b-3)%3;
  70.             ret = iter[x];}
  71.         } else if (a == 1) {
  72.             x = b % 3;
  73.             ret = iter[x];
  74.         } else if (a == 2) {
  75.             ret = 2;
  76.         } else if (a == 3) {
  77.             if(b == 0) ret = 3;
  78.             else if(b == 1) ret = 5;
  79.             else {x = (b-2) % 3;
  80.             ret = iter[x];}
  81.         } else if (a == 4) {
  82.             if(b==0) ret = 4;
  83.             else if(b==1) ret = 16;
  84.             else if(b==2) ret = 7;
  85.             else if(b==3) ret = 12;
  86.             else {x = (b-4) % 3;
  87.             ret = iter[x];}
  88.         } else if (a == 5) {
  89.             if(b==0) ret = 5;
  90.             else {x = (b-1) % 3;
  91.             ret = iter[x];}
  92.         } else if (a == 6) {
  93.             if(b==0) ret = 6;
  94.             else {ret = 11;}
  95.         } else if (a == 7) {
  96.             if(b==0) ret = 7;
  97.             else if(b==1) ret = 12;
  98.             else {x = (b-2) % 3;
  99.             ret = iter[x];}
  100.         } else if (a == 8) {
  101.             if(b==0) ret = 8;
  102.             else if(b==1) ret = 4;
  103.             else if(b==2) ret = 16;
  104.             else if(b==3) ret = 7;
  105.             else if(b==4) ret = 12;
  106.             else {x = (b-5) % 3;
  107.             ret = iter[x];}
  108.         } else if (a == 9) {
  109.             if(b==0) ret = 9;
  110.             else if(b==1) ret = 4;
  111.             else if(b==2) ret = 16;
  112.             else if(b==3) ret = 7;
  113.             else if(b==4) ret = 12;
  114.             else {x = (b-5) % 3;
  115.             ret = iter[x];}
  116.         } else if (a == 10) {
  117.             if(b==0) ret = 10;
  118.             else if(b==1) ret = 12;
  119.             else {x = (b-2) % 3;
  120.             ret = iter[x];}
  121.         }
  122.         return ret;
  123.     }
  124.  
  125.     @Override
  126.     public int compareTo(PasanganAB other) {
  127.         // this = -1, other = 1
  128.         int ret = 0;
  129.         if(this.a - other.a > 0) ret = 1;// this a lebih besar
  130.         else if(this.a - other.a < 0) ret = -1;// other a lebih besar
  131.         else {
  132.             if(this.b - other.b > 0) ret = 1;// this b lebih besar
  133.             else if(this.b - other.b < 0) ret = -1;// other b lebih besar
  134.             else ret = 0;
  135.         }
  136.  
  137.         return ret;
  138.     }
  139.  
  140.    
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement