📌  相关文章
📜  在给定范围内所有数字相同的数字计数

📅  最后修改于: 2021-04-29 11:12:50             🧑  作者: Mango

给定两个整数LR表示一个范围的开始和结束值,任务是计算该范围内所有数字相同的所有数字,例如1、22、444、3333等。

例子:

天真的方法:遍历从L到R的所有数字,对于每个数字,检查其所有数字是否相同。如果是,则增加所需的计数。最后打印此计数。

高效的方法:这个想法基于以下事实:1、11、111等的倍数(1到9)的所有数字都相同。
例如:

1 times 1 = 1 (All digits are same)
2 times 1 = 2 (All digits are same)
3 times 1 = 3 (All digits are same)
.
.
9 times 1 = 9 (All digits are same)

Similarly
1 times 11 = 11 (All digits are same)
2 times 11 = 22 (All digits are same)
3 times 11 = 33 (All digits are same)
.
.
9 times 11 = 99 (All digits are same)

Same is the case for 111, 1111, etc.

因此,这些步骤可以定义为:

  1. 找到R中的位数。这将确定要创建的连续1的长度,直到我们必须检查该长度。
    例如,如果R = 100,则length(R)=3。因此,我们只需要检查1、11和111的倍数。
  2. 对于从1到length(R)的每个连续1s长度:
    • 将它们与29的所有值相乘
    • 检查它是否在[L,R]范围内。
    • 如果是,则增加所需数字的数量。
  3. 打印所需的数字计数。

      下面的代码是上述方法的实现:

      C++
      // C++ program to count the
      // total numbers in the range
      // L and R which have all the
      // digit same
        
      #include 
      using namespace std;
        
      // Function that count the
      // total numbersProgram between L
      // and R which have all the
      // digit same
      int count_same_digit(int L, int R)
      {
          int tmp = 0, ans = 0;
        
          // length of R
          int n = log10(R) + 1;
        
          for (int i = 0; i < n; i++) {
        
              // tmp has all digits as 1
              tmp = tmp * 10 + 1;
        
              // For each multiple
              // of tmp in range 1 to 9,
              // check if it present
              // in range [L, R]
              for (int j = 1; j <= 9; j++) {
        
                  if (L <= (tmp * j)
                      && (tmp * j) <= R) {
        
                      // Increment the required count
                      ans++;
                  }
              }
          }
          return ans;
      }
        
      // Driver Program
      int main()
      {
          int L = 12, R = 68;
        
          cout << count_same_digit(L, R)
               << endl;
          return 0;
      }


      Java
      // Java program to count the
      // total numbers in the range
      // L and R which have all the
      // digit same
      import java.util.*;
        
      class GFG{
        
      // Function that count the total 
      // numbersProgram between L and 
      // R which have all the digit same
      static int count_same_digit(int L, int R)
      {
          int tmp = 0, ans = 0;
        
          // Length of R
          int n = (int)Math.log10(R) + 1;
        
          for(int i = 0; i < n; i++)
          {
               
             // tmp has all digits as 1
             tmp = tmp * 10 + 1;
               
             // For each multiple of tmp 
             // in range 1 to 9, check if
             // it present in range [L, R]
             for(int j = 1; j <= 9; j++)
             {
                if (L <= (tmp * j) && (tmp * j) <= R) 
                {
                    // Increment the required count
                    ans++;
                }
             }
          }
          return ans;
      }
        
      // Driver code
      public static void main(String[] args)
      {
          int L = 12, R = 68;
        
          System.out.println(count_same_digit(L, R));
      }
      }
        
      // This code is contributed by offbeat


      Python3
      # Python3 program to count the
      # total numbers in the range
      # L and R which have all the
      # digit same
      import math
        
      # Function that count the
      # total numbersProgram between L
      # and R which have all the
      # digit same
      def count_same_digit(L, R):
        
          tmp = 0; ans = 0;
        
          # length of R
          n = int(math.log10(R) + 1);
        
          for i in range(0, n):
        
              # tmp has all digits as 1
              tmp = tmp * 10 + 1;
        
              # For each multiple
              # of tmp in range 1 to 9,
              # check if it present
              # in range [L, R]
              for j in range(1, 9):
        
                  if (L <= (tmp * j) and (tmp * j) <= R):
        
                      # Increment the required count
                      ans += 1;
                    
          return ans;
        
      # Driver Code
      L = 12; R = 68;
        
      print(count_same_digit(L, R))
        
      # This code is contributed by Nidhi_biet


      C#
      // C# program to count the
      // total numbers in the range
      // L and R which have all the
      // digit same
      using System;
        
      class GFG{
        
      // Function that count the total 
      // numbersProgram between L and 
      // R which have all the digit same
      static int count_same_digit(int L, int R)
      {
          int tmp = 0, ans = 0;
        
          // Length of R
          int n = (int)Math.Log10(R) + 1;
        
          for(int i = 0; i < n; i++)
          {
                
              // tmp has all digits as 1
              tmp = tmp * 10 + 1;
                    
              // For each multiple of tmp 
              // in range 1 to 9, check if
              // it present in range [L, R]
              for(int j = 1; j <= 9; j++)
              {
                  if (L <= (tmp * j) && (tmp * j) <= R) 
                  {
                      // Increment the required count
                      ans++;
                  }
              }
          }
          return ans;
      }
        
      // Driver code
      public static void Main()
      {
          int L = 12, R = 68;
        
          Console.Write(count_same_digit(L, R));
      }
      }
        
      // This code is contributed by Code_Mech


      输出:
      5
      

      时间复杂度: O(length(R))