📌  相关文章
📜  计算所有数字,直到N,最后一位是M

📅  最后修改于: 2021-04-29 02:19:24             🧑  作者: Mango

给定两个正数MN ,任务是从范围[1,N]中查找所有以M为最后一位的所有数字的计数。

例子:

天真的方法:最简单的方法是在1N的范围内进行迭代,并检查最后一位是否等于M。如果发现为真,则增加计数。最后,打印获得的计数。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下事实:以每个数字结尾的数字的计数将一直相同,直到10的最大倍数小于N (例如x )为止。因此,其计数为(N / 10)。现在,简化任务以计算以M结尾的在xN之间的数字的计数
步骤如下:

  • 初始化一个变量以存储总数,例如total_count
  • (N / 10)添加到总数中。
  • 使用以下公式计算x以存储最大10的倍数,该最大倍数小于N
  • 现在,计算以M结尾的介于xN之间的数字的数量。
  • 将此计数添加到total_count中。打印最终值 获得的total_count

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
  
#include 
using namespace std;
  
// Function to count the numbers
// ending with M
int getCount(int N, int M)
{
  
    // Stores the count of
    // numbers required
    int total_count = 0;
  
    // Calculate count upto
    // nearest power of 10
    total_count += (N / 10);
  
    // Computing the value of x
    int x = (N / 10) * 10;
  
    // Adding the count of numbers
    // ending at M from x to N
    if ((N - x) >= M) 
    {
        total_count = total_count + 1;
    }
    return total_count;
}
  
// Driver Code
int main()
{
    int N = 100, M = 1;
  
    // Function Call
    cout << getCount(N, M);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to count the numbers
// ending with M
static int getCount(int N, int M)
{
  
    // Stores the count of
    // numbers required
    int total_count = 0;
  
    // Calculate count upto
    // nearest power of 10
    total_count += (N / 10);
  
    // Computing the value of x
    int x = (N / 10) * 10;
  
    // Adding the count of numbers
    // ending at M from x to N
    if ((N - x) >= M) 
    {
        total_count = total_count + 1;
    }
    return total_count;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 100, M = 1;
  
    // Function call
    System.out.print(getCount(N, M));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 Program to implement
# the above approach
  
# Function to count the numbers
# ending with M 
def getCount(N, M):
  
    # Stores the count of
    # numbers required
    total_count = 0
  
    # Calculate count upto
    # nearest power of 10
    total_count += N // 10
  
    # Computing the value of x
    x = (N // 10) * 10
  
    # Adding the count of numbers
    # ending at M from x to N
    if((N - x) >= M):
        total_count = total_count + 1
  
    return total_count
  
# Driver Code
N = 100
M = 1
  
# Function call
print(getCount(N, M))
  
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
  
    // Function to count the 
    // numbers ending with M
    static int getCount(int N, int M)
    {
  
        // Stores the count of
        // numbers required
        int total_count = 0;
  
        // Calculate count upto
        // nearest power of 10
        total_count += (N / 10);
  
        // Computing the value of x
        int x = (N / 10) * 10;
  
        // Adding the count of numbers
        // ending at M from x to N
        if ((N - x) >= M) 
        {
            total_count = total_count + 1;
        }
        return total_count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 100, M = 1;
  
        // Function call
        Console.Write(getCount(N, M));
    }
}
  
// This code is contributed by shikhasingrajput


输出:
10



时间复杂度: O(1)
辅助空间: O(1)