[백준 1427] 소트인사이드

alsry._.112·2023년 9월 9일
0

백준

목록 보기
49/102

🔗문제 풀러가기
단계별로 풀어보기 단계 13의 6번째 문제이다.

문제 분석


코드

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int arr[10];

	int input;
	cin >> input;

	int cnt = 0;

	while (input != 0)
	{
		arr[cnt] = input % 10;

		input /= 10;
		cnt++;
	}

	sort(arr, arr + cnt, greater<int>());

	for (int i = 0; i < cnt; i++)
	{
		cout << arr[i];
	}
}

해석

  1. 수를 입력 받은 후 입력받은 수를 배열로 나타내기 위해 while문을 통해 입력받은 수를 10으로 계속해서 나누어 arr에 저장한다.
  2. 이렇게 얻은 arr를 내림차순으로 정렬하고 출력하면 끝!

sort의 세번째 인자로 greater<자료형>()
추가하여 내림차순으로 정렬한다.

profile
소통해요

0개의 댓글