GitHubSeob
C++ / 프로그래머스 / 정수 내림차순으로 배치하기 본문
문제
https://programmers.co.kr/learn/courses/30/lessons/12933
코딩테스트 연습 - 정수 내림차순으로 배치하기
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. 제한 조건 n은 1이
programmers.co.kr
문제풀이
n을 to_string을 이용해 string형으로 바꾼다.
그다음 algorithm의 sort를 이용하여 내림차순으로 정렬한다.
그다음 stoll을 이용하여 string을 long long형으로 바꾼다.
코드
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
long long solution(long long n) {
long long answer = 0;
string ns = to_string(n);
int idx = 0;
sort(ns.begin(), ns.end(),greater<>());
answer=stoll(ns);
return answer;
}
'Programmers > Level 1' 카테고리의 다른 글
C++ / 프로그래머스 / 제일 작은 수 제거하기 (0) | 2021.09.13 |
---|---|
C++ / 프로그래머스 / 정수 제곱근 판별 (0) | 2021.09.13 |
C++ / 프로그래머스 / 자연수 뒤집어 배열로 만들기 (0) | 2021.09.13 |
C++ / 프로그래머스 / 자릿수 더하기 (0) | 2021.09.13 |
C++ / 프로그래머스 / 이상한 문자 만들기 (0) | 2021.09.13 |