给定整数K和一系列连续数字[L,R] 。任务是计算给定范围内数字根为K(1≤K≤9)的数字。数字根是一个数字的总和,直到成为单个数字为止。例如,256-> 2 + 5 + 6 = 13-> 1 + 3 = 4。
例子:
Input: L = 10, R = 22, K = 3
Output: 2
12 and 21 are the only numbers from the range whose digit sum is 3.
Input: L = 100, R = 200, K = 5
Output: 11
方法:
- 首先要注意的是,对于任何数字,数字总和等于数字%9。如果余数为0,则数字总和为9。
- 因此,如果K = 9,则将K替换为0。
- 现在的任务是找到模数等于K的L到R范围内的数字计数。
- 从L(TotalRange / 9)开始,将整个范围分成9个最大可能的组,因为在每个范围内,模数9等于K时将恰好有一个数字。
- 循环从R到R的剩余元素数–剩余元素数,并检查是否有满足条件的元素。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#define ll long long int
using namespace std;
// Function to return the count
// of required numbers
int countNumbers(int L, int R, int K)
{
if (K == 9)
K = 0;
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--) {
int rem1 = i % 9;
if (rem1 == K)
ans++;
}
return ans;
}
// Driver code
int main()
{
int L = 10;
int R = 22;
int K = 3;
cout << countNumbers(L, R, K);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the count
// of required numbers
static int countNumbers(int L, int R, int K) {
if (K == 9) {
K = 0;
}
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--) {
int rem1 = i % 9;
if (rem1 == K) {
ans++;
}
}
return ans;
}
// Driver code
public static void main(String[] args) {
int L = 10;
int R = 22;
int K = 3;
System.out.println(countNumbers(L, R, K));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function to return the count
# of required numbers
def countNumbers(L, R, K):
if (K == 9):
K = 0
# Count of numbers present
# in given range
totalnumbers = R - L + 1
# Number of groups of 9 elements
# starting from L
factor9 = totalnumbers // 9
# Left over elements not covered
# in factor 9
rem = totalnumbers % 9
# One Number in each group of 9
ans = factor9
# To check if any number in rem
# satisfy the property
for i in range(R, R - rem, -1):
rem1 = i % 9
if (rem1 == K):
ans += 1
return ans
# Driver code
L = 10
R = 22
K = 3
print(countNumbers(L, R, K))
# This code is contributed
# by mohit kumar
C#
// C# implementation of the approach
using System ;
class GFG
{
// Function to return the count
// of required numbers
static int countNumbers(int L, int R, int K)
{
if (K == 9)
{
K = 0;
}
// Count of numbers present
// in given range
int totalnumbers = R - L + 1;
// Number of groups of 9 elements
// starting from L
int factor9 = totalnumbers / 9;
// Left over elements not covered
// in factor 9
int rem = totalnumbers % 9;
// One Number in each group of 9
int ans = factor9;
// To check if any number in rem
// satisfy the property
for (int i = R; i > R - rem; i--)
{
int rem1 = i % 9;
if (rem1 == K)
{
ans++;
}
}
return ans;
}
// Driver code
public static void Main()
{
int L = 10;
int R = 22;
int K = 3;
Console.WriteLine(countNumbers(L, R, K));
}
}
/* This code is contributed by Ryuga */
PHP
$R - $rem; $i--)
{
$rem1 = $i % 9;
if ($rem1 == $K)
$ans++;
}
return $ans;
}
// Driver code
$L = 10;
$R = 22;
$K = 3;
echo countNumbers($L, $R, $K);
// This code is contributed by Ita_c
?>
Javascript
输出:
2