简体   繁体   中英

Python function to return smallest number

I do not understand the problem, for example, the sample input is 123 the next smallest natural number is 124 but the sum here is 7 so, next 125, 126, 127, all of which do not give a sum of 6. I do not understand how you get to 132?

can someone please clarify, is it just changing one digit or more than one?

Please write a function in Python which receives a natural decimal N-digit number ( 0<N<15 ) as an input and returns the minimal next N-digit number with the same sum of digits or -1 if there isn't one. Examples:

Input: Output:
123 132
0200 1001
09999999999999 18999999999999
90 -1
9999 -1

can someone please clarify, is it just changing one digit or more than one?

Change as many digits as needed, as long as the new number is greater than the original number, and has the same number of digits as the original.

For example, if the input is 0200, then 0011 is not an acceptable solution, because 0011 is less than 0200.

1100 is also an incorrect solution, because there is a smaller number that sums to 2: 1001

Another example, when input is 9999, the solution 18999 is incorrect because it has 5 digits, and 9999 has only 4 digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM