📜  查找第N个最小的整数,该整数可被100次K整除

📅  最后修改于: 2021-04-22 10:20:53             🧑  作者: Mango

给定两个数字N和K。任务是找到第N个最小的数字,该数字除以100,即正好K次。

例子:

方法:

  • 首先找到最小的数字,该数字可被100次精确整整K次。那是1之后的2 * K 0,因为100只有两个0。
  • 要找到第N个最小数字,请将N乘以2 * k 0后得到的前一个数字。
  • 考虑一种情况,当N被100整除时,就好像我们将N与前一个数字相乘,则新数字的尾数将大于(2 * k +1),即0,这意味着它将被K整除100倍。
  • 将该数字乘以(N + 1)。使用字符串,因为N和K可能非常大,不适合整数限制。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to find the Nth smallest number
string find_number(int N, int K)
{
    string r;
  
    // If N is divisible by 100 then we
    // multiply N + 1 otherwise, it will be
    // divisible by 100 more than K times
    if (N % 100 == 0) {
        N += 1;
  
        // convert integer to string
        r = to_string(N);
    }
  
    // if N is not divisible by 100
    else {
        // convert integer to string
        r = to_string(N);
    }
  
    // add 2*K 0's at the end to be divisible
    // by 100 exactly K times
    for (int i = 1; i <= K; i++)
        r += "00";
  
    return r;
}
  
// Driver Code
int main()
{
    int N = 1000, K = 2;
    string ans = find_number(N, K);
    cout << ans << "\n";
  
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
  
class GFG
{
  
// Function to find the Nth smallest number
static String find_number(int N, int K)
{
    String r;
  
    // If N is divisible by 100 then we
    // multiply N + 1 otherwise, it will be
    // divisible by 100 more than K times
    if (N % 100 == 0) 
    {
        N += 1;
  
        // convert integer to string
        r = String.valueOf(N);
    }
  
    // if N is not divisible by 100
    else 
    {
        // convert integer to string
        r = String.valueOf(N);
    }
  
    // add 2*K 0's at the end to be divisible
    // by 100 exactly K times
    for (int i = 1; i <= K; i++)
        r += "00";
  
    return r;
}
  
// Driver Code
public static void main(String[] args) 
{
    int N = 1000, K = 2;
    String ans = find_number(N, K);
    System.out.println(ans);
}
}
  
/* This code is contributed by PrinciRaj1992 */


Python3
# Python3 implementation of above approach
  
# Function to find the Nth smallest number
def find_number(N, K):
      
    r = ""
  
    # If N is divisible by 100 then we
    # multiply N + 1 otherwise, it will be
    # divisible by 100 more than K times
    if (N % 100 == 0):
        N += 1;
  
        # convert integer to string
        r = str(N)
  
    # if N is not divisible by 100
    else:
          
        # convert integer to string
        r = str(N)
  
    # add 2*K 0's at the end to be divisible
    # by 100 exactly K times
    for i in range(1, K + 1):
        r += "00"
  
    return r
  
# Driver Code
N = 1000
K = 2;
ans = find_number(N, K)
print(ans)
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to find the Nth smallest number
static String find_number(int N, int K)
{
    String r;
  
    // If N is divisible by 100 then we
    // multiply N + 1 otherwise, it will be
    // divisible by 100 more than K times
    if (N % 100 == 0) 
    {
        N += 1;
  
        // convert integer to string
        r = N.ToString();
    }
  
    // if N is not divisible by 100
    else
    {
        // convert integer to string
        r = N.ToString();
    }
  
    // add 2*K 0's at the end to be divisible
    // by 100 exactly K times
    for (int i = 1; i <= K; i++)
        r += "00";
  
    return r;
}
  
// Driver Code
public static void Main(String[] args) 
{
    int N = 1000, K = 2;
    String ans = find_number(N, K);
    Console.WriteLine(ans);
}
}
  
// This code is contributed by Rajput-Ji


输出:
10010000

时间复杂度: O(K)