给定范围内最后 K 位数字相等的整数个数
给定一个从L到R的范围和一个整数K ,任务是计算给定范围内的整数个数,使它们的最后K个数字相等。
例子:
Input: L = 49, R = 101, K=2
Output: 6
Explanation: There are 6 possible integers t.e., 55, 66, 77, 88, 99 and 100 such that their last K(i.e., 2) digits are equal.
Input: L = 10, R = 20, K=2
Output: 1
有效方法:可以观察到,在1到X范围内,最后K位等于整数z (即 i % 10 K = z)的整数i的计数为(X – z)/10 K + 1 .使用此观察,可以使用以下步骤解决上述问题:
- 假设intCount(X, K)表示从1到X的最后K位数相等的整数的计数。
- 要计算intCount(X, K) ,请遍历所有具有K位数字的z的可能性(即{00…0, 11…1, 22…2, 33…3, 44…4, …., 99…9 } )在公式 (X – z)/10 K +1 中并保持它们的总和是所需的值。
- 因此, L到R范围内的整数计数可以通过intCount(R, K) – intCount(L-1, K)获得。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// Function to return the count of
// integers from 1 to X having the
// last K digits as equal
int intCount(int X, int K)
{
// Stores the total count of integers
int ans = 0;
// Loop to iterate over all
// possible values of z
for (int z = 0; z < pow(10, K);
z += (pow(10, K) - 1) / 9) {
// Terminate the loop when z > X
if (z > X)
break;
// Add count of integers with
// last K digits equal to z
ans += ((X - z) / pow(10, K) + 1);
}
// Return count
return ans;
}
// Function to return the count of
// integers from L to R having the
// last K digits as equal
int intCountInRange(int L, int R, int K)
{
return (intCount(R, K)
- intCount(L - 1, K));
}
// Driver Code
int main()
{
int L = 49;
int R = 101;
int K = 2;
// Function Call
cout << intCountInRange(L, R, K);
return 0;
}
Java
// Java Program of the above approach
import java.util.*;
class GFG{
// Function to return the count of
// integers from 1 to X having the
// last K digits as equal
static int intCount(int X, int K)
{
// Stores the total count of integers
int ans = 0;
// Loop to iterate over all
// possible values of z
for (int z = 0; z < Math.pow(10, K);
z += (Math.pow(10, K) - 1) / 9) {
// Terminate the loop when z > X
if (z > X)
break;
// Add count of integers with
// last K digits equal to z
ans += ((X - z) / Math.pow(10, K) + 1);
}
// Return count
return ans;
}
// Function to return the count of
// integers from L to R having the
// last K digits as equal
static int intCountInRange(int L, int R, int K)
{
return (intCount(R, K)
- intCount(L - 1, K));
}
// Driver Code
public static void main(String[] args)
{
int L = 49;
int R = 101;
int K = 2;
// Function Call
System.out.print(intCountInRange(L, R, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to return the count of
# integers from 1 to X having the
# last K digits as equal
def intCount(X, K):
# Stores the total count of integers
ans = 0
# Loop to iterate over all
# possible values of z
for z in range(0, int(pow(10, K)),
int((pow(10, K) - 1) / 9)):
# Terminate the loop when z > X
if (z > X):
break
# Add count of integers with
# last K digits equal to z
ans += int((X - z) / int(pow(10, K)) + 1)
# Return count
return ans
# Function to return the count of
# integers from L to R having the
# last K digits as equal
def intCountInRange(L, R, K):
return(intCount(R, K) - intCount(L - 1, K))
# Driver Code
L = 49
R = 101
K = 2
# Function Call
print(intCountInRange(L, R, K))
# This code is contributed by sanjoy_62
C#
// C# Program of the above approach
using System;
class GFG{
// Function to return the count of
// integers from 1 to X having the
// last K digits as equal
static int intCount(int X, int K)
{
// Stores the total count of integers
int ans = 0;
// Loop to iterate over all
// possible values of z
for (int z = 0; z < Math.Pow(10, K);
z += ((int)Math.Pow(10, K) - 1) / 9) {
// Terminate the loop when z > X
if (z > X)
break;
// Add count of integers with
// last K digits equal to z
ans += ((X - z) / (int)Math.Pow(10, K) + 1);
}
// Return count
return ans;
}
// Function to return the count of
// integers from L to R having the
// last K digits as equal
static int intCountInRange(int L, int R, int K)
{
return (intCount(R, K)
- intCount(L - 1, K));
}
// Driver Code
public static void Main(String[] args)
{
int L = 49;
int R = 101;
int K = 2;
// Function Call
Console.Write(intCountInRange(L, R, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
6
时间复杂度: O(log K)
空间复杂度: O(1)