20124

최명수·2021년 1월 22일

20124

1.아이디어
처음에 pair벡터를 사용하여 문자열과 정수를 동시에 받았다. 그리고 pair벡터에 push_back을 이용하여 입력받으 문자열과 정수를 v[0].first와 second에 넣었다. 그리고 마지막에 최고점수가 높은 사람이 여러명이면 사전순으로 앞선 사람을 출력하는 것은 sort를 이용해 구현하였다.

//pair 벡터를 사용하여 문자열과 정수를 동시에 받는다.
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

int main()
{
    int n,max=0,count=0,index=0;
    cin>>n;
    int number=n;
    //string과 int를 가지는 pair 벡터 v를만듬 
    vector<pair<string, int>> v;
    cin.ignore();
    while(n--)
    {               
        string tmp;
        int number;
        cin>>tmp>>number;
        //v[0] first와second부터 넣는다.
        v.push_back(pair<string,int>(tmp,number));           
    }
    
    //가장많은 숫자를 체크해주는 for문
    for(int i=0; i<number; i++)
    {
        if(v[i].second>=max)
        {
            max=v[i].second;
        }
    }
    //높은사람이 몇명인지 체크해주는 for문
    for(int i=0; i<number; i++)
    {
        if(v[i].second==max)
        {
            count++;
            index=i;
        }
    }
    //1명이면 그 사람을 출력하고
    if(count==1)
    {
        cout<<v[index].first;
    }
    //1명이아니면 sort를 사용하여 사전순으로 정렬한후 가장 사전에서 앞선사람출력
    else if(count>1)
    {
        sort(v.begin(),v.end());
        for(int i=0; i<number; i++)
        {
            if(v[i].second==max)
            {
                cout<<v[i].first;
                break;
            }
        }
    }
}

## 3.깨달은점
실제로 스스로 pair 벡터를 생각하여 사용해본적은 이번이 처음이었는데 정말 편리하고 좋은 것 같다. 조금만 더 익숙해지자.

0개의 댓글