给定数字n,请找到最大数小于或等于n的数字,并以非递减顺序显示数字。
例子:
Input : n = 200
Output : 199
If the given number is 200, the largest
number which is smaller or equal to it
having digits in non decreasing order is
199.
Input : n = 139
Output : 139
方法1(蛮力)
从n开始,对于每个数字,请检查其数字是否以非降序排列。如果是,则返回。否则,请检查下一个数字,直到找到结果。
C++
/* C++ program for brute force approach to find
largest number having digits in non-decreasing
order. */
#include
using namespace std;
// Returns the required number
long long nondecdigits(long long n)
{
/* loop to recursively check the numbers less
than or equal to given number*/
long long int x = 0;
for (x=n; x>=1; x--)
{
int no = x;
int prev_dig = 11;
// Keep traversing digits from
// right to left. For every digit
// check if it is smaller than prev_dig
bool flag = true;
while (no != 0)
{
if (prev_dig < no%10)
{
flag = false;
break;
}
prev_dig = no % 10;
no /= 10;
}
// We found the required number
if (flag == true)
break;
}
return x;
}
// Driver program
int main()
{
long long n = 200;
cout << nondecdigits(n);
return 0;
}
Java
// Java program for brute force
// approach to find largest number
// having digits in non-decreasing
// order.
import java.io.*;
class GFG
{
// Returns the required number
static int nondecdigits(int n)
{
// loop to recursively check
// the numbers less than or
// equal to given number
int x = 0;
for (x = n; x >= 1; x--)
{
int no = x;
int prev_dig = 11;
// Keep traversing digits
// from right to left. For
// every digit check if it
// is smaller than prev_dig
boolean flag = true;
while (no != 0)
{
if (prev_dig < no % 10)
{
flag = false;
break;
}
prev_dig = no % 10;
no /= 10;
}
// We found the
// required number
if (flag == true)
break;
}
return x;
}
// Driver Code
public static void main (String[] args)
{
int n = 200;
System.out.println (nondecdigits(n));
}
}
// This code is contributed by ajit
Python3
# Python 3 program for brute force approach
# to find largest number having digits in
# non-decreasing order.
# Returns the required number
def nondecdigits( n):
''' loop to recursively check the numbers
less than or equal to given number'''
x = 0
for x in range(n, 0, -1):
no = x
prev_dig = 11
# Keep traversing digits from
# right to left. For every digit
# check if it is smaller than prev_dig
flag = True
while (no != 0):
if (prev_dig < no % 10):
flag = False
break
prev_dig = no % 10
no //= 10
# We found the required number
if (flag == True):
break
return x
# Driver Code
if __name__=="__main__":
n = 200
print(nondecdigits(n))
# This code is contributed by ita_c
C#
// C# program for brute
// force approach to find
// largest number having
// digits in non-decreasing
// order.
using System;
class GFG
{
// Returns the required number
static int nondecdigits(int n)
{
// loop to recursively
// check the numbers less
// than or equal to given
// number
int x = 0;
for (x = n; x >= 1; x--)
{
int no = x;
int prev_dig = 11;
// Keep traversing digits
// from right to left. For
// every digit check if it
// is smaller than prev_dig
bool flag = true;
while (no != 0)
{
if (prev_dig < no % 10)
{
flag = false;
break;
}
prev_dig = no % 10;
no /= 10;
}
// We found the
// required number
if (flag == true)
break;
}
return x;
}
// Driver Code
static public void Main ()
{
int n = 200;
Console.WriteLine(nondecdigits(n));
}
}
// This code is contributed
// by akt_mit
PHP
= 1; $x--)
{
$no = $x;
$prev_dig = 11;
// Keep traversing
// digits from
// right to left.
// For every digit
// check if it is
// smaller than prev_dig
$flag = true;
while ($no != 0)
{
if ($prev_dig < $no%10)
{
$flag = false;
break;
}
$prev_dig = $no % 10;
$no /= 10;
}
// We found the
// required number
if ($flag == true)
break;
}
return $x;
}
// Driver Code
$n = 200;
echo nondecdigits($n);
// This code is contributed by ajt
?>
Javascript
C++
/* C++ program for efficient approach to find
largest number having digits in non-decreasing
order. */
#include
using namespace std;
// Prints the largest number smaller than s and
// digits in non-decreasing order.
void nondecdigits(string s)
{
long long m = s.size();
/* array to store digits of number */
long long a[m];
/* conversion of characters of string int number */
for (long long i=0; i0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i-1])
{
a[i-1]--;
level=i-1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (long long i=0; i<=level; i++)
cout << a[i];
for (long long i=level+1; i
Java
/* Java program for efficient approach to find
largest number having digits in non-decreasing
order. */
import java.util.*;
class GFG
{
// Prints the largest number smaller than s and
// digits in non-decreasing order.
static void nondecdigits(String s)
{
int m = s.length();
/* array to store digits of number */
int[] a = new int[m + 1];
/* conversion of characters of string int number */
for (int i = 0; i < m; i++)
a[i] = (int)s.charAt(i) - (int)'0';
/* variable holds the value of index after which
all digits are set 9 */
int level = m - 1;
for (int i = m - 1; i > 0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (int i = 0; i <= level; i++)
System.out.print(a[i]);
for (int i = level + 1; i < m; i++)
System.out.print("9");
}
else
{
for (int i = 1; i < level; i++)
System.out.print(a[i]);
for (int i = level + 1; i < m; i++)
System.out.print("9");
}
}
// Driver code
public static void main(String[] args)
{
String n = "200";
nondecdigits(n);
}
}
// This code is contributed by chandan_jnu
Python3
# Python3 program for efficient approach to
# find largest number having digits in
# non-decreasing order.
# Prints the largest number smaller than s
# and digits in non-decreasing order.
def nondecdigits(s):
m = len(s);
# array to store digits of number
a = [0] * m;
# conversion of characters of string
# int number
for i in range(m):
a[i] = ord(s[i]) - ord('0');
# variable holds the value of index
# after which all digits are set 9
level = m - 1;
for i in range(m - 1, 0, -1):
# Checking the condition if the digit
# is less than its left digit
if (a[i] < a[i - 1]):
a[i - 1] -= 1;
level = i - 1;
# If first digit is 0 no need to print it */
if (a[0] != 0):
for i in range(level + 1):
print(a[i], end = "");
for i in range(level + 1, m):
print("9", end = "");
else:
for i in range(1, level):
print(a[i], end = "");
for i in range(level + 1, m):
print("9", end = "");
# Driver Code
n = "200";
nondecdigits(n);
# This code is contributed by mits
C#
/* C# program for efficient approach to find
largest number having digits in non-decreasing
order. */
using System;
class GFG
{
// Prints the largest number smaller than s and
// digits in non-decreasing order.
static void nondecdigits(string s)
{
int m = s.Length;
/* array to store digits of number */
int[] a = new int[m + 1];
/* conversion of characters of string int number */
for (int i = 0; i < m; i++)
a[i] = (int)s[i] - (int)'0';
/* variable holds the value of index after which
all digits are set 9 */
int level = m - 1;
for (int i = m - 1; i > 0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (int i = 0; i <= level; i++)
Console.Write(a[i]);
for (int i = level + 1; i < m; i++)
Console.Write("9");
}
else
{
for (int i = 1; i < level; i++)
Console.Write(a[i]);
for (int i = level + 1; i < m; i++)
Console.Write("9");
}
}
// Driver code
static void Main()
{
string n = "200";
nondecdigits(n);
}
}
// This code is contributed by chandan_jnu
PHP
0; $i--)
{
/* Checking the condition
if the digit is less than
its left digit */
if ($a[$i] < $a[$i - 1])
{
$a[$i - 1]--;
$level = $i - 1;
}
}
/* If first digit is 0
no need to print it */
if ($a[0] != 0)
{
for ($i = 0;
$i <= $level; $i++)
echo $a[$i];
for ($i = $level + 1;
$i < $m; $i++)
echo "9";
}
else
{
for ($i = 1; $i < $level; $i++)
echo $a[$i];
for ($i = $level + 1;
$i < $m; $i++)
echo "9";
}
}
// Driver Code
$n = "200";
nondecdigits($n);
// This code is contributed
// by ajit
?>
输出:
199
高效的方法
上面讨论的方法效率不高,因为只会给出最大为10 ^ 5的数字的结果,但是如果数字很大,以至于它包含10 ^ 5的数字,则该方法不会有效。
因此,我们将讨论另一种处理此类数字的方法。
步骤1:将数字的数字存储在数组或向量中。
步骤2:在给定编号中,从最右边的数字到最左边的数字开始遍历数组。
步骤3:如果某个数字大于其右侧的数字,请记下该数字在该数组中的索引,并将该数字减1。
步骤4:不断更新该索引,直到按照步骤3中所述完全遍历数组为止。
步骤4:将所有数字正确地设置为该索引为9。
步骤5:打印数组,因为这是必需的数字。
假设数字为200,则数字将为2、0、0。最左位数大于右位数的索引为索引1(跟随1索引),因此索引1处的数字为2-1 = 1,并且右边的所有数字都是9。因此最终数组将是1、9、9。所需的数字将是199。
C++
/* C++ program for efficient approach to find
largest number having digits in non-decreasing
order. */
#include
using namespace std;
// Prints the largest number smaller than s and
// digits in non-decreasing order.
void nondecdigits(string s)
{
long long m = s.size();
/* array to store digits of number */
long long a[m];
/* conversion of characters of string int number */
for (long long i=0; i0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i-1])
{
a[i-1]--;
level=i-1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (long long i=0; i<=level; i++)
cout << a[i];
for (long long i=level+1; i
Java
/* Java program for efficient approach to find
largest number having digits in non-decreasing
order. */
import java.util.*;
class GFG
{
// Prints the largest number smaller than s and
// digits in non-decreasing order.
static void nondecdigits(String s)
{
int m = s.length();
/* array to store digits of number */
int[] a = new int[m + 1];
/* conversion of characters of string int number */
for (int i = 0; i < m; i++)
a[i] = (int)s.charAt(i) - (int)'0';
/* variable holds the value of index after which
all digits are set 9 */
int level = m - 1;
for (int i = m - 1; i > 0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (int i = 0; i <= level; i++)
System.out.print(a[i]);
for (int i = level + 1; i < m; i++)
System.out.print("9");
}
else
{
for (int i = 1; i < level; i++)
System.out.print(a[i]);
for (int i = level + 1; i < m; i++)
System.out.print("9");
}
}
// Driver code
public static void main(String[] args)
{
String n = "200";
nondecdigits(n);
}
}
// This code is contributed by chandan_jnu
Python3
# Python3 program for efficient approach to
# find largest number having digits in
# non-decreasing order.
# Prints the largest number smaller than s
# and digits in non-decreasing order.
def nondecdigits(s):
m = len(s);
# array to store digits of number
a = [0] * m;
# conversion of characters of string
# int number
for i in range(m):
a[i] = ord(s[i]) - ord('0');
# variable holds the value of index
# after which all digits are set 9
level = m - 1;
for i in range(m - 1, 0, -1):
# Checking the condition if the digit
# is less than its left digit
if (a[i] < a[i - 1]):
a[i - 1] -= 1;
level = i - 1;
# If first digit is 0 no need to print it */
if (a[0] != 0):
for i in range(level + 1):
print(a[i], end = "");
for i in range(level + 1, m):
print("9", end = "");
else:
for i in range(1, level):
print(a[i], end = "");
for i in range(level + 1, m):
print("9", end = "");
# Driver Code
n = "200";
nondecdigits(n);
# This code is contributed by mits
C#
/* C# program for efficient approach to find
largest number having digits in non-decreasing
order. */
using System;
class GFG
{
// Prints the largest number smaller than s and
// digits in non-decreasing order.
static void nondecdigits(string s)
{
int m = s.Length;
/* array to store digits of number */
int[] a = new int[m + 1];
/* conversion of characters of string int number */
for (int i = 0; i < m; i++)
a[i] = (int)s[i] - (int)'0';
/* variable holds the value of index after which
all digits are set 9 */
int level = m - 1;
for (int i = m - 1; i > 0; i--)
{
/* Checking the condition if the digit is
less than its left digit */
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
/* If first digit is 0 no need to print it */
if (a[0] != 0)
{
for (int i = 0; i <= level; i++)
Console.Write(a[i]);
for (int i = level + 1; i < m; i++)
Console.Write("9");
}
else
{
for (int i = 1; i < level; i++)
Console.Write(a[i]);
for (int i = level + 1; i < m; i++)
Console.Write("9");
}
}
// Driver code
static void Main()
{
string n = "200";
nondecdigits(n);
}
}
// This code is contributed by chandan_jnu
的PHP
0; $i--)
{
/* Checking the condition
if the digit is less than
its left digit */
if ($a[$i] < $a[$i - 1])
{
$a[$i - 1]--;
$level = $i - 1;
}
}
/* If first digit is 0
no need to print it */
if ($a[0] != 0)
{
for ($i = 0;
$i <= $level; $i++)
echo $a[$i];
for ($i = $level + 1;
$i < $m; $i++)
echo "9";
}
else
{
for ($i = 1; $i < $level; $i++)
echo $a[$i];
for ($i = $level + 1;
$i < $m; $i++)
echo "9";
}
}
// Driver Code
$n = "200";
nondecdigits($n);
// This code is contributed
// by ajit
?>
输出:
199
时间复杂度时间复杂度为O(d),其中d为否。数字中的位数。