📌  相关文章
📜  可以在 K 步中减少到 0 的最大数 N

📅  最后修改于: 2021-09-16 10:58:16             🧑  作者: Mango

给定一个整数N 。执行以下任务:

  • 编号已注明。
  • 选自N前导数字选自N减去所得到的值存储回N.
  • 再次记录N的新值。
  • 这个过程一直持续到 N 变为 0。最后,最后记下0

例如,取N = 13 。在将 13 减少到 0 的过程中记下的数字将是:

  • 13
  • 13 – 1 = 12
  • 12 – 1 = 11
  • 11 – 1 = 10
  • 10 – 1 = 9
  • 9 – 9 = 0

给定一个整数K ,它是在将数字N减少到 0的过程中记下的数字总数,任务是找到起始数字N的最大可能值。

例子:

方法:想法是观察 N 的最大可能值将始终小于 10*K。因此,想法是在 K 到 10*K 的范围内应用二分搜索,并寻找可以在 K 步中减少到 0 的最大数。
要找到最大可能的数字:

  1. 初始化为k初始化为k*10
  2. mid初始化为(left + right) / 2
  3. 获取记下的数字的计数以将mid转换为 0并将其存储在len 中
  4. 遵循分而治之的方法:虽然len不等于k
    • 将 mid 更新为当前(left + right) / 2
    • mid开始时获取计数。
    • 如果计数大于中旬中旬更新更大。
    • 否则,更新leftmid
  5. 现在, mid有一个值,这将导致写下k 个整数。
  6. 要找到最大的这样的数字:
    • 虽然计数等于k
    • 如果当前计数不等于采用mid+1得到的计数,则中断
    • 否则,继续将mid增加 1。
  7. 如果写下k 个整数, mid现在是最大可能的整数。

下面是上述方法的实现:

C++
// C++ program to implement above approach
 
#include 
using namespace std;
 
// Utility function to return the first
// digit of a number.
int firstDigit(int n)
{
    // Remove last digit from number
    // till only one digit is left
    while (n >= 10) {
        n /= 10;
    }
 
    // return the first digit
    return n;
}
 
// Utility function that returns the count of numbers
// written down when starting from n
int getCount(int n)
{
    int count = 1;
 
    while (n != 0) {
        int leadDigit = firstDigit(n);
        n -= leadDigit;
        count++;
    }
    return count;
}
 
// Function to find the largest number N which
// can be reduced to 0 in K steps
int getLargestNumber(int k)
{
    int left = k;
    int right = k * 10;
    int mid = (left + right) / 2;
 
    // Get the sequence length of the mid point
    int len = getCount(mid);
 
    // Until k sequence length is reached
    while (len != k) {
 
        // Update mid point
        mid = (left + right) / 2;
 
        // Get count of the new mid point
        len = getCount(mid);
 
        if (len > k) {
 
            // Update right to mid
            right = mid;
        }
        else {
 
            // Update left to mid
            left = mid;
        }
    }
 
    // Increment mid point by one while count
    // is equal to k to get the maximum value
    // of mid point
    while (len == k) {
 
        if (len != getCount(mid + 1)) {
            break;
        }
 
        mid++;
    }
 
    return (mid);
}
 
// Driver Code
int main()
{
    int k = 3;
 
    cout << getLargestNumber(k);
 
    return 0;
}


Java
// Java program to implement above approach
import java.io.*;
 
class GFG
{
     
// Utility function to return the first
// digit of a number.
static int firstDigit(int n)
{
    // Remove last digit from number
    // till only one digit is left
    while (n >= 10)
    {
        n /= 10;
    }
 
    // return the first digit
    return n;
}
 
// Utility function that returns the count of numbers
// written down when starting from n
static int getCount(int n)
{
    int count = 1;
 
    while (n != 0)
    {
        int leadDigit = firstDigit(n);
        n -= leadDigit;
        count++;
    }
    return count;
}
 
// Function to find the largest number N which
// can be reduced to 0 in K steps
static int getLargestNumber(int k)
{
    int left = k;
    int right = k * 10;
    int mid = (left + right) / 2;
 
    // Get the sequence length of the mid point
    int len = getCount(mid);
 
    // Until k sequence length is reached
    while (len != k)
    {
 
        // Update mid point
        mid = (left + right) / 2;
 
        // Get count of the new mid point
        len = getCount(mid);
 
        if (len > k)
        {
 
            // Update right to mid
            right = mid;
        }
        else
        {
 
            // Update left to mid
            left = mid;
        }
    }
 
    // Increment mid point by one while count
    // is equal to k to get the maximum value
    // of mid point
    while (len == k)
    {
 
        if (len != getCount(mid + 1))
        {
            break;
        }
 
        mid++;
    }
 
    return (mid);
}
 
