C++ Coding Test Course, Sorting Digits in Descending Order

Problem Description

This is a problem of creating a new integer by sorting the digits of a given integer in descending order.
For example, if the given integer is 4213, the result after sorting the digits in descending order is 4321.
This type of problem is commonly encountered in C++ coding tests, and it tests the ability to solve problems based on an understanding of sorting algorithms.

Problem Input

An integer N (0 ≤ N ≤ 2,147,483,647) is given in the first line.

Problem Output

Output a new integer by sorting the digits of integer N in descending order.

Input Example

4213

Output Example

4321

Solution Process

To solve this problem, several steps must be followed.
The main steps are as follows.

1. Input

First, we need to receive an integer input.
In C++, we can use cin to take input from the user.
The received integer will later be converted to a character type for the purpose of separating the digits.

2. Digit Separation

After converting the input integer to a character array, we can separate each digit individually.
In C++, the to_string function can be used to convert an integer to a string.
Each character of the converted string can be stored in a vector.

3. Descending Sort

After separating the digits, they need to be sorted in descending order.
The sort function in C++ can be used, and a comparison function can be applied to perform the descending sort.
Specifically, since the digits are sorted as characters, the ASCII values of the numbers must be considered during sorting.

4. Integer Conversion

After sorting the digits, convert them back to a string and then to an integer to obtain the final result.
Finally, use cout to display the result.

Code Example


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    // 1. Input
    int N;
    cin >> N;

    // 2. Digit Separation
    string str = to_string(N);
    vector digits(str.begin(), str.end());

    // 3. Descending Sort
    sort(digits.begin(), digits.end(), greater());

    // 4. Integer Conversion
    string sorted_str(digits.begin(), digits.end());
    int result = stoi(sorted_str);

    // Output result
    cout << result << endl;

    return 0;
}
    

Result

When the above code is executed, you can see the result of sorting the digits of the input integer in descending order.
For example, when 4213 is inputted, the output will show 4321.

Exception Handling

To solve this problem, exceptional situations must also be considered.
For instance, if the input value is 0, the output should also be 0.
Therefore, when writing the code, handling these exceptional situations should also be added.

Exception Handling Code Example


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    // 1. Input
    int N;
    cin >> N;

    // 0 Exception Handling
    if (N == 0) {
        cout << 0 << endl;
        return 0;
    }

    // 2. Digit Separation
    string str = to_string(N);
    vector digits(str.begin(), str.end());

    // 3. Descending Sort
    sort(digits.begin(), digits.end(), greater());

    // 4. Integer Conversion
    string sorted_str(digits.begin(), digits.end());
    int result = stoi(sorted_str);

    // Output result
    cout << result << endl;

    return 0;
}
    

Conclusion

In this lesson, we learned how to solve an algorithm problem that involves sorting digits in descending order.
We were able to solve the problem by using basic input/output and data structures in C++.
Problems of this type greatly aid in developing basic algorithmic skills.
Similar logic can be applied to other problems, so I hope you will practice by solving a variety of problems to improve your skills.