have to do_yeon

[백준 / C++] 7568. 덩치 본문

C++/Baekjoon (C++)

[백준 / C++] 7568. 덩치

또김또 2022. 8. 27. 18:42

https://www.acmicpc.net/problem/7568

 

7568번: 덩치

우리는 사람의 덩치를 키와 몸무게, 이 두 개의 값으로 표현하여 그 등수를 매겨보려고 한다. 어떤 사람의 몸무게가 x kg이고 키가 y cm라면 이 사람의 덩치는 (x, y)로 표시된다. 두 사람 A 와 B의 덩

www.acmicpc.net

 

 

 


 

반복문을 사용하려고 했지만 아무리 생각해도 아닌 것 같아서 두 개를 한 쌍으로 취급하는 방법, 또는 개념이 있는지 알아보았다.

그 개념은 바로 pair 이다.

https://have-to-do-yeon.tistory.com/49

 

[C++] Pair

Pair 두 데이터 값을 하나의 쌍으로 묶어줄 때 사용한다. 헤더파일 및 선언방법 헤더파일 : Pair 선언 : pair<자료형1, 자료형2> 이름; #include int main(){ pair name; // int형, char형이 한 쌍인 pair 선언 }..

have-to-do-yeon.tistory.com

자신보다 큰 몸무게와 키를 가진 사람이 있을 경우 등수를 1등씩 밀어나가는 방식이다.

 


제출 답안
#include <iostream>
#include <utility>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    
    int n, rank = 1;
    pair<int,int> human[50];
    cin>> n;

    for(int i = 0; i < n; i++){
        cin>> human[i].first >> human[i].second;
    }

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(human[i].first < human[j].first && human[i].second < human[j].second) rank++;
        }   
        cout<< rank << " ";
        rank = 1;
    }
}
Comments