给定数字n,请找到与n具有相同数字集且大于n的最小数字。如果n是用其数字集表示的最大可能数,则打印“不可能”。
例子:
为了简化实现,我们将输入数字视为字符串。
Input: n = "218765"
Output: "251678"
Input: n = "1234"
Output: "1243"
Input: n = "4321"
Output: "Not Possible"
Input: n = "534976"
Output: "536479"
以下是有关下一个更大数目的一些观察结果。
1)如果所有数字均按降序排列,则输出始终为“不可能”。例如4321。
2)如果所有数字都按升序排序,那么我们需要交换最后两位数字。例如1234。
3)在其他情况下,我们需要从最右边处理数字(为什么?因为我们需要找到所有较大数字中的最小数字)
您现在可以尝试自己开发算法。
以下是用于查找下一个更大数字的算法。
I)从最右边的数字开始遍历给定的数字,继续遍历直到找到一个比先前遍历的数字小的数字。例如,如果输入数字为“ 534976”,由于4小于下一个数字9,我们在4处停止。如果找不到这样的数字,则输出为“不可能”。
II)现在,在上面找到的数字“ d”的右侧搜索大于“ d”的最小数字。对于“ 53 4 976”, 4的右侧包含“ 976”。大于4的最小数字为6 。
III)交换上面找到的两位数,在上面的示例中我们得到53 6 97 4 。
IV)现在,对所有数字进行排序,从“ d”旁边的位置到数字的末尾。排序后得到的数字就是输出。对于上面的例子,我们的排序数字粗体536 974。我们得到“ 536 479 ”,这是输入534976的下一个更大的数字。
以下是上述方法的实现。
C++
// C++ program to find the smallest number which greater than a given number
// and has same set of digits as given number
#include
#include
#include
using namespace std;
// Utility function to swap two digits
void swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
// Given a number as a char array number[], this function finds the
// next greater number. It modifies the same array to store the result
void findNext(char number[], int n)
{
int i, j;
// I) Start from the right most digit and find the first digit that is
// smaller than the digit next to it.
for (i = n-1; i > 0; i--)
if (number[i] > number[i-1])
break;
// If no such digit is found, then all digits are in descending order
// means there cannot be a greater number with same set of digits
if (i==0)
{
cout << "Next number is not possible";
return;
}
// II) Find the smallest digit on right side of (i-1)'th digit that is
// greater than number[i-1]
int x = number[i-1], smallest = i;
for (j = i+1; j < n; j++)
if (number[j] > x && number[j] < number[smallest])
smallest = j;
// III) Swap the above found smallest digit with number[i-1]
swap(&number[smallest], &number[i-1]);
// IV) Sort the digits after (i-1) in ascending order
sort(number + i, number + n);
cout << "Next number with same set of digits is " << number;
return;
}
// Driver program to test above function
int main()
{
char digits[] = "534976";
int n = strlen(digits);
findNext(digits, n);
return 0;
}
Java
// Java program to find next greater
// number with same set of digits.
import java.util.Arrays;
public class nextGreater
{
// Utility function to swap two digit
static void swap(char ar[], int i, int j)
{
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
// Given a number as a char array number[],
// this function finds the next greater number.
// It modifies the same array to store the result
static void findNext(char ar[], int n)
{
int i;
// I) Start from the right most digit
// and find the first digit that is smaller
// than the digit next to it.
for (i = n - 1; i > 0; i--)
{
if (ar[i] > ar[i - 1]) {
break;
}
}
// If no such digit is found, then all
// digits are in descending order means
// there cannot be a greater number with
// same set of digits
if (i == 0)
{
System.out.println("Not possible");
}
else
{
int x = ar[i - 1], min = i;
// II) Find the smallest digit on right
// side of (i-1)'th digit that is greater
// than number[i-1]
for (int j = i + 1; j < n; j++)
{
if (ar[j] > x && ar[j] < ar[min])
{
min = j;
}
}
// III) Swap the above found smallest
// digit with number[i-1]
swap(ar, i - 1, min);
// IV) Sort the digits after (i-1)
// in ascending order
Arrays.sort(ar, i, n);
System.out.print("Next number with same" +
" set of digits is ");
for (i = 0; i < n; i++)
System.out.print(ar[i]);
}
}
public static void main(String[] args)
{
char digits[] = { '5','3','4','9','7','6' };
int n = digits.length;
findNext(digits, n);
}
}
Python
# Python program to find the smallest number which
# is greater than a given no. has same set of
# digits as given number
# Given number as int array, this function finds the
# greatest number and returns the number as integer
def findNext(number,n):
# Start from the right most digit and find the first
# digit that is smaller than the digit next to it
for i in range(n-1,0,-1):
if number[i] > number[i-1]:
break
# If no such digit found,then all numbers are in
# descending order, no greater number is possible
if i == 1 and number[i] <= number[i-1]:
print "Next number not possible"
return
# Find the smallest digit on the right side of
# (i-1)'th digit that is greater than number[i-1]
x = number[i-1]
smallest = i
for j in range(i+1,n):
if number[j] > x and number[j] < number[smallest]:
smallest = j
# Swapping the above found smallest digit with (i-1)'th
number[smallest],number[i-1] = number[i-1], number[smallest]
# X is the final number, in integer datatype
x = 0
# Converting list upto i-1 into number
for j in range(i):
x = x * 10 + number[j]
# Sort the digits after i-1 in ascending order
number = sorted(number[i:])
# converting the remaining sorted digits into number
for j in range(n-i):
x = x * 10 + number[j]
print "Next number with set of digits is",x
# Driver Program to test above function
digits = "534976"
# converting into integer array,
# number becomes [5,3,4,9,7,6]
number = map(int ,digits)
findNext(number, len(digits))
# This code is contributed by Harshit Agrawal
C#
// C# program to find next greater
// number with same set of digits.
using System;
public class nextGreater
{
// Utility function to swap two digit
static void swap(char []ar, int i, int j)
{
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
// Given a number as a char array number[],
// this function finds the next greater number.
// It modifies the same array to store the result
static void findNext(char []ar, int n)
{
int i;
// I) Start from the right most digit
// and find the first digit that is smaller
// than the digit next to it.
for (i = n - 1; i > 0; i--)
{
if (ar[i] > ar[i - 1])
{
break;
}
}
// If no such digit is found, then all
// digits are in descending order means
// there cannot be a greater number with
// same set of digits
if (i == 0)
{
Console.WriteLine("Not possible");
}
else
{
int x = ar[i - 1], min = i;
// II) Find the smallest digit on right
// side of (i-1)'th digit that is greater
// than number[i-1]
for (int j = i + 1; j < n; j++)
{
if (ar[j] > x && ar[j] < ar[min])
{
min = j;
}
}
// III) Swap the above found smallest
// digit with number[i-1]
swap(ar, i - 1, min);
// IV) Sort the digits after (i-1)
// in ascending order
Array.Sort(ar, i, n-i);
Console.Write("Next number with same" +
" set of digits is ");
for (i = 0; i < n; i++)
Console.Write(ar[i]);
}
}
// Driver code
public static void Main(String[] args)
{
char []digits = { '5','3','4','9','7','6' };
int n = digits.Length;
findNext(digits, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Next number with same set of digits is 536479