백준
1427 소트인사이트
불타는강정
2019. 11. 3. 15:10
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
sort(s.begin(), s.end());
reverse(s.begin(), s.end());
cout << s;
return 0;
}
내림차순으로 정렬하는 문제였다. 나는 이렇게 정렬했는데 이 방법 말고 더 간단하게 할 수 있었다!
C++에는 내장 비교함수가 있는 데 그 중 greater과 less가 있다. less가 디폴트고 내림차순 정렬이 하고 싶으면 greater를 써주면 된다!
#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
int main() {
string s;
cin >> s;
sort(s.begin(), s.end(), greater<char>());
cout << s;
return 0;
}