CodingTest
백준(Backjoon) 7568, 덩치
김용환
2021. 7. 12. 18:21
문제풀이
몸무게와 키의 값을 담을 Person 클래스를 정의하고 배열로 저장한다. 그리고 이중 반복문을 통해서 키와 몸무게를 비교하고 rank 필드 멤버를 증가시킨다.
소스코드
public class Main {
static class Person
{
int rank=1;
int weight; // 몸무게
int height; // 키
Person(int weight, int height)
{
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Person [rank=" + rank + ", weight=" + weight + ", height=" + height + "]";
}
}
public static String solution(Person[] persons)
{
StringBuilder answer = new StringBuilder();
for(int i=0;i<persons.length;i++)
{
for(int j=i+1;j<persons.length;j++)
{
if(persons[i].weight > persons[j].weight &&
persons[i].height > persons[j].height)
{
persons[j].rank++;
}else if(persons[i].weight < persons[j].weight &&
persons[i].height < persons[j].height)
{
persons[i].rank++;
}
}
answer.append(persons[i].rank + " ");
}
return answer.toString().trim();
}
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Person[] persons = new Person[n];
for(int i=0;i<n;i++)
{
String str[] = br.readLine().split(" ");
persons[i] = new Person(Integer.parseInt(str[0]),Integer.parseInt(str[1]));
}
System.out.println(solution(persons));
}
}