0

The minimum number without a digit, solve in c++ with while loop

Hello! I have to solve this problem in c++ with while loop. Below I put a solution, but I receive 2/4 correct tests. Careful! I must remove only 1 digit from the number, even if the same digit appears 2 or more times. If we take the number 4832851, I have to eliminate the first digit 8 from left to right, thus resulting in the final number 432851. How could I eliminate only that one, and not both or not on the other digit 8? The problem sounds like that: Requirement A natural number N is read from the keyboard. Calculate the minimum number obtained by removing a single digit from the initial number. Input data Read the natural number N. from the keyboard. Output data The minimum number will be displayed after deleting a digit. Restrictions and specifications 0 <N <1,000,000,000 Example Input data Output data 5912 512 My solution: #include using namespace std; int main() { unsigned long int N, copyOfN, newN; int digitMax = 0, multiply = 1; int counter = 1; cin >> N; copyOfN = N; newN = 0; while (N != 0) { if (N % 10 > digitMax) { digitMax = N % 10; } N = N / 10; } while (copyOfN != 0) { if (copyOfN % 10 != digitMax || counter == 0) { newN = (copyOfN % 10) * multiply + newN; multiply = multiply * 10; } else if (copyOfN % 10 == digitMax && counter == 1) { counter = 0; } copyOfN = copyOfN / 10; } cout << newN; return 0; }

5th Apr 2021, 12:31 PM
Florin H
Florin H - avatar
4 Respostas
+ 1
Your solution removes the greatest digit, which will not work all of the time. For example, assume N = 329. If you remove 9 as the greatest digit, you get 32, but obviously 29 is the minimum number you can aquire by deleting 3. I explained a possible solution to this problem in a different thread some time ago: https://www.sololearn.com/discuss/2700286/?ref=app Since your approach is already incorrect, it is somewhat hard to point out specific problems in your code. I would suggest you to read through the other thread linked above, and try to implement a better algorithm. If you think you need help with that or my other answer there is not understandable somewhere, feel free to ask again.
5th Apr 2021, 2:51 PM
Shadow
Shadow - avatar
+ 1
As an example, here is the code I wrote back then: https://code.sololearn.com/ca1809a25A13/?ref=app The first loop searches for the left-most digit greater than or equal to its predecessor, e.g. 3 in 329. First, I assume there is no such digit, such that I can delete the last digit, and then repeatedly check adjacent digits, updating an index along the way if a fitting digit is found. The second loop simply skips the digit at that particular index when constructing the number again. If you must use while loops, the transformation from the for loop I used is trivial. Just pull the variable definition before the loop, and put the loop expression, e.g. n /= 10 in the second loop, right at the end of the loop's body.
6th Apr 2021, 3:38 PM
Shadow
Shadow - avatar
0
Hello! Thanks for the reply. I had seen your previous post. I realized I was wrong, looking for the highest number. Only your version I do not realize how I could implement it in the code (using the while loop). I'm a beginner. I've been thinking about a solution for a few days now and I don't see it.
6th Apr 2021, 3:05 PM
Florin H
Florin H - avatar
0
thanks a lot, it worked. now let me look at your solution to make sure I understand. thank you very much for your help
6th Apr 2021, 4:26 PM
Florin H
Florin H - avatar