Home > OS >  How to calculate the smallest number possible based on digits (0-9) not used in a user inputted inte
How to calculate the smallest number possible based on digits (0-9) not used in a user inputted inte

Time:07-20

I am trying to write a code that will accept an integer input and then calculate the smallest number possible using digits not found in the inputted integer. The possible digits would be 0-9, however, 0 can not be the leading value in the output.

For example, if the user enters:

6789

the the program would output:

102345

How can I solve this?

CodePudding user response:

Let's divide your question into three steps:

1:find the number not found in input;

2:sort the not found numbers;

3:do not put 0 at first.

So it is easy to write code like this:

std::vector<int> total;
std::vector<int> res;

//init
for(int i=0;i<10;i  )
    total.emplace_back(i);

while(input>0)
{
    total[input]=-1;
    input = input/10;
}
for(int i=0;i<10;i  )
{
    if(total[i]!=-1)
        res.emplace_back(i);
}

if(res.size()>1&&res[0]==0)
    std::swap(res[0],res[1]);

for(auto &i:res)
    std::cout<<i;

CodePudding user response:

The lowest number possible from any set of digits (ignoring, for now, the issue of the zero) comprises those digits in order; thus, from the digits 2, 1, 6 and 3, the lowest number is 1236.

So, we can start of with a list of all digits, in order, then run through the digits in the given input number (after we have converted that to a string), removing each of those digits from our list (if it's still in it). If we end up with a list whose first element is zero, we simply swap that with the second digit.

Here's a possible implementation:

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

int main()
{
    std::string numbs = "0123456789";
    int input;
    std::cout << "Enter a number: ";
    std::cin >> input;
    std::string check = std::to_string(input); // Convert our input to a string
    for (auto digit : check) { // Remove each digit in that from our list...
        size_t p;
        if ((p = numbs.find(digit)) != std::string::npos) numbs.erase(p, 1);
    }
    // A basic error check that at least one digit remains ...
    if (numbs.length() == 0) {
        std::cout << "No digit left with which to make a number!\n";
        return 1;
    }
    // Swap first two digits if first is zero and there is at least one other ...
    if (numbs[0] == '0' && numbs.length() > 1) std::swap(numbs[0], numbs[1]);
    int answer = std::stoi(numbs);
    std::cout << answer << std::endl;
    return 0;
}

In this example, I have used the std::string container class from the Standard Library; in many ways, that acts like an array of characters; however, if you want to use actual arrays, you could readily adapt the shown code to use them.

CodePudding user response:

Yet another implementation. Same algorithm as from Adrian . . .

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    // Basic string
    std::string allDigits{ "0123456789" };

    // Get input. Digits only
    if (std::string input{}; std::getline(std::cin, input) and std::all_of(input.begin(), input.end(), std::isdigit)) {

        // Erase from the allDigits string the characters that are in the input string
        std::erase_if(allDigits, [&](const char d) { return std::any_of(input.begin(), input.end(), [d](const char c) { return c == d; }); });

        // Take care of leading 0
        if ((allDigits.length() > 1) and allDigits.front() == '0') std::swap(allDigits[0], allDigits[1]);

        // Show result
        std::cout << allDigits << '\n';
    }
    else std::cerr << "\n*** Error: Invalid input\n\n";
}
  • Related