Koder / 박성훈
article thumbnail

그냥 std 쓰자

편해서 좋았다.

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

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을

www.acmicpc.net

compare함수만 따로 잘 작성해주고

구조체 하나 짜주면 쉽게 풀 수 있다.

#include <stdio.h>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
struct JUDGE{ int priority; int age; char name[101]; };// 가입순서 - 나이 - 이름 
vector<JUDGE> v; 

bool compare(JUDGE a, JUDGE b){
	if(a.age != b.age) return a.age < b.age;
	else return a.priority <= b.priority;
}

int main(){
	int n;
	scanf("%d", &n);
	v.resize(n);
	for(int i=0; i<n; i++){
		scanf("%d %s",&v[i].age, &v[i].name);
		v[i].priority = i;
	}
	
	sort(v.begin(), v.end(), compare);
	for(int i=0; i<n; i++){
		printf("%d %s\n", v[i].age, v[i].name);
	}
	return 0;
}

AC.

반응형