给定一个数字N和一个整数K ,任务是找到大于或等于N的最小数字,该数字仅使用前K个非零数字(1、2,…,K-1,K)形成。
例子:
Input: N = 124, K = 3
Output: 131
Explanation:
The smallest number greater than or equal to 124, which is only made of digits 1, 2, 3 is 131.
Input: N = 325242, K = 4
Output: 331111
天真的方法:
最简单的解决方案是从N + 1开始for循环,并找到由前K个数字组成的第一个数字。
高效的解决方案:
- 为了获得一种有效的解决方案,我们需要理解一个事实,即最大9位数字的可形成最多至10 10。因此,我们将反向遍历数字的位数并检查:
- 如果当前数字> = K,则使该数字= 1 。
- 如果当前数字
并没有在这之后没有位数大于K,然后递增1的数字上复制所有剩下的数字,因为它是。
- 一旦我们遍历了所有数字并且仍然找不到小于K的任何数字,那么我们需要在答案中添加一个数字(1)。
下面是上述方法的实现:
C++
// C++ Program to find the smallest
// number greater than or equal
// to N which is made up of
// first K digits
#include
using namespace std;
// Function to count the
// digits greater than K
int CountGreater(int n, int k)
{
int a = 0;
while (n) {
if ((n % 10) > k) {
a++;
}
n = n / 10;
}
return a;
}
// Function to print the list
int PrintList(list ans)
{
for (auto it = ans.begin();
it != ans.end(); it++)
cout << *it;
}
// Function to find the number
// greater than or equal to n,
// which is only made of first
// k digits
void getNumber(int n, int k)
{
int count = CountGreater(n, k);
// If the number itself
// satisfy the conditions
if (count == 0) {
cout << n;
return;
}
list ans;
bool changed = false;
// Check digit from back
while (n > 0) {
int digit = n % 10;
if (changed == true) {
ans.push_front(digit);
}
else {
// If digit > K is
// present previously and
// current digit is less
// than K
if (count == 0 && digit < k) {
ans.push_front(digit + 1);
changed = true;
}
else {
ans.push_front(1);
// If current digit is
// greater than K
if (digit > k) {
count--;
}
}
}
n = n / 10;
}
// If an extra digit needs
// to be added
if (changed == false) {
ans.push_front(1);
}
// Print the number
PrintList(ans);
return;
}
// Driver Code
int main()
{
int N = 51234;
int K = 4;
getNumber(N, K);
return 0;
}
Java
// Java program to find the smallest
// number greater than or equal
// to N which is made up of
// first K digits
import java.util.*;
class GFG{
// Function to count the
// digits greater than K
static int CountGreater(int n, int k)
{
int a = 0;
while (n > 0)
{
if ((n % 10) > k)
{
a++;
}
n = n / 10;
}
return a;
}
// Function to print the list
static void PrintList(List ans)
{
for(int it : ans)
System.out.print(it);
}
// Function to find the number
// greater than or equal to n,
// which is only made of first
// k digits
static void getNumber(int n, int k)
{
int count = CountGreater(n, k);
// If the number itself
// satisfy the conditions
if (count == 0)
{
System.out.print(n);
return;
}
List ans = new LinkedList<>();
boolean changed = false;
// Check digit from back
while (n > 0)
{
int digit = n % 10;
if (changed == true)
{
ans.add(0, digit);
}
else
{
// If digit > K is
// present previously and
// current digit is less
// than K
if (count == 0 && digit < k)
{
ans.add(0, digit + 1);
changed = true;
}
else
{
ans.add(0, 1);
// If current digit is
// greater than K
if (digit > k)
{
count--;
}
}
}
n = n / 10;
}
// If an extra digit needs
// to be added
if (changed == false)
{
ans.add(0, 1);
}
// Print the number
PrintList(ans);
return;
}
// Driver Code
public static void main(String[] args)
{
int N = 51234;
int K = 4;
getNumber(N, K);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to find the smallest
# number greater than or equal
# to N which is made up of
# first K digits
# Function to count the
# digits greater than K
def CountGreater(n, k):
a = 0
while (n > 0):
if ((n % 10) > k):
a += 1
n = n // 10
return a
# Function to print the list
def PrintList (ans):
for i in ans:
print(i, end = '')
# Function to find the number
# greater than or equal to n,
# which is only made of first
# k digits
def getNumber(n, k):
count = CountGreater(n, k)
# If the number itself
# satisfy the conditions
if (count == 0):
print(n)
return
ans = []
changed = False
# Check digit from back
while (n > 0):
digit = n % 10
if (changed == True):
ans.insert(0, digit)
else:
# If digit > K is
# present previously and
# current digit is less
# than K
if (count == 0 and digit < k):
ans.insert(0, digit + 1)
changed = True
else:
ans.insert(0, 1)
# If current digit is
# greater than K
if (digit > k):
count -= 1
n = n // 10
# If an extra digit needs
# to be added
if (changed == False):
ans.insert(0, 1)
# Print the number
PrintList(ans)
return
# Driver Code
N = 51234
K = 4
getNumber(N, K)
# This code is contributed by himanshu77
C#
// C# program to find the smallest
// number greater than or equal
// to N which is made up of
// first K digits
using System;
using System.Collections.Generic;
class GFG{
// Function to count the
// digits greater than K
static int CountGreater(int n, int k)
{
int a = 0;
while (n > 0)
{
if ((n % 10) > k)
{
a++;
}
n = n / 10;
}
return a;
}
// Function to print the list
static void PrintList(List ans)
{
foreach(int it in ans)
Console.Write(it);
}
// Function to find the number
// greater than or equal to n,
// which is only made of first
// k digits
static void getNumber(int n, int k)
{
int count = CountGreater(n, k);
// If the number itself
// satisfy the conditions
if (count == 0)
{
Console.Write(n);
return;
}
List ans = new List();
bool changed = false;
// Check digit from back
while (n > 0)
{
int digit = n % 10;
if (changed == true)
{
ans.Insert(0, digit);
}
else
{
// If digit > K is
// present previously and
// current digit is less
// than K
if (count == 0 && digit < k)
{
ans.Insert(0, digit + 1);
changed = true;
}
else
{
ans.Insert(0, 1);
// If current digit is
// greater than K
if (digit > k)
{
count--;
}
}
}
n = n / 10;
}
// If an extra digit needs
// to be added
if (changed == false)
{
ans.Insert(0, 1);
}
// Print the number
PrintList(ans);
return;
}
// Driver Code
public static void Main(String[] args)
{
int N = 51234;
int K = 4;
getNumber(N, K);
}
}
// This code is contributed by Princi Singh
输出:
111111