📜  查找最接近和较小的整齐数

📅  最后修改于: 2021-05-04 09:09:44             🧑  作者: Mango

整理号码是数字以不降序排列的号码。在这里,我们得到一个数字,我们必须找到另一个更小但最接近给定数字的数字,并且该数字应整齐,即其数字应按非降序排列。

例子:

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

这个想法是从头到尾遍历。每当侵犯整洁的属性时,我们将数字减少一位,然后将所有后续数字设为9。

C++
// CPP 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


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 = [1,1,3,3,3,4,4,5,5,3,8] 
len = len(str) 
      
# num will store closest tidy number
num = tidyNum(str, len) 
  
for i in range(0,len):
    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
= 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
?>


输出:

11333444999

参考 :

在Google Code Jam 2017资格回合中询问此问题。