📜  大于X且K周期最小的最小数字

📅  最后修改于: 2021-04-22 02:58:27             🧑  作者: Mango

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

例子:

方法:
为了解决该问题,想法是使所有i> K的X i = X ik 。然后按照以下步骤解决问题:

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

下面是上述方法的实现:

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


输出:
1313

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