본문 바로가기

BOJ

[백준] 10814번 나이순정렬 c++

#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
using namespace std;

class BackJun {
public:
	static int num;
	BackJun() {
	}
	int getAge() {
		return age;
	}
	string getName() {
		return name;
	}

	void setAge(int a) {
		age = a;
		num++;
	}
	void setName(string name) {
		this->name = name;
	}

	int getNum() {
		return num;
	}
private:
	int age;
	string name;
};

int BackJun::num = 1;
bool cmp(pair<BackJun, int> &a, pair<BackJun, int> &b) {
	if (a.first.getAge() == b.first.getAge()) return a.second < b.second;
	return a.first.getAge() < b.first.getAge();
}
int main(void) {
	ios::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);
	BackJun one;
	vector<pair<BackJun,int>> customer;
	int testCase, age,a;
	string name,b;
	cin >> testCase;
	for (int i = 0; i < testCase; i++) {
		cin >> age >> name;
		one.setAge(age);
		one.setName(name);
		a = one.getNum();
		customer.push_back(make_pair(one,a));
	}

	sort(customer.begin(), customer.end(), cmp);
	for (int i = 0; i < testCase; i++) {
		cout << customer[i].first.getAge() << " " << customer[i].first.getName() << "\n";
	}
}

클래스의 static변수

vector와 pair 이용해서 cmp까지 만들어준다음에 비교

'BOJ' 카테고리의 다른 글

[백준] 2839번 설탕 배달 c++  (0) 2020.05.03
[백준] 2581번 소수 c++  (0) 2020.05.03
[백준] 18883번 N M 찍기 c++  (0) 2020.05.03
[백준] 5622번 다이얼 c++  (0) 2020.04.26
[백준] 2908번 상수 c++  (0) 2020.04.26