[백준] 5648번 역원소 정렬 (C++, 정렬)

728x90
반응형

예제를 보고 알아서 정렬 조건을 만들어줘야 된다.

0을 없애기 위해 문자열을 정수로 변환한 후 다시 문자열로 만들어줬다

 

#define _CRT_SECURE_NO_WARNINGS
#define rep(i,s,e) for(int i=s;i<e;i++)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int cmp(const string& a, const string& b) {
	if (a.length() != b.length())return a.length() < b.length();
	return stol(a) < stol(b);
}


int main(int argc, char** argv)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n; 
	vector<string> a;
	string tmp;
	cin >> n;
	rep(i, 0, n) {
		cin >> tmp;
		reverse(tmp.begin(), tmp.end());
		a.push_back(to_string(stol(tmp)));
	}
	sort(a.begin(),a.end(),cmp);
	for (auto x : a) {
		cout << stol(x) << "\n";
	}
	return 0; 
}
728x90
반응형
TAGS.

Comments