import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(Integer.parseInt(br.readLine()));
}
Collections.sort(list);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0; i < n; i++) {
bw.write(list.get(i) + "\n");
}
bw.flush();
}
}
import java.io.*;
import java.util.*;
public class Main {
static class Point implements Comparable<Point> {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Point that) {
if(this.x < that.x) {
return -1;
} else if(this.x == that.x) {
if(this.y < that.y) {
return -1;
} else if(this.y == that.y) {
return 0;
} else {
return 1;
}
} else {
return 1;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Point a[] = new Point[n];
for(int i = 0; i < n; i++) {
String[] temp = br.readLine().split(" ");
int x = Integer.parseInt(temp[0]);
int y = Integer.parseInt(temp[1]);
a[i] = new Point(x, y);
}
Arrays.sort(a);
StringBuilder sb = new StringBuilder();
for(Point p : a) {
sb.append(p.x + " " + p.y + "\n");
}
System.out.print(sb);
}
}
Comparable을 이용하여 해결한다.
import java.io.*;
import java.util.*;
public class Main {
static class Point implements Comparable<Point> {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Point that) {
if(this.y < that.y) {
return -1;
} else if(this.y == that.y) {
if(this.x < that.x) {
return -1;
} else if(this.x == that.x) {
return 0;
} else {
return 1;
}
} else {
return 1;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Point a[] = new Point[n];
for(int i = 0; i < n; i++) {
String[] temp = br.readLine().split(" ");
int x = Integer.parseInt(temp[0]);
int y = Integer.parseInt(temp[1]);
a[i] = new Point(x, y);
}
Arrays.sort(a);
StringBuilder sb = new StringBuilder();
for(Point p : a) {
sb.append(p.x + " " + p.y + "\n");
}
System.out.print(sb);
}
}
좌표 정렬하기 문제에서 비교 함수를 y를 우선해서 비교하게 변경하면 된다.
import java.io.*;
import java.util.*;
public class Main {
static class Person implements Comparable<Person> {
int age;
String name;
int join;
Person(int age, String name, int join) {
this.age = age;
this.name = name;
this.join = join;
}
public int compareTo(Person that) {
if(this.age < that.age) {
return -1;
} else if(this.age == that.age) {
if(this.join < that.join) {
return -1;
} else if(this.join == join) {
return 0;
} else {
return 1;
}
} else {
return 1;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Person a[] = new Person[n];
for(int i = 0; i < n; i++) {
String line[] = br.readLine().split(" ");
a[i] = new Person(Integer.parseInt(line[0]), line[1], i);
}
Arrays.sort(a);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(a[i].age + " " + a[i].name + "\n");
}
System.out.print(sb);
}
}
Comparable을 이용해서 비교 함수를 age를 우선해서 비교하고 같은 경우에는 join을 이용해서 비교하게 구현하면 된다.
import java.util.*;
public class DocumentTheft {
static class Info implements Comparable<Info> {
public String name;
public int time;
Info(String name, int time) {
this.name = name;
this.time = time;
}
@Override
public int compareTo(Info ob) {
return this.time - ob.time;
}
}
public static int getTime(String time) {
int h = Integer.parseInt(time.split(":")[0]);
int m = Integer.parseInt(time.split(":")[1]);
return h * 60 + m;
}
public static String[] solution(String[] reports, String times) {
ArrayList<Info> tmp = new ArrayList<>();
for(String x : reports) {
String a = x.split(" ")[0];
String b = x.split(" ")[1];
tmp.add(new Info(a, getTime(b)));
}
Collections.sort(tmp);
int s = getTime(times.split(" ")[0]);
int e = getTime(times.split(" ")[1]);
ArrayList<String> res = new ArrayList<>();
for(Info ob : tmp) {
if(ob.time >= s && ob.time <= e) {
res.add(ob.name);
}
if(ob.time > e) {
break;
}
}
String answer[] = new String[res.size()];
for(int i = 0; i < res.size(); i++) {
answer[i] = res.get(i);
}
return answer;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(DocumentTheft.solution(new String[]{"john 15:23", "daniel 09:30", "tom 07:23", "park 09:59", "luis 08:57" }, "08:33 09:45")));
}
}
시간을 분 단위로 환산해서 파싱한다. 그 다음 시간을 기준으로 정렬을 하고 범위 안에 속하는 사람을 answer에 추가해주면 된다.