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

📅  最后修改于: 2021-10-26 06:25:23             🧑  作者: Mango

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

例子:

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

  • 初始化一个变量来存储总计数,比如total_count
  • (N / 10)添加到总数中。
  • 使用以下公式计算x以存储小于N的 10 的最大倍数:
  • 现在,计算xN之间以M结尾的数字的计数。
  • 将此计数添加到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


Javascript


输出:
10

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