    // Driver Code
    public static void main (String[] args)
    {
 
        int k = 3;
        System.out.println (getLargestNumber(k));
    }
}
 
// This code is contributed by jit_t


Python3
# Python3 program to implement above approach
 
# Utility function to return the first
# digit of a number.
def firstDigit(n) :
 
    # Remove last digit from number
    # till only one digit is left
    while (n >= 10) :
        n //= 10;
     
    # return the first digit
    return n;
 
# Utility function that returns
# the count of numbers written
# down when starting from n
def getCount(n) :
     
    count = 1;
 
    while (n != 0) :
        leadDigit = firstDigit(n);
        n -= leadDigit;
        count += 1;
     
    return count;
 
# Function to find the largest number N
# which can be reduced to 0 in K steps
def getLargestNumber(k) :
 
    left = k;
    right = k * 10;
    mid = (left + right) // 2;
 
    # Get the sequence length of the mid point
    length = getCount(mid);
 
    # Until k sequence length is reached
    while (length != k) :
 
        # Update mid point
        mid = (left + right) // 2;
 
        # Get count of the new mid point
        length = getCount(mid);
 
        if (length > k) :
 
            # Update right to mid
            right = mid;
         
        else :
 
            # Update left to mid
            left = mid;
         
    # Increment mid point by one while count
    # is equal to k to get the maximum value
    # of mid point
    while (length == k) :
 
        if (length != getCount(mid + 1)) :
            break;
         
        mid += 1;
     
    return mid;
 
# Driver Code
if __name__ == "__main__" :
 
    k = 3;
 
    print(getLargestNumber(k));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Utility function to return the first
// digit of a number.
static int firstDigit(int n)
{
    // Remove last digit from number
    // till only one digit is left
    while (n >= 10)
    {
        n /= 10;
    }
 
    // return the first digit
    return n;
}
 
// Utility function that returns the count of numbers
// written down when starting from n
static int getCount(int n)
{
    int count = 1;
 
    while (n != 0)
    {
        int leadDigit = firstDigit(n);
        n -= leadDigit;
        count++;
    }
    return count;
}
 
// Function to find the largest number N which
// can be reduced to 0 in K steps
static int getLargestNumber(int k)
{
    int left = k;
    int right = k * 10;
    int mid = (left + right) / 2;
 
    // Get the sequence length of the mid point
    int len = getCount(mid);
 
    // Until k sequence length is reached
    while (len != k)
    {
 
        // Update mid point
        mid = (left + right) / 2;
 
        // Get count of the new mid point
        len = getCount(mid);
 
        if (len > k)
        {
 
            // Update right to mid
            right = mid;
        }
        else
        {
 
            // Update left to mid
            left = mid;
        }
    }
 
    // Increment mid point by one while count
    // is equal to k to get the maximum value
    // of mid point
    while (len == k)
    {
 
        if (len != getCount(mid + 1))
        {
            break;
        }
 
        mid++;
    }
 
    return (mid);
}
 
// Driver Code
public static void Main(String []args)
{
    int k = 3;
 
    Console.WriteLine( getLargestNumber(k));
}
}
 
// This code is contributed by Arnab Kundu


PHP
= 10)
    {
        $n = (int)($n / 10);
    }
 
    // return the first digit
    return $n;
}
 
// Utility function that returns the
// count of numbers written down when
// starting from n
function getCount($n)
{
    $count = 1;
 
    while ($n != 0)
    {
        $leadDigit = firstDigit($n);
        $n -= $leadDigit;
        $count++;
    }
    return $count;
}
 
// Function to find the largest number N
// which can be reduced to 0 in K steps
function getLargestNumber($k)
{
    $left = $k;
    $right = $k * 10;
    $mid = (int)(($left + $right) / 2);
 
    // Get the sequence length
    // of the mid point
    $len = getCount($mid);
 
    // Until k sequence length is reached
    while ($len != $k)
    {
 
        // Update mid point
        $mid = (int)(($left + $right) / 2);
 
        // Get count of the new mid point
        $len = getCount($mid);
 
        if ($len > $k)
        {
 
            // Update right to mid
            $right = $mid;
        }
        else
        {
 
            // Update left to mid
            $left = $mid;
        }
    }
 
    // Increment mid point by one while count
    // is equal to k to get the maximum value
    // of mid point
    while ($len == $k)
    {
        if ($len != getCount($mid + 1))
        {
            break;
        }
 
        $mid++;
    }
 
    return ($mid);
}
 
// Driver Code
$k = 3;
echo(getLargestNumber($k));
 
// This code is contributed by Code_Mech.
?>


Javascript


输出:
10