📜  可以用M个字符打印的最大自然数

📅  最后修改于: 2021-04-29 17:59:35             🧑  作者: Mango

给定一个数字M,该数字表示要用于打印前N个自然数的最大字符数(忽略空格)。找出N的最大值。

例子:

Input : M = 5
Output : 5
We can type 1 2 3 4 5 using 5 key
presses.

Input : M = 15
Output : 12
We can type 1 2 3 4 5 6 7 8 9 10 
11 12 using 15 key presses.

观察到,对于小于11的M,我们可以打印1到9。因此,N将是9。现在,对于10到99之间的数字(总共90个数字),我们需要两个字符。对于100到999之间的数字(总共900个数字),我们需要三个字符。因此,请继续计算并从M中减去字符数。此外,当M小于键入的字符数时。查找可以使用允许的其余按键进行打印的偏移号。

以下是此方法的实现:

C/C++
// Maximum natural number that can be printed
// with M characters.
#include 
  
int printMaxN(int m)
{
    // At starting point, from 1 to 9, we
    // will have only one digit
    int total_numbers_within_range = 9;
    int number_of_digits = 1;
  
    // While the number of characters is
    // greater than the total number of
    // natural number in given range e.g.
    // if m = 12, then at first step, (m >
    // (9)*(1)) evaluates to true
    while (m > total_numbers_within_range * number_of_digits) {
  
        // Now here we have exhausted some
        // of the digits in making up some natural
        // numbers, we reduce the count of m
  
        m = m - (total_numbers_within_range * number_of_digits);
  
        // Increment the number of digits
        number_of_digits++;
  
        // Increase the range of the digits
        total_numbers_within_range *= 10;
    }
  
    // Gives the starting point of any range
    int ans = (total_numbers_within_range) / 9 - 1;
  
    // Add the add the remaining digits/(number of
    // digits required for current series)
    ans += (m / number_of_digits);
  
    return ans;
}
  
// Driver code
int main()
{
    int m = 15;
    printf("%dn", printMaxN(m));
    return 0;
}


Java
// Maximum natural number that
// can be printed with M characters.
import java.util.*;
  
class GFG {
  
    static int printMaxN(int m)
    {
  
        // At starting point, from 1 to 9, we
        // will have only one digit
        int total_numbers_within_range = 9;
        int number_of_digits = 1;
  
        // While the number of characters is
        // greater than the total number of
        // natural number in given range e.g.
        // if m = 12, then at first step, (m >
        // (9)*(1)) evaluates to true
        while (m > total_numbers_within_range * number_of_digits) {
  
            // Now here we have exhausted some
            // of the digits in making up some natural
            // numbers, we reduce the count of m
  
            m = m - (total_numbers_within_range * number_of_digits);
  
            // Increment the number of digits
            number_of_digits++;
  
            // Increase the range of the digits
            total_numbers_within_range *= 10;
        }
  
        // Gives the starting point of any range
        int ans = (total_numbers_within_range) / 9 - 1;
  
        // Add the add the remaining digits/(number of
        // digits required for current series)
        ans += (m / number_of_digits);
  
        return ans;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int m = 15;
        System.out.print(printMaxN(m));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Maximum natural number
# that can be printed
# with M characters.
  
def printMaxN(m):
  
    # At starting point, from 1 to 9, we 
    # will have only one digit
    total_numbers_within_range = 9
    number_of_digits = 1
    
    ''' While the number of characters is 
        greater than the total number of 
        natural number in given range e.g.''' 
       # if m = 12, then at first step, (m > 
      # (9)*(1)) evaluates to true
    while (m > total_numbers_within_range * number_of_digits):
   
        # Now here we have exhausted some 
        # of the digits in making up some natural 
        # 3 numbers, we reduce the count of m
    
        m = m - (total_numbers_within_range * number_of_digits)
    
        # Increment the number of digits
        number_of_digits = number_of_digits + 1
    
        # Increase the range of the digits
        total_numbers_within_range = total_numbers_within_range * 10
      
    
    # Gives the starting point of any range
    ans = (total_numbers_within_range) // 9 - 1
    
    # Add the add the remaining digits/(number of
    # digits required for current series)
    ans = ans +  (m // number_of_digits)
    
    return ans
  
# Driver code
  
m = 15 
print(printMaxN(m))
  
  
# This code is contributed
# by Anant Agarwal.


C#
// Maximum natural number that
// can be printed with M characters.
using System;
  
class GFG {
  
    static int printMaxN(int m)
    {
  
        // At starting point, from 1 to 9, we
        // will have only one digit
        int total_numbers_within_range = 9;
        int number_of_digits = 1;
  
        // While the number of characters is
        // greater than the total number of
        // natural number in given range e.g.
        // if m = 12, then at first step, (m >
        // (9)*(1)) evaluates to true
        while (m > total_numbers_within_range * number_of_digits) {
  
            // Now here we have exhausted some
            // of the digits in making up some natural
            // numbers, we reduce the count of m
  
            m = m - (total_numbers_within_range * number_of_digits);
  
            // Increment the number of digits
            number_of_digits++;
  
            // Increase the range of the digits
            total_numbers_within_range *= 10;
        }
  
        // Gives the starting point of any range
        int ans = (total_numbers_within_range) / 9 - 1;
  
        // Add the add the remaining digits/(number of
        // digits required for current series)
        ans += (m / number_of_digits);
  
        return ans;
    }
  
    // Driver code
    public static void Main()
    {
        int m = 15;
        Console.Write(printMaxN(m));
    }
}
  
// This code is contributed by vt_m.


PHP
 
    // (9)*(1)) evaluates to true
    while ($m > $total_numbers_within_range *
                          $number_of_digits) 
    {
  
        // Now here we have 
        // exhausted some of 
        // the digits in making
        // up some natural numbers,
        // we reduce the count of m
        $m = $m - ($total_numbers_within_range *
                             $number_of_digits);
  
        // Increment the number
        // of digits
        $number_of_digits++;
  
        // Increase the range 
        // of the digits
        $total_numbers_within_range *= 10;
    }
  
    // Gives the starting 
    // point of any range
    $ans = ($total_numbers_within_range)
                                / 9 - 1;
  
    // Add the add the remaining
    // digits/(number of digits 
    // required for current series)
    $ans += ($m / $number_of_digits);
  
    return $ans;
}
  
    // Driver Code
    $m = 15; 
    echo printMaxN($m);
// This code is contributed by ajit
?>


输出:

12

参考 :
https://practice.geeksforgeeks.org/problems/faulty-keyboard/0