Problem

3/10

비교기와 함께 세트 사용

Theory Click to read/hide

set 비교기와 함께 사용
내림차순으로 정렬된 집합을 만드는 비교기의 예입니다. 구조체 cmp { 부울 연산자() ( int a, int b) const{ 반환 > 비; } };
세트를 만들 때 비교기를 사용합니다. 세트 <int, cmp> 에스;

Problem

비교기로 프로그램을 완성하여 다음 문제를 해결하십시오.

주어진 N개의 자연수. 숫자의 자릿수 합으로 정렬된 집합을 출력합니다.
 
<헤드> <몸>
# 입력 출력
1 4
123 321 34 23
23 123 34
Write the program below
#include <iostream>
#include <set>

using namespace std;      
  
int main()
{
    int n, a;
    set <int, cmp> s;
    
    cin >> n;
    for(int i = 0; i<n; i++)
    {
       	 cin >> a;
       	 s.insert(a);
    }
    for(auto x: s)
        cout << x << " ";
}        

     

Program check result

To check the solution of the problem, you need to register or log in!