📜  大于 X 的最小数是 K 周期

📅  最后修改于: 2021-10-26 06:28:23             🧑  作者: Mango

给定一个由N位数字组成的字符串整数X和一个整数K ,任务是找到大于或等于X的最小整数,它是K周期(K <= N)

例子:

方法:
为了解决这个问题,我们的想法是让X i = X ik对所有i > K 。然后按照以下步骤解决问题:

  • 检查X 是否大于或等于初始整数。如果是,则打印当前字符串作为答案。
  • 否则,从右向左遍历,找到第一个不等于9 的数字。然后将该数字加1,并用名为pos的变量标记该位置。
  • 然后将pos之后的所有数字设置为第K数字为0 。再次使X i = X ik对所有i > K

下面是上述方法的实现:

C++
// C++ Program to find the
// smallest K periodic
// integer greater than X
#include 
using namespace std;
 
// Function to find the
// smallest K periodic
// integer greater than X
string Kperiodicinteger(string X, int N,
                        int K)
{
    // Stores the number
    // in a temporary string
    string temp = X;
 
    // Set X[i]=X[i-k] for
    // i>k
    for (int i = 0; i < K; i++)
 
    {
        // Start from
        // the current
        // index
        int j = i;
 
        // Loop upto N
        // change
        // X[j] to X[i]
        while (j < N) {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return X if current
    // Value is greater
    // than original value
    if (X >= temp) {
 
        return X;
    }
 
    int POS;
 
    // Find the first
    // digit not equal to 9
    for (int i = K - 1; i >= 0; i--) {
        if (X[i] != '9') {
             
        // Increment X[i]
            X[i]++;
 
            // Set POS to
            // current index
            POS = i;
 
            break;
        }
    }
 
    // Change X[i] to 0
    // for all indices
    // from POS+1 to K
    for (int i = POS + 1; i < K; i++) {
        X[i] = '0';
    }
 
    // Set X[i]=X[i-k] for
    // i>k
    for (int i = 0; i < K; i++)
 
    {
        int j = i;
        // Loop upto N
        // change
        // X[j] to X[i]
 
        while (j < N) {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return the
    // final string
    return X;
}
 
// Driver Code
int main()
{
 
    int N = 4, K = 2;
 
    string X = "1215";
 
    cout << Kperiodicinteger(X, N, K);
 
    return 0;
}


Java
// Java program to find the smallest
// K periodic integer greater than X
import java.util.*;
 
class GFG{
 
// Function to find the
// smallest K periodic
// integer greater than X
static String Kperiodicinteger(char []X,
                            int N, int K)
{
     
    // Stores the number
    // in a temporary String
    String temp = String.valueOf(X);
 
    // Set X[i]=X[i-k] for
    // i>k
    for(int i = 0; i < K; i++)
    {
         
        // Start from the current
        // index
        int j = i;
 
        // Loop upto N change
        // X[j] to X[i]
        while (j < N)
        {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return X if current
    // Value is greater
    // than original value
    if (String.valueOf(X).compareTo(temp) >= 0)
    {
        return String.valueOf(X);
    }
 
    int POS = 0;
 
    // Find the first
    // digit not equal to 9
    for(int i = K - 1; i >= 0; i--)
    {
        if (X[i] != '9')
        {
             
            // Increment X[i]
            X[i]++;
 
            // Set POS to
            // current index
            POS = i;
            break;
        }
    }
 
    // Change X[i] to 0
    // for all indices
    // from POS+1 to K
    for(int i = POS + 1; i < K; i++)
    {
        X[i] = '0';
    }
 
    // Set X[i]=X[i-k] for
    // i>k
    for(int i = 0; i < K; i++)
    {
        int j = i;
         
        // Loop upto N change
        // X[j] to X[i]
        while (j < N)
        {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return the
    // final String
    return String.valueOf(X);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4, K = 2;
    String X = "1215";
 
    System.out.print(Kperiodicinteger(
            X.toCharArray(), N, K));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to find the smallest
# K periodic integer greater than X
 
# Function to find the 
# smallest K periodic 
# integer greater than X
def Kperiodicinteger(X, N, K):
     
    X = list(X)
     
    # Stores the number 
    # in a temporary string 
    temp = X.copy()
 
    # Set X[i]=X[i-k] for 
    # i>k
    for i in range(K):
         
        # Start from the current 
        # index 
        j = i
 
        # Loop upto N change 
        # X[j] to X[i]
        while (j < N):
            X[j] = X[i]
            j += K
 
    # Return X if current 
    # Value is greater 
    # than original value 
    if (X >= temp):
        return X
 
    POS = 0
 
    # Find the first digit
    # not equal to 9
    for i in range(K - 1, -1, -1):
        if (X[i] != '9'):
             
            # Increment X[i] 
            X[i] = str(int(X[i]) + 1)
 
            # Set POS to 
            # current index
            POS = i
            break
 
    # Change X[i] to 0 
    # for all indices 
    # from POS+1 to K 
    for i in range(POS + 1, K):
        X[i] = '0'
 
    # Set X[i]=X[i-k] for 
    # i>k
    for i in range(K):
        j = i
 
        # Loop upto N 
        # change 
        # X[j] to X[i]
        while (j < N):
            X[j] = X[i]
            j += K
 
    # Return the 
    # final string 
    return X
 
# Driver Code
N = 4
K = 2
X = "1215"
 
print(*Kperiodicinteger(X, N, K), sep = '')
         
# This code is contributed by avanitrachhadiya2155


C#
// C# program to find the smallest
// K periodic integer greater than X
using System;
 
class GFG{
 
// Function to find the
// smallest K periodic
// integer greater than X
static String Kperiodicinteger(char[] X,
                               int N, int K)
{
 
    // Stores the number
    // in a temporary String
    String temp = String.Join("", X);
 
    // Set X[i]=X[i-k] for
    // i>k
    for(int i = 0; i < K; i++)
    {
 
        // Start from the current
        // index
        int j = i;
 
        // Loop upto N change
        // X[j] to X[i]
        while (j < N)
        {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return X if current
    // Value is greater
    // than original value
    if (String.Join("", X).CompareTo(temp) >= 0)
    {
        return String.Join("", X);
    }
 
    int POS = 0;
 
    // Find the first
    // digit not equal to 9
    for(int i = K - 1; i >= 0; i--)
    {
        if (X[i] != '9')
        {
 
            // Increment X[i]
            X[i]++;
 
            // Set POS to
            // current index
            POS = i;
            break;
        }
    }
 
    // Change X[i] to 0
    // for all indices
    // from POS+1 to K
    for(int i = POS + 1; i < K; i++)
    {
        X[i] = '0';
    }
 
    // Set X[i]=X[i-k] for
    // i>k
    for(int i = 0; i < K; i++)
    {
        int j = i;
 
        // Loop upto N change
        // X[j] to X[i]
        while (j < N)
        {
            X[j] = X[i];
            j += K;
        }
    }
 
    // Return the
    // readonly String
    return String.Join("", X);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 4, K = 2;
    String X = "1215";
 
    Console.Write(Kperiodicinteger(
          X.ToCharArray(), N, K));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
1313

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程