C ++ replacement joke

Garlic and a little naughty. This time, the test report her sister murdered in cold blood.

Originally recorded on the lab from sister sequence 1 to n, between any two numbers separated by spaces. However, "pit sister" garlic actually spaces between the digital gave deleted, the entire length of the digital sequence into a string of digits and no spaces header is 1 to 100.

Now my sister has anger, garlic find you quickly write a program to test data recovery.

Input :

Input file a line, a string - is the experimental data garlic messing.

The length of the string between 1 and 100.

Output :

The output common line, the sister of the original test data --1 to n outputs.

There is a space between any two data.

If more than one set in line with the requirements of the correct solution, output any of a group can be.

The only answer to this question does not meet the requirements of the answers are correct

Sample input Copy

4111109876532

Sample output copy

4 1 11 10 9 8 7 6 5 3 2

 

Ideas:

① Because the number of columns is 1 to 100, it may be a two-digit number may also be given a numeric string, to use the calculated number len the number of output, followed by the number to save the upcoming digital b output.

Number ② represented by x to be outputted to first determine the number of output before being there the same number, if not to use the array b to hold the value of x, followed by a recursive function will next passed to the function DEF; if then it specifies the number x is two digits to be output, it is stored with two numbers x, b used to hold the value of x, and the lower the number passed to the function def.

③ a final output format space when it is determined that all the numbers have been saved using the array b, b in accordance with the output of a number of outputs.

#include<iostream>
#include<string>
using namespace std;
char a[105];
int len, n, b[105];
bool f, vis[105];
void def(int s, int num)
{
    if (f) return ;
    if (s == len)
    {
        f = true;
        for (int i = 0;i < num;++i)
        {
            cout << b[i] << " ";
        }
        return ;
    }
    int x = a[s] - '0';
    if (!vis[x] && x <= n && x > 0)
    {
        vis[x] = true;
        b[num] = x;
        def(s + 1, num + 1);
        vis[x] = false;
    }
    x = x * 10 + a[s + 1] - '0';
    if (!vis[x] && x <= n && x > 0)
    {
        vis[x] = true;
        b[num] = x;
        def(s + 2, num + 1);
        vis[x] = false;
    }
}
int main() {
    cin >> a;
    len = strlen(a);
    n = (len <= 9 ? len:(len - 9) / 2 + 9);
    def(0, 0); 
}

 

Guess you like

Origin www.cnblogs.com/liushipeng648/p/12593432.html