计算所有排列大于该数的自然数
有一些自然数的所有排列都大于或等于该数,例如。 123,其所有排列(123、231、321)都大于或等于123。
给定一个自然数n ,任务是计算从 1 到 n 的所有这些数字。
例子:
Input : n = 15.
Output : 14
Explanation:
1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12,
13, 14, 15 are the numbers whose all
permutation is greater than the number
itself. So, output 14.
Input : n = 100.
Output : 54
一个简单的解决方案是运行一个从 1 到 n 的循环,并为每个数字检查其数字是否处于非递减顺序。
一个有效的解决方案基于以下观察。
观察 1:从 1 到 9,所有数字都有这个性质。因此,对于 n <= 9,输出 n。
观察 2:所有排列大于或等于该数的数的所有数字都按升序排列。
这个想法是将所有数字从 1 推到 9。现在,弹出顶部元素,说topel并尝试制作数字按升序排列的数字,第一个数字是topel 。要制作这样的数字,第二个数字可以从topel%10到 9。如果这个数字小于n ,则增加计数并将数字压入堆栈,否则忽略。
下面是这种方法的实现:
C++
// C++ program to count the number less than N,
// whose all permutation is greater
// than or equal to the number.
#include
using namespace std;
// Return the count of the number having all
// permutation greater than or equal to the number.
int countNumber(int n)
{
int result = 0;
// Pushing 1 to 9 because all number from 1
// to 9 have this property.
stack s;
for (int i = 1; i <= 9; i++)
{
if (i <= n)
{
s.push(i);
result++;
}
// take a number from stack and add
// a digit smaller than or equal to last digit
// of it.
while (!s.empty())
{
int tp = s.top();
s.pop();
for (int j = tp % 10; j <= 9; j++)
{
int x = tp * 10 + j;
if (x <= n)
{
s.push(x);
result++;
}
}
}
}
return result;
}
// Driven Code
int main()
{
int n = 15;
cout << countNumber(n) << endl;
return 0;
}
Java
// Java program to count the number less than N,
// whose all permutation is greater
// than or equal to the number.
import java.util.Stack;
class GFG
{
// Return the count of the number having all
// permutation greater than or equal to the number.
static int countNumber(int n)
{
int result = 0;
// Pushing 1 to 9 because all number from 1
// to 9 have this property.
Stack s = new Stack<>();
for (int i = 1; i <= 9; i++)
{
if (i <= n)
{
s.push(i);
result++;
}
// take a number from stack and add
// a digit smaller than or equal to last digit
// of it.
while (!s.empty())
{
int tp = s.pop();
for (int j = tp % 10; j <= 9; j++)
{
int x = tp * 10 + j;
if (x <= n) {
s.push(x);
result++;
}
}
}
}
return result;
}
// Driven Code
public static void main(String[] args)
{
int n = 15;
System.out.println(countNumber(n));
}
}
// this code contributed by Rajput-Ji
Python3
# Python3 program to count the number less
# than N, whose all permutation is greater
# than or equal to the number.
# Return the count of the number having
# all permutation greater than or equal
# to the number.
def countNumber(n):
result = 0
# Pushing 1 to 9 because all number
# from 1 to 9 have this property.
s = []
for i in range(1, 10):
if (i <= n):
s.append(i)
result += 1
# take a number from stack and add
# a digit smaller than or equal to last digit
# of it.
while len(s) != 0:
tp = s[-1]
s.pop()
for j in range(tp % 10, 10):
x = tp * 10 + j
if (x <= n):
s.append(x)
result += 1
return result
# Driver Code
if __name__ == '__main__':
n = 15
print(countNumber(n))
# This code is contributed by PranchalK
C#
// C# program to count the number less than N,
// whose all permutation is greater than
// or equal to the number.
using System;
using System.Collections.Generic;
class GFG {
// Return the count of the number
// having all permutation greater than
// or equal to the number.
static int countNumber(int n)
{
int result = 0;
// Pushing 1 to 9 because all number from 1
// to 9 have this property.
Stack s = new Stack();
for (int i = 1; i <= 9; i++)
{
if (i <= n)
{
s.Push(i);
result++;
}
// take a number from stack and add
// a digit smaller than or equal to last digit
// of it.
while (s.Count != 0)
{
int tp = s.Peek();
s.Pop();
for (int j = tp % 10; j <= 9; j++)
{
int x = tp * 10 + j;
if (x <= n) {
s.Push(x);
result++;
}
}
}
}
return result;
}
// Driver Code
public static void Main(String[] args)
{
int n = 15;
Console.WriteLine(countNumber(n));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出
14
时间复杂度: O(x),其中 x 是输出中打印的元素数。