0
Problem C++ with while loop
Guys I get stuck with this problem 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
4 ответов
+ 1
The code just multiplies the number by 10 with some extra steps, how is that related to finding the smallest number from deleting a digit?
Due to the way number systems work, you want to delete the first digit that is greater than its predecessor. You can split this into two tasks:
1) Find the leftmost digit that is greater that the digit to its right. A single loop is all you need, you can just iteratively compare the last and the second-last digit and update some index variable if the
last digit is smaller.
2) Reconstruct the number without the previously determined digit. Again, it only takes one loop, as you can just build up the number similarly to the way you did in your code above, while checking if you have reached the particular index and leaving out the corresponding digit.
That's at least how I solved it without resorting to other stuff like arrays and strings, and should give you an idea what to do.
+ 1
You could input your number as a string and delete one character. Is it easy, does it?
0
#include <iostream>
using namespace std;
int main(){
unsigned int N;
int m=0,p=1;
cin>>N;
while(N!=0){
p=p*10;
m=(N%10)*p+m;
N=N/10;
}
cout<<m;
return 0;
}
0
this I tried