检查一个数字是否可以表示为至少一位数字等于 K 的数字之和
给定整数N和K ,任务是检查一个数字是否可以表示为至少有一位数字等于 K 的数字的总和。
例子:
Input: N = 68, K = 7
Output: YES
Explanation: 68 = (27 + 17 + 17 + 7). Each number has atleast one digit equal to 7.
Input: N = 23, K = 3
Output: YES
Explanation: 23 itself contains a digit equal to 3.
方法:给定的问题可以通过使用简单的数学概念来解决。请按照以下步骤解决问题:
- 将变量temp初始化为k ,并取一个计数器说count ,将其分配为 0
- 迭代直到temp和N的最后一位不相等,并且在每次迭代中
- 将temp的值增加k
- 保持迭代计数并在计数大于 10 时中断循环
- 检查temp和N的最后一位是否相等,以及temp <= N的值是否相等:
- 如果满足上述条件,则返回 true
- 否则返回假
- 此外,如果k * 10 <= N ,则返回 true
- 否则返回假
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to Check if a number can
// be equal to sum of numbers having
// at least one digit equal to k
bool checkEqualtoSum(int N, int k)
{
// Temporary variable to
// store k
int temp = k;
// Variable for count
int count = 0;
// Iterating till count is less or
// equal to 10 and N % 10 is not
// equal to temp % 10
while(count <= 10 &&
N % 10 != temp % 10) {
temp += k;
count++;
}
// If N % 10 is equal to temp % 10
// and temp is less or equal to N,
// return true
if(N % 10 == temp % 10 && temp <= N)
return true;
// If k * 10 <= N, return true
if(k * 10 <= N)
return true;
// Else return false
return false;
}
// Driver Code
int main()
{
int N = 68;
int K = 7;
// Call the function
if(checkEqualtoSum(N, K))
cout << "YES";
else cout << "NO";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to Check if a number can
// be equal to sum of numbers having
// at least one digit equal to k
static boolean checkEqualtoSum(int N, int k)
{
// Temporary variable to
// store k
int temp = k;
// Variable for count
int count = 0;
// Iterating till count is less or
// equal to 10 and N % 10 is not
// equal to temp % 10
while (count <= 10 && N % 10 != temp % 10) {
temp += k;
count++;
}
// If N % 10 is equal to temp % 10
// and temp is less or equal to N,
// return true
if (N % 10 == temp % 10 && temp <= N)
return true;
// If k * 10 <= N, return true
if (k * 10 <= N)
return true;
// Else return false
return false;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 68;
int K = 7;
// Call the function
if (checkEqualtoSum(N, K))
System.out.println("YES");
else
System.out.println("NO");
}
}
// This code is contributed by dwivediyash
Python3
# python implementation for the above approach
# Function to Check if a number can
# be equal to sum of numbers having
# at least one digit equal to k
def checkEqualtoSum(N, k):
# Temporary variable to
# store k
temp = k
# Variable for count
count = 0
# Iterating till count is less or
# equal to 10 and N % 10 is not
# equal to temp % 10
while(count <= 10 and N % 10 != temp % 10):
temp += k
count += 1
# If N % 10 is equal to temp % 10
# and temp is less or equal to N,
# return true
if(N % 10 == temp % 10 and temp <= N):
return True
# If k * 10 <= N, return true
if(k * 10 <= N):
return True
# Else return false
return False
# Driver Code
if __name__ == "__main__":
N = 68
K = 7
# Call the function
if(checkEqualtoSum(N, K)):
print("YES")
else:
print("NO")
# This code is contributed by rakeshsahni
Javascript
C#
// C# implementation for the above approach
using System;
class gFG
{
// Function to Check if a number can
// be equal to sum of numbers having
// at least one digit equal to k
static bool checkEqualtoSum(int N, int k)
{
// Temporary variable to
// store k
int temp = k;
// Variable for count
int count = 0;
// Iterating till count is less or
// equal to 10 and N % 10 is not
// equal to temp % 10
while (count <= 10 && N % 10 != temp % 10) {
temp += k;
count++;
}
// If N % 10 is equal to temp % 10
// and temp is less or equal to N,
// return true
if (N % 10 == temp % 10 && temp <= N)
return true;
// If k * 10 <= N, return true
if (k * 10 <= N)
return true;
// Else return false
return false;
}
// Driver Code
public static void Main()
{
int N = 68;
int K = 7;
// Call the function
if (checkEqualtoSum(N, K))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by ukasp.
输出
YES
时间复杂度: O(1)
辅助空间: O(1)