I think this is a more difficult problem, the meaning of the title is relatively easy to understand. But it’s harder to deal with

The title

The first idea is to sort a number twice and then subtract the resulting number again until the number black hole “6174” appears. It is worth noting that the input four digits are all the same, so the output is direct
N N = 0000 N – N = 0000
.

When doing the problem, we should pay attention to two small points. The test requires that the input N is 0~1e4, so the input may not be four digits. In this case, we need to manually add 0 before the string to make up the four digits. And the second thing is just in case you input 6174 the first time, so if you have a do while loop, anyway, do a loop first, and it says 7641-6174 = 6174.

First, use a variety of functions, ideas, code is very simple, as long as you can use the function will be. Let’s see what functions we need to use.

The insert function

string &insert(int p0, const char *s); Insert string s at position P0

string &insert(int p0, const char *s, int n); Insert the first n characters of string s at position P0

string &insert(int p0,const string &s); Insert string s at position P0

string &insert(int p0,const string &s, int pos, int n); Insert string s at position P0, n consecutive characters from pos

string &insert(int p0, int n, char c); Insert n characters c at p0

iterator insert(iterator it, char c); Insert the character c at it, returning the position of the inserted iterator

void insert(iterator it, const_iterator first, const_iteratorlast); Void insert(iterator it, int n, char c) void insert(iterator it, int n, char c); Insert n characters c at it

It’s easy to see that insert is an insert function. The above iterator is an iterator that represents a position in a String. In general, I’ll write 0, which means I plug in from the beginning. And the number of things that you insert and the number of things that you insert is the argument that you write in. So what we’re going to do here is we’re going to insert a 0 at the beginning, and we’re going to fill in four digits.

Sort function

The first two arguments to sort are the start address and the end address. Begin (); end(); end(); end()

Begin: Gets the pointer to the array header

End: Gets a pointer to the last element of the array +1

The sort function sorts strings using begin() and end(). General: sort(s.beam (),s.end()); This is the default sort from smallest to largest and when you want to sort in descending order, you write a CMP function bool compare(int a,int b) {return a< b; // Return a>b in descending order}.

Stoi function

Stoi () does a very thoughtful range check. The default range is within the range of an int, and if it is outside the range, it will raise runtime error. Of course, only numbers in the string can be converted to int. There is no need to convert other characters to numbers.

To_string function

This function, in contrast, converts integers to string representations. This is also a simple function to use. Once you’ve introduced these functions, the code is simple.

// The number black hole is called 6174
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

string s;


bool cmp(char a,char b){
    return a > b;
}

int main(){
    cin >> s;
    s.insert(0.4 - s.length(), '0');// If there are less than four digits, insert 0
    
    do {
    string a = s,b = s;
    sort(a.begin(),a.end(),cmp);// Sort from largest to smallest
    sort(b.begin(),b.end());/ / in turn
    int res = stoi(a) - stoi(b);
    s = to_string(res);
    s.insert(0.4 - s.length(), '0');// If there are less than four digits, insert 0
    cout << a << "-" << b << "=" << s << endl; 
    } while(s ! ="6174"&& s ! ="0000");

    return 0;
}
Copy the code

This is simple, and there is other code that doesn’t apply to so many functions or uses the char[] type, but nothing quite as neat. Topic link