本文概述
- C ++
- Java
- Python3
- C#
例子:
输入:L = 5, R = 11, K = 10方法:初始化count = 0,对于[L, R]范围内的每个元素,检查它是否包含至少一个除k的数字,如果是,则增加计数。
输出:3 5、10和11就是这样的数字。
输入:L = 32, R = 38, K = 13
输出:0
下面是上述方法的实现:
C ++
//C++ implementation of the approach
#include <
bits/stdc++.h>
using namespace std;
//Function that returns true if num
//contains at least one digit
//that divides k
bool digitDividesK( int num, int k)
{
while (num) {//Get the last digit
int d = num % 10;
//If the digit is non-zero
//and it divides k
if (d != 0 and k % d == 0)
return true ;
//Remove the last digit
num = num /10;
}//There is no digit in num
//that divides k
return false ;
}//Function to return the required
//count of elements from the given
//range which contain at least one
//digit that divides k
int findCount( int l, int r, int k)
{//To store the result
int count = 0;
//For every number from the range
for ( int i = l;
i <
= r;
i++) {//If any digit of the current
//number divides k
if (digitDividesK(i, k))
count++;
}
return count;
}//Driver code
int main()
{
int l = 20, r = 35;
int k = 45;
cout <
<
findCount(l, r, k);
return 0;
}
Java
//Java implementation of the approach class GFG
{
//Function that returns true if num
//contains at least one digit
//that divides k
static boolean digitDividesK( int num, int k)
{
while (num != 0 )
{ //Get the last digit
int d = num % 10 ;
//If the digit is non-zero
//and it divides k
if (d != 0 &
&
k % d == 0 )
return true ;
//Remove the last digit
num = num /10 ;
} //There is no digit in num
//that divides k
return false ;
} //Function to return the required
//count of elements from the given
//range which contain at least one
//digit that divides k
static int findCount( int l, int r, int k)
{ //To store the result
int count = 0 ;
//For every number from the range
for ( int i = l;
i <
= r;
i++)
{ //If any digit of the current
//number divides k
if (digitDividesK(i, k))
count++;
}
return count;
} //Driver code
public static void main(String []args)
{
int l = 20 , r = 35 ;
int k = 45 ;
System.out.println(findCount(l, r, k));
}
}//This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach# Function that returns true if num
# contains at least one digit
# that divides k
def digitDividesK(num, k):
while (num):# Get the last digit
d = num % 10# If the digit is non-zero
# and it divides k
if (d ! = 0 and k % d = = 0 ):
return True# Remove the last digit
num = num //10# There is no digit in num
# that divides k
return False# Function to return the required
# count of elements from the given
# range which contain at least one
# digit that divides k
def findCount(l, r, k):# To store the result
count = 0# For every number from the range
for i in range (l, r + 1 ):# If any digit of the current
# number divides k
if (digitDividesK(i, k)):
count + = 1return count# Driver code
l = 20
r = 35
k = 45print (findCount(l, r, k))# This code is contributed by Mohit Kumar
C#
//C# implementation of the approach
using System;
class GFG
{
//Function that returns true if num
//contains at least one digit
//that divides k
static bool digitDividesK( int num, int k)
{
while (num != 0)
{ //Get the last digit
int d = num % 10;
//If the digit is non-zero
//and it divides k
if (d != 0 &
&
k % d == 0)
return true ;
//Remove the last digit
num = num /10;
} //There is no digit in num
//that divides k
return false ;
} //Function to return the required
//count of elements from the given
//range which contain at least one
//digit that divides k
static int findCount( int l, int r, int k)
{ //To store the result
int count = 0;
//For every number from the range
for ( int i = l;
i <
= r;
i++)
{ //If any digit of the current
//number divides k
if (digitDividesK(i, k))
count++;
}
return count;
} //Driver code
public static void Main()
{
int l = 20, r = 35;
int k = 45;
Console.WriteLine(findCount(l, r, k));
}
}//This code is contributed by AnkitRai01
【[L,R]范围内的数字计数,其中至少包含一个除以K的数字】输出如下:
10
推荐阅读
- Python Django-allauth设置和配置
- 从BST构建二叉树,使其遍历级别顺序可打印排序的数据
- 将二进制字符串拆分为0和1相等数量的子字符串
- win10变回win7图文详细教程图解
- 本图文详细教程教你各版本Ghost windows10系统激活码密钥
- 最新通用版win10激活密钥分享制作详细说明
- win10 64位 产品密钥分享制作详细说明
- windows10专业版激活密钥分享制作详细说明
- 系统之家ghost win10系统最新推荐