https://www.acmicpc.net/problem/11292
문제
테스트 케이스마다 키가 가장 큰 친구의 이름을 출력하는 문제이다.
입력
학생의 수 n (0 < n <=50) 이 주어지고 학생 이름과 키가 공백으로 구별되 주어진다.
출력
키가 가장 큰사람의 이름 출력, 중복될경우 모두 출력
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool max(float a,float b){ //max 함수
return a > b;
}
int main()
{
pair<string,float> a = make_pair("A",0); // 입력받을 pair
vector<pair<string,float>> res; // 마지막에 출력할 pair 벡터
res.push_back(make_pair("a",0));
int n = 1;
while(n!=0){
cin >> n; // 테스트케이스 마다 친구들 수 받기
for(int i=0;i<n;i++){
string name;
float tall;
cin>>name;
cin>>tall;
a = make_pair(name,tall); // 친구 정보 저장
if (max(a.second,res.front().second)) // 현재 친구 정보와 현재까지 가장큰 친구 정보비교
{
res.clear(); // 그전 친구 정보 삭제
res.push_back(a); // 가장큰 친구 저장
}
else if (a.second == res.front().second){
res.push_back(a); // 중복일경우 벡터에 푸시
}
}
if(n ==0) // 0을 받으면 종료
break;
for(auto i:res) // 가장 큰 친구들 이름 출력
cout<<i.first<<" ";
res.clear(); // 친구 정보 초기화
res.push_back(make_pair("a",0));
cout<<"\n";
// break;
}
return 0;
}
'알고리즘' 카테고리의 다른 글
프로그래머스 타겟넘버 (python) (0) | 2022.08.07 |
---|---|
백준 (2460) 지능형 기차 2 (python) (0) | 2022.08.06 |
백준 (10818) 최소, 최대 (python) (0) | 2022.08.05 |
백준 (3460) 이진수 (python) (0) | 2022.08.04 |
백준 (2501) 약수 구하기 (파이썬) (0) | 2022.08.03 |