📌  相关文章
📜  最大数N,可以K步减少为0

📅  最后修改于: 2021-04-24 19:01:11             🧑  作者: 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 (如上所述)在将数字N减少为0的过程中记下的总数,任务是找到起始数字N的最大可能值。

例子:

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

要找到最大可能的数字:

  1. 初始化kK * 10。
  2. 初始化(左+右)/ 2
  3. 获取记下的数字的计数,以将转换为0并将其存储在len中
  4. 遵循分而治之的方法:当len不等于k时
    • 将中点更新为当前(左+右)/ 2
    • mid开始时获取计数。
    • 如果计数大于中点,请从右更新。
    • 否则,请从左更新。
  5. 现在, mid具有一个值,这将导致写下k个整数。
  6. 要找到最大的此类数字:
    • 计数等于k时
    • 如果当前计数不等于取mid + 1所得的计数,则中断
    • 否则,不断递增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.
?>


输出:
10