📜  计算给定范围内单位数字k的数字

📅  最后修改于: 2021-05-04 13:40:01             🧑  作者: Mango

此处给出从低到高的范围并给定数字k。您必须找出与k相同数字的计数数
例子:

Input: low = 2, high = 35, k = 2   
Output: 4
Numbers are 2, 12, 22, 32  

Input: low = 3, high = 30, k = 3 
Output: 3
Numbers are  3, 13, 23

天真的方法是遍历给定范围内的所有数字,并检查每个数字的最后一位,如果最后一位等于k,则递增结果。

C++
// Simple CPP program to count numbers with
// last digit as k in given range.
#include 
using namespace std;
 
// Returns count of numbers with k as last
// digit.
int counLastDigitK(int low, int high, int k)
{
    int count = 0;
    for (int i = low; i <= high; i++)
        if (i % 10 == k)
            count++;  
    return count;
}
 
// Driver Program
int main()
{
    int low = 3, high = 35, k = 3;
    cout << counLastDigitK(low, high, k);
    return 0;
}


Java
// Simple Java program to count numbers with
// last digit as k in given range.
import java.util.*;
import java.lang.*;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                                int high, int k)
    {
        int count = 0;
        for (int i = low; i <= high; i++)
            if (i % 10 == k)
                count++;
        return count;
    }
     
    // driver function
    public static void main(String args[])
    {
        int low = 3, high = 35, k = 3;
        System.out.println(counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by Sagar Shukla


Python3
# Simple python program to count numbers with
# last digit as k in given range.
 
# Returns count of numbers with k as last
# digit.
def counLastDigitK(low, high, k):
    count = 0
    for i in range(low, high+1):
        if (i % 10 == k):
            count+=1
    return count
 
 
# Driver Program
low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// Simple C# program to count numbers with
// last digit as k in given range.
using System;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                                int high, int k)
    {
        int count = 0;
        for (int i = low; i <= high; i++)
            if (i % 10 == k)
                count++;
        return count;
    }
     
    // Driver function
    public static void Main()
    {
        int low = 3, high = 35, k = 3;
        Console.WriteLine(counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// Efficient CPP program to count numbers 
// with last digit as k in given range.
#include 
using namespace std;
 
// Returns count of numbers with k as last
// digit.
int countLastDigitK(long long low,
        long long high, long long K)
{
 
  long long mlow = 10 * ceil(low/10.0);
  long long mhigh = 10 * floor(high/10.0);
 
  int count = (mhigh - mlow)/10;
  if (high % 10 >= K)
    count++;
  if (low % 10 <=K && (low%10))
    count++;
 
  return count;
}
 
// Driver Code
int main()
{
    int low = 3, high = 35, k = 3;
    cout << countLastDigitK(low, high, k);
    return 0;
}


Java
// Efficient Java program to count numbers
// with last digit as k in given range.
import java.util.*;
import java.lang.*;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                             int high, int k)
    {
        int mlow = 10 * (int)
                      Math.ceil(low/10.0);
        int mhigh = 10 * (int)
                      Math.floor(high/10.0);
        int count = (mhigh - mlow)/10;
        if (high % 10 >= k)
            count++;
        if (low % 10 <= k && (low%10) > 0)
            count++;
        return count;
    }
     
    // driver function
    public static void main(String argc[])
    {
        int low = 3, high = 35, k = 3;
        System.out.println(counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by Sagar Shukla


Python3
import math
# Efficient python program to count numbers
# with last digit as k in given range.
 
# Returns count of numbers with k as last
# digit.
def counLastDigitK(low, high, k):
    mlow = 10 * math.ceil(low/10.0)
    mhigh = 10 * int(high/10.0)
     
    count = (mhigh - mlow)/10
    if (high % 10 >= k):
        count += 1
    if (low % 10 <= k and \
        (low%10) > 0):
        count += 1
    return int(count)
 
 
# Driver Code
low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// Efficient Java program to count numbers
// with last digit as k in given range.
using System;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                                int high, int k)
    {
        int mlow = 10 * Convert.ToInt32(
                  Math.Ceiling(low/10.0));
        int mhigh = 10 * Convert.ToInt32(
                  Math.Floor(high/10.0));
        int count = (mhigh - mlow) / 10;
        if (high % 10 >= k)
            count++;
        if (low % 10 <= k && (low%10) > 0)
            count++;
        return count;
    }
     
    // Driver function
    public static void Main()
    {
        int low = 3, high = 35, k = 3;
        Console.WriteLine(
          counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by vt_m


输出:

4

时间复杂度: O(高–低)一个有效的解决方案基于以下事实:每10个连续数字中的最后一位数字出现一次。

C++

// Efficient CPP program to count numbers 
// with last digit as k in given range.
#include 
using namespace std;
 
// Returns count of numbers with k as last
// digit.
int countLastDigitK(long long low,
        long long high, long long K)
{
 
  long long mlow = 10 * ceil(low/10.0);
  long long mhigh = 10 * floor(high/10.0);
 
  int count = (mhigh - mlow)/10;
  if (high % 10 >= K)
    count++;
  if (low % 10 <=K && (low%10))
    count++;
 
  return count;
}
 
// Driver Code
int main()
{
    int low = 3, high = 35, k = 3;
    cout << countLastDigitK(low, high, k);
    return 0;
}

Java

// Efficient Java program to count numbers
// with last digit as k in given range.
import java.util.*;
import java.lang.*;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                             int high, int k)
    {
        int mlow = 10 * (int)
                      Math.ceil(low/10.0);
        int mhigh = 10 * (int)
                      Math.floor(high/10.0);
        int count = (mhigh - mlow)/10;
        if (high % 10 >= k)
            count++;
        if (low % 10 <= k && (low%10) > 0)
            count++;
        return count;
    }
     
    // driver function
    public static void main(String argc[])
    {
        int low = 3, high = 35, k = 3;
        System.out.println(counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by Sagar Shukla

Python3

import math
# Efficient python program to count numbers
# with last digit as k in given range.
 
# Returns count of numbers with k as last
# digit.
def counLastDigitK(low, high, k):
    mlow = 10 * math.ceil(low/10.0)
    mhigh = 10 * int(high/10.0)
     
    count = (mhigh - mlow)/10
    if (high % 10 >= k):
        count += 1
    if (low % 10 <= k and \
        (low%10) > 0):
        count += 1
    return int(count)
 
 
# Driver Code
low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// Efficient Java program to count numbers
// with last digit as k in given range.
using System;
 
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low,
                                int high, int k)
    {
        int mlow = 10 * Convert.ToInt32(
                  Math.Ceiling(low/10.0));
        int mhigh = 10 * Convert.ToInt32(
                  Math.Floor(high/10.0));
        int count = (mhigh - mlow) / 10;
        if (high % 10 >= k)
            count++;
        if (low % 10 <= k && (low%10) > 0)
            count++;
        return count;
    }
     
    // Driver function
    public static void Main()
    {
        int low = 3, high = 35, k = 3;
        Console.WriteLine(
          counLastDigitK(low, high, k));
    }
}
 
// This code is contributed by vt_m
输出
4

时间复杂度: O(1)