📜  大于n的最小整数,使得它由数m精确地构成k次

📅  最后修改于: 2021-04-22 01:47:07             🧑  作者: Mango

给定三个整数nmk ,任务是找到最小的整数> n ,以使数字m恰好出现在其中k次。
例子:

方法:n + 1开始迭代,对于每个整数,检查它是否完全由m个数字组成k次。这样就可以找到最小整数> n,其中m恰好出现了k次。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if n contains
// digit m exactly k times
bool digitWell(int n, int m, int k)
{
    int cnt = 0;
    while (n > 0) {
        if (n % 10 == m)
            ++cnt;
        n /= 10;
    }
    return cnt == k;
}
 
// Function to return the smallest integer > n
// with digit m occurring exactly k times
int findInt(int n, int m, int k)
{
 
    int i = n + 1;
 
    while (true) {
        if (digitWell(i, m, k))
            return i;
        i++;
    }
}
 
// Driver code
int main()
{
    int n = 111, m = 2, k = 2;
    cout << findInt(n, m, k);
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function that returns true if n contains
// digit m exactly k times
static boolean digitWell(int n, int m, int k)
{
    int cnt = 0;
    while (n > 0)
    {
        if (n % 10 == m)
            ++cnt;
        n /= 10;
    }
    return cnt == k;
}
 
// Function to return the smallest integer > n
// with digit m occurring exactly k times
static int findInt(int n, int m, int k)
{
 
    int i = n + 1;
 
    while (true)
    {
        if (digitWell(i, m, k))
            return i;
        i++;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 111, m = 2, k = 2;
    System.out.println(findInt(n, m, k));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
 
# Function that returns true if n
# contains digit m exactly k times
def digitWell(n, m, k):
 
    cnt = 0
    while (n > 0):
 
        if (n % 10 == m):
            cnt = cnt + 1;
        n = (int)(n / 10);
 
    return cnt == k;
 
# Function to return the smallest integer > n
# with digit m occurring exactly k times
def findInt(n, m, k):
 
    i = n + 1;
 
    while (True):
        if (digitWell(i, m, k)):
            return i;
        i = i + 1;
 
# Driver code
n = 111; m = 2; k = 2;
print(findInt(n, m, k));
 
# This code is contributed
# by Akanksha Rai


C#
// C# implementation of the approach
using System;
class GFG
{
     
// Function that returns true if n contains
// digit m exactly k times
static bool digitWell(int n, int m, int k)
{
    int cnt = 0;
    while (n > 0)
    {
        if (n % 10 == m)
            ++cnt;
        n /= 10;
    }
    return cnt == k;
}
 
// Function to return the smallest integer > n
// with digit m occurring exactly k times
static int findInt(int n, int m, int k)
{
 
    int i = n + 1;
 
    while (true)
    {
        if (digitWell(i, m, k))
            return i;
        i++;
    }
}
 
// Driver code
public static void Main()
{
    int n = 111, m = 2, k = 2;
    Console.WriteLine(findInt(n, m, k));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
 0)
    {
        if ($n % 10 == $m)
            ++$cnt;
        $n = floor($n / 10);
    }
    return $cnt == $k;
}
 
// Function to return the smallest integer > n
// with digit m occurring exactly k times
function findInt($n, $m, $k)
{
    $i = $n + 1;
 
    while (true)
    {
        if (digitWell($i, $m, $k))
            return $i;
        $i++;
    }
}
 
// Driver code
$n = 111;
$m = 2;
$k = 2;
 
echo findInt($n, $m, $k);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
122