Skip to content

Instantly share code, notes, and snippets.

@so77id
Created April 17, 2020 22:24
Show Gist options
  • Save so77id/910337130122490cd8781f9431ed1f08 to your computer and use it in GitHub Desktop.
Save so77id/910337130122490cd8781f9431ed1f08 to your computer and use it in GitHub Desktop.
VectorExample
import java.util.Scanner; // Import the Scanner class
import java.util.Vector; // import the ArrayList class
import java.util.Collections;
public class VectorExample {
public static class Pair {
private int first;
private int second;;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int first() {return this.first;}
public int second() {return this.second;}
}
public static void main(String[] args){
int n, buff1, buff2;
Scanner scanner = new Scanner(System.in); // Create a Scanner object
Vector<Pair> vec = new Vector<Pair>();
n = scanner.nextInt();
for(int i=0; i<n; i++) {
buff1 = scanner.nextInt();
buff2 = scanner.nextInt();
Pair tmp = new Pair(buff1, buff2);
vec.add(tmp);
}
Collections.sort(vec, (a, b) -> {
int sum_a = a.first() + a.second();
int sum_b = b.first() + b.second();
return sum_b - sum_a;
});
for(int i=0; i<n; i++) {
Pair tmp = vec.get(i);
System.out.println(tmp.first + " " + tmp.second);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment