原文:

给定两个数字 nd 。任务是找出小于或等于 n 的最大数字,该数字包含最大数量的尾随 9,n 与该数字的差值不应大于 d举例:

input: n = 1029, d = 102
output: 999
1029 has 1 trailing nine while 999 has three 
trailing nine.also 1029-999 = 30(which is less than 102).
input: n = 10281, d = 1
output: 10281

一种天真的方法将是从 n 迭代到 n-d,并找到尾随 9 数量最多的数字。 一个有效的方法可以通过一些关键的观察发现。这个问题的一个关键观察是,小于 n 的最大数字是 ,其结尾至少是(k9)

[n – (n mod 10^k) – 1]

从 n 到 1 的位数总数开始遍历 k 的所有可能值,检查 d >是否为 n% 。如果没有得到这样的值,最终的答案将是 n 本身。否则,使用上面的观察来检查答案。 以下是上述办法的实施情况。

c

// cpp to implement above function
#include 
using namespace std;
// it's better to use long long
// to handle big integers
#define ll long long
// function to count no of digits
ll dig(ll a)
{
    ll count = 0;
    while (a > 0) {
        a /= 10;
        count  ;
    }
    return count;
}
// function to implement above approach
void required_number(ll num, ll n, ll d)
{
    ll i, j, power, a, flag = 0;
    for (i = num; i >= 1; i--) {
        power = pow(10, i);
        a = n % power;
        // if difference between power
        // and n doesn't exceed d
        if (d > a) {
            flag = 1;
            break;
        }
    }
    if (flag) {
        ll t = 0;
        // loop to build a number from the
        // appropriate no of digits containing only 9
        for (j = 0; j < i; j  ) {
            t  = 9 * pow(10, j);
        }
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            cout << n;
        else {
            // observation
            cout << n - (n % power) - 1;
        }
    }
    else
        cout << n;
}
// driver code
int main()
{
    ll n = 1029, d = 102;
    // variable that stores no of digits in n
    ll num = dig(n);
    required_number(num, n, d);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java code to implement above function
import java.io.*;
class gfg {
// it's better to use long
// to handle big integers
// function to count no. of digits
static long dig(long a)
{
    long count = 0;
    while (a > 0)
    {
        a /= 10;
        count  ;
    }
    return count;
}
// function to implement above approach
 static void required_number(long num, long n, long d)
{
    long i, j, power=1, a, flag = 0;
    for (i = num; i >= 1; i--)
    {
        power = (long)math.pow(10, i);
        a = n % power;
        // if difference between power
        // and n doesn't exceed d
        if (d > a)
        {
            flag = 1;
            break;
        }
    }
    if (flag>0)
    {
        long t = 0;
        // loop to build a number from the
        // appropriate no of digits containing
        // only 9
        for (j = 0; j < i; j  )
        {
            t  = 9 * math.pow(10, j);
        }
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            system.out.print( n);
        else {
            // observation
            system.out.print( n - (n % power) - 1);
        }
    }
    else
        system.out.print(n);
}
    // driver code
    public static void main (string[] args)
    {
        long n = 1029, d = 102;
        // variable that stores no
        // of digits in n
        long num = dig(n);
        required_number(num, n, d);
    }
}
// this code is contributed by chandan_jnu

python 3

# python3 to implement above function
# function to count no of digits
def dig(a):
    count = 0;
    while (a > 0):
        a /= 10
        count =1
    return count
# function to implement above approach
def required_number(num, n, d):
    flag = 0
    power=0
    a=0
    for i in range(num,0,-1):
        power = pow(10, i)
        a = n % power
        # if difference between power
        # and n doesn't exceed d
        if (d > a):
            flag = 1
            break
    if(flag):
        t=0
        # loop to build a number from the
        # appropriate no of digits containing only 9
        for j in range(0,i):
            t  = 9 * pow(10, j)
        # if the build number is
        # same as original number(n)
        if(n % power ==t):
            print(n,end="")
        else:
            # observation
            print((n - (n % power) - 1),end="")
    else:
        print(n,end="")
# driver code
if __name__ == "__main__":
    n = 1029
    d = 102
# variable that stores no of digits in n
    num = dig(n)
    required_number(num, n, d)
# this code is contributed by mits

c

// c# code to implement
// above function
using system;
class gfg
{
// it's better to use long
// to handle big integers
// function to count no. of digits
static long dig(long a)
{
    long count = 0;
    while (a > 0)
    {
        a /= 10;
        count  ;
    }
    return count;
}
// function to implement
// above approach
static void required_number(long num,
                            long n,
                            long d)
{
    long i, j, power = 1, a, flag = 0;
    for (i = num; i >= 1; i--)
    {
        power = (long)math.pow(10, i);
        a = n % power;
        // if difference between power
        // and n doesn't exceed d
        if (d > a)
        {
            flag = 1;
            break;
        }
    }
    if (flag > 0)
    {
        long t = 0;
        // loop to build a number
        // from the appropriate no
        // of digits containing only 9
        for (j = 0; j < i; j  )
        {
            t  = (long)(9 * math.pow(10, j));
        }
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            console.write( n);
        else
        {
            // observation
            console.write(n - (n % power) - 1);
        }
    }
    else
        console.write(n);
}
    // driver code
    public static void main()
    {
        long n = 1029, d = 102;
        // variable that stores
        // no. of digits in n
        long num = dig(n);
        required_number(num, n, d);
    }
}
// this code is contributed
// by chandan_jnu

服务器端编程语言(professional hypertext preprocessor 的缩写)

 0)
    {
        $a = (int)($a / 10);
        $count  ;
    }
    return $count;
}
// function to implement above approach
function required_number($num, $n, $d)
{
    $flag = 0;
    for ($i = $num; $i >= 1; $i--)
    {
        $power = pow(10, $i);
        $a = $n % $power;
        // if difference between power
        // and n doesn't exceed d
        if ($d > $a)
        {
            $flag = 1;
            break;
        }
    }
    if ($flag)
    {
        $t = 0;
        // loop to build a number from the
        // appropriate no of digits containing only 9
        for ($j = 0; $j < $i; $j  )
        {
            $t  = 9 * pow(10, $j);
        }
        // if the build number is
        // same as original number(n)
        if ($n % $power == $t)
            echo $n;
        else
        {
            // observation
            echo ($n - ($n % $power) - 1);
        }
    }
    else
        echo $n;
}
// driver code
$n = 1029;
$d = 102;
// variable that stores no of
// digits in n
$num = dig($n);
required_number($num, $n, $d);
// this code is contributed by mits
?>

java 描述语言


output: 

999

时间复杂度: o(位数)