Numero ordinato sono quei numeri le cui cifre sono disposte in ordine non decrescente. Qui ci viene dato un numero e dobbiamo trovarne un altro più piccolo ma più vicino al numero indicato e anche quel numero dovrebbe essere ordinato, cioè la loro cifra dovrebbe essere in ordine non decrescente.
Esempi:
Input : 91234 Output : 89999 Tidy property is violated by appearing 1 after 9. So we will reduce 9 by 1 and the number right to it will be replaced by 9. So generated tidy number is 89999. Input : 45000 Output : 44999
L'idea è di attraversare dalla fine. Ogni volta che viene violata la proprietà ordinata, riduciamo la cifra di uno e rendiamo tutte le cifre successive 9.
C++
// C++ program to find closest // tidy number smaller than the // given number #include using namespace std; char* tidyNum(char str[] int len) { for (int i = len-2; i >= 0; i--) { // check whether string violates tidy property if (str[i] > str[i+1]) { // if string violates tidy property then // decrease the value stored at that index by 1 // and replace all the value stored right to // that index by 9 (char)str[i]--; for (int j=i+1; j<len; j++) str[j] = '9'; } } return str; } // Driver code int main() { char str[] = '11333445538'; int len = strlen(str); // num will store closest tidy number char *num = tidyNum(str len); printf('%sn' num); return 0; }
Java // Java program to find closest // tidy number smaller than the // given number import java.io.*; class GFG { static String tidyNum(String str1 int len) { char[] str = str1.toCharArray(); for (int i = len - 2; i >= 0; i--) { // check whether string // violates tidy property if (str[i] > str[i + 1]) { // if string violates tidy // property then decrease the // value stored at that index // by 1 and replace all the value // stored right to that index by 9 str[i]--; for (int j = i + 1; j < len; j++) str[j] = '9'; } } return String.valueOf(str); } // Driver code public static void main(String[] args) { String str = '11333445538'; int len = str.length(); // num will store closest tidy number System.out.println(tidyNum(str len)); } } // This code is contributed by mits
Python3 # Python 3 program to find closest # tidy number smaller than the # given number def tidyNum(str len): for i in range(len-2 -1 -1): # check whether string # violates tidy property if (str[i] > str[i+1]): # if string violates tidy # property then decrease the # value stored at that index by 1 # and replace all the value # stored right to that index by 9 str[i] -= 1 for j in range(i+1 len): str[j] = 9 return str # Driver code str = [11333445538] len = len(str) # num will store closest tidy number num = tidyNum(str len) for i in range(0len): print(str[i] end = '') # This code is contributed by # Smitha Dinesh Semwal
C# // C# program to find closest // tidy number smaller than the // given number using System; class GFG { static String tidyNum(String str1 int len) { char[] str = str1.ToCharArray(); for (int i = len - 2; i >= 0; i--) { // check whether string // violates tidy property if (str[i] > str[i + 1]) { // if string violates tidy // property then decrease the // value stored at that index // by 1 and replace all the value // stored right to that index by 9 str[i]--; for (int j = i + 1; j < len; j++) str[j] = '9'; } } string s = new string(str); return s; } // Driver code static void Main() { String str = '11333445538'; int len = str.Length; // num will store closest tidy number Console.WriteLine(tidyNum(str len)); } } // This code is contributed by mits
PHP // PHP program to find closest // tidy number smaller than the // given number function tidyNum($str $len) { for ($i = $len - 2; $i >= 0; $i--) { // check whether string // violates tidy property if ($str[$i] > $str[$i + 1]) { // if string violates tidy // property then decrease // the value stored at that // index by 1 and replace all // the value stored right to // that index by 9 $x = ord($str[$i]); $x--; $str[$i] = chr($x); for ($j = $i + 1; $j < $len; $j++) $str[$j] = '9'; } } return $str; } // Driver code $str = '11333445538'; $len = strlen($str); // num will store // closest tidy number $num = tidyNum($str $len); echo $num; // This code is contributed by mits ?> JavaScript <script> // Javascript program to find closest // tidy number smaller than the // given number function tidyNum(str1 len) { var str = str1.split(''); for (i = len - 2; i >= 0; i--) { // Check whether string // violates tidy property if (str[i] > str[i + 1]) { // If string violates tidy // property then decrease the // value stored at that index // by 1 and replace all the value // stored right to that index by 9 str[i]--; for(j = i + 1; j < len; j++) str[j] = '9'; } } return str.join(''); } // Driver code var str = '11333445538'; var len = str.length; // num will store closest tidy number document.write(tidyNum(str len)); // This code is contributed by Amit Katiyar </script>
Produzione:
11333444999