📜  计算从1到n的总位数

📅  最后修改于: 2021-05-07 05:25:52             🧑  作者: Mango

给定数字n,计算将1到n的所有数字都写入所需要的总位数。

例子:

Input : 13
Output : 17
Numbers from 1 to 13 are 1, 2, 3, 4, 5, 
6, 7, 8, 9, 10, 11, 12, 13.
So 1 - 9 require 9 digits and 10 - 13 require 8
digits. Hence 9 + 8 = 17 digits are required. 

Input : 4
Output : 4
Numbers are 1, 2, 3, 4 . Hence 4 digits are required.

天真的递归方法–
解决上述问题的幼稚方法是计算每个数字从1到n的长度,然后计算每个数字的长度之和。递归实现是–

C++
#include 
using namespace std;
 
int findDigits(int n)
{
    if (n == 1)
    {
        return 1;
    }
     
    // Changing number to String
    string s = to_string(n);
     
    // Add length of number to  total_sum
    return s.length() + findDigits(n - 1);
}
 
// Driver code  
int main()
{
    int n = 13;
     
    cout << findDigits(n) << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
public class Main {
 
    static int findDigits(int n)
    {
        if (n == 1) {
            return 1;
        }
        // Changing number to String
        String s = String.valueOf(n);
         
        // add length of number to  total_sum
        return s.length() + findDigits(n - 1);
    }
    public static void main(String[] args)
    {
        int n = 13;
        System.out.println(findDigits(n));
    }
}


Python3
def findDigits(N):
 
    if N == 1:
        return 1
 
    # Changing number to string
    s = str(N)
 
    # Add length of number to total_sum
    return len(s) + findDigits(N - 1)
 
# Driver Code
 
# Given N
N = 13
 
# Function call
print(findDigits(N))
 
# This code is contributed by vishu2908


C#
using System;
using System.Collections;
class GFG{
   
static int findDigits(int n)
{
  if (n == 1)
  {
    return 1;
  }
 
  // Changing number to String
  string s = n.ToString();
 
  // add length of number to  total_sum
  return s.Length + findDigits(n - 1);
}
   
// Driver Code
public static void Main(string[] args)
{
  int n = 13;
  Console.Write(findDigits(n));
}
}
 
// This code is contributed by rutvik_56


C++
// C++ program to count total number
// of digits we have to write
// from 1 to n
#include 
using namespace std;
 
int totalDigits(int n)
{
   
    // number_of_digits store total
    // digits we have to write
    int number_of_digits = 0;
 
    // In the loop we are decreasing
    // 0, 9, 99 ... from n till
    // ( n - i + 1 ) is greater than 0
    // and sum them to number_of_digits
    // to get the required sum
    for(int i = 1; i <= n; i *= 10)
        number_of_digits += (n - i + 1);
 
    return number_of_digits;
}
 
// Driver code
int main()
{
    int n = 13;
   
    cout << totalDigits(n) << endl;
   
    return 0;
}


Java
// Java program to count total number of digits
// we have to write from 1 to n
 
public class GFG {
    static int totalDigits(int n)
    {
        // number_of_digits store total
        // digits we have to write
        int number_of_digits = 0;
 
        // In the loop we are decreasing
        // 0, 9, 99 ... from n till
        // ( n - i + 1 ) is greater than 0
        // and sum them to number_of_digits
        // to get the required sum
        for (int i = 1; i <= n; i *= 10)
            number_of_digits += (n - i + 1);
 
        return number_of_digits;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
        int n = 13;
        System.out.println(totalDigits(n));
    }
}


Python3
# Python3 program to count total number
# of digits we have to write from 1 to n
 
def totalDigits(n):
 
    # number_of_digits store total
    # digits we have to write
    number_of_digits = 0;
 
    # In the loop we are decreasing
    # 0, 9, 99 ... from n till
    #( n - i + 1 ) is greater than 0
    # and sum them to number_of_digits
    # to get the required sum
    for i in range(1, n, 10):
        number_of_digits = (number_of_digits +
                                 (n - i + 1));
         
    return number_of_digits;
 
 
# Driver code
n = 13;
s = totalDigits(n) + 1;
print(s);
     
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to count total number of
// digits we have to write from 1 to n
using System;
 
public class GFG {
 
    static int totalDigits(int n)
    {
 
        // number_of_digits store total
        // digits we have to write
        int number_of_digits = 0;
 
        // In the loop we are decreasing
        // 0, 9, 99 ... from n till
        // ( n - i + 1 ) is greater than 0
        // and sum them to number_of_digits
        // to get the required sum
        for (int i = 1; i <= n; i *= 10)
            number_of_digits += (n - i + 1);
 
        return number_of_digits;
    }
 
    // Driver Method
    public static void Main()
    {
        int n = 13;
 
        Console.WriteLine(totalDigits(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

17

迭代方法–(优化)
要计算位数,我们必须计算写成1,数十,百分数…所需的总位数。号码的地方。考虑n = 13,所以在一个地方的数字是1、2、3、4、5、6、7、8、9、0、1、2、3,在一个地方的数字是1、1、1、1。因此,从1到13的总位数基本上是13(13 – 0),而十位数是4(13 – 9)。再以n = 234为例,单位位置的数字是1(24次),2(24次),3(24次),4(24次),5(23次),6(23次), 7(23次),8(23次),9(23次),0(23次)因此23 * 6 + 24 * 4 = 234。十位数字是234 – 9 = 225,因为从1到234只有1 – 9是一位数字。最后,由于只有1-99是两位数字,所以位数是234-99 = 135。因此,我们必须写入的总位数为234(234 – 1 + 1)+ 225(234 – 10 + 1)+ 135(234 – 100 + 1)= 594。因此,基本上,我们必须从n减少0、9、99、999…以获得位数,十位数,百分之一,千分之一…的位数,并将其求和以获得所需的结果

以下是此方法的实现。

C++

// C++ program to count total number
// of digits we have to write
// from 1 to n
#include 
using namespace std;
 
int totalDigits(int n)
{
   
    // number_of_digits store total
    // digits we have to write
    int number_of_digits = 0;
 
    // In the loop we are decreasing
    // 0, 9, 99 ... from n till
    // ( n - i + 1 ) is greater than 0
    // and sum them to number_of_digits
    // to get the required sum
    for(int i = 1; i <= n; i *= 10)
        number_of_digits += (n - i + 1);
 
    return number_of_digits;
}
 
// Driver code
int main()
{
    int n = 13;
   
    cout << totalDigits(n) << endl;
   
    return 0;
}

Java

// Java program to count total number of digits
// we have to write from 1 to n
 
public class GFG {
    static int totalDigits(int n)
    {
        // number_of_digits store total
        // digits we have to write
        int number_of_digits = 0;
 
        // In the loop we are decreasing
        // 0, 9, 99 ... from n till
        // ( n - i + 1 ) is greater than 0
        // and sum them to number_of_digits
        // to get the required sum
        for (int i = 1; i <= n; i *= 10)
            number_of_digits += (n - i + 1);
 
        return number_of_digits;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
        int n = 13;
        System.out.println(totalDigits(n));
    }
}

Python3

# Python3 program to count total number
# of digits we have to write from 1 to n
 
def totalDigits(n):
 
    # number_of_digits store total
    # digits we have to write
    number_of_digits = 0;
 
    # In the loop we are decreasing
    # 0, 9, 99 ... from n till
    #( n - i + 1 ) is greater than 0
    # and sum them to number_of_digits
    # to get the required sum
    for i in range(1, n, 10):
        number_of_digits = (number_of_digits +
                                 (n - i + 1));
         
    return number_of_digits;
 
 
# Driver code
n = 13;
s = totalDigits(n) + 1;
print(s);
     
# This code is contributed
# by Shivi_Aggarwal

C#

// C# program to count total number of
// digits we have to write from 1 to n
using System;
 
public class GFG {
 
    static int totalDigits(int n)
    {
 
        // number_of_digits store total
        // digits we have to write
        int number_of_digits = 0;
 
        // In the loop we are decreasing
        // 0, 9, 99 ... from n till
        // ( n - i + 1 ) is greater than 0
        // and sum them to number_of_digits
        // to get the required sum
        for (int i = 1; i <= n; i *= 10)
            number_of_digits += (n - i + 1);
 
        return number_of_digits;
    }
 
    // Driver Method
    public static void Main()
    {
        int n = 13;
 
        Console.WriteLine(totalDigits(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

17

时间复杂度: O(登录)