문제풀이/백준oj
[백준OJ] 7568번 덩치
Hyeon-Uk
2021. 11. 5. 14:49
반응형
https://www.acmicpc.net/problem/7568
7568번: 덩치
우리는 사람의 덩치를 키와 몸무게, 이 두 개의 값으로 표현하여 그 등수를 매겨보려고 한다. 어떤 사람의 몸무게가 x kg이고 키가 y cm라면 이 사람의 덩치는 (x, y)로 표시된다. 두 사람 A 와 B의 덩
www.acmicpc.net
풀이
먼저 N명의 정보를 입력받은다음에, 모두를 조건에 맞게 비교를 했다. 이때 compareTo메소드를 오버라이딩해서 height와 weight값 모두가 자신보다 큰 경우를 추가시켜주어서 문제를 해결했다.
시간복잡도
이중for문으로 해결이 가능하기때문에 O(N2)이 된다.
코드
import java.util.*;
public class Main {
static Vector<Person> v=new Vector<>();
static int n;
static class Person implements Comparable<Person>{
int weight;
int height;
public Person(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public int compareTo(Person o) {
if(this.weight<o.weight&&this.height<o.height){
return 1;
}
else{
return 0;
}
}
}
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();
for(int i=0;i<n;i++){
int w=scanner.nextInt();
int h=scanner.nextInt();
v.add(new Person(w,h));
}
for(int i=0;i<n;i++){
int rank=1;
for(int j=0;j<n;j++){
if(i==j) continue;
if(v.get(i).compareTo(v.get(j))==1) rank++;
}
System.out.print(rank+" ");
}
scanner.close();
}
}
반응형