📌  相关文章
📜  检查 N 是否可以表示为至少一次包含数字 D 的正整数之和

📅  最后修改于: 2022-05-13 01:56:06.908000             🧑  作者: Mango

检查 N 是否可以表示为至少一次包含数字 D 的正整数之和

给定一个正整数N和一个数字D ,任务是检查N是否可以表示为至少一次包含数字D的正整数之和。如果可以用这种格式表示N ,则打印“Yes” 。否则,打印“否”

例子:

方法:按照以下步骤解决问题:

  • 检查给定的N是否包含数字D。如果发现是真的,则打印“是”
  • 否则,迭代直到N大于0并执行以下步骤:
    • N的值中减去D的值。
    • 检查N的更新值是否包含数字D。如果发现为,则打印“是”并跳出循环。
  • 完成上述步骤后,如果以上条件都不满足,则打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if N contains
// digit D in it
bool findDigit(int N, int D)
{
    // Iterate until N is positive
    while (N > 0) {
 
        // Find the last digit
        int a = N % 10;
 
        // If the last digit is the
        // same as digit D
        if (a == D) {
            return true;
        }
 
        N /= 10;
    }
 
    // Return false
    return false;
}
 
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
bool check(int N, int D)
{
    // Iterate until N is positive
    while (N > 0) {
 
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true) {
            return true;
        }
 
        // Subtracting D from N
        N -= D;
    }
 
    // Return false
    return false;
}
 
// Driver Code
int main()
{
    int N = 24;
    int D = 7;
    if (check(N, D)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}


Java
// Java approach for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if N contains
// digit D in it
static boolean findDigit(int N, int D)
{
     
    // Iterate until N is positive
    while (N > 0)
    {
         
        // Find the last digit
        int a = N % 10;
 
        // If the last digit is the
        // same as digit D
        if (a == D)
        {
            return true;
        }
        N /= 10;
    }
 
    // Return false
    return false;
}
 
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
static boolean check(int N, int D)
{
     
    // Iterate until N is positive
    while (N > 0)
    {
         
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true)
        {
            return true;
        }
         
        // Subtracting D from N
        N -= D;
    }
     
    // Return false
    return false;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 24;
    int D = 7;
     
    if (check(N, D))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to check if N contains
# digit D in it
def findDigit(N, D):
     
    # Iterate until N is positive
    while (N > 0):
         
        # Find the last digit
        a = N % 10
 
        # If the last digit is the
        # same as digit D
        if (a == D):
            return True
 
        N /= 10
 
    # Return false
    return False
 
# Function to check if the value of
# N can be represented as sum of
# integers having digit d in it
def check(N, D):
     
    # Iterate until N is positive
    while (N > 0):
 
        # Check if N contains digit
        # D or not
        if (findDigit(N, D) == True):
            return True
 
        # Subtracting D from N
        N -= D
 
    # Return false
    return False
 
# Driver Code
if __name__ == '__main__':
     
    N = 24
    D = 7
     
    if (check(N, D)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if N contains
// digit D in it
static bool findDigit(int N, int D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Find the last digit
        int a = N % 10;
  
        // If the last digit is the
        // same as digit D
        if (a == D)
        {
            return true;
        }
        N /= 10;
    }
  
    // Return false
    return false;
}
  
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
static bool check(int N, int D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true)
        {
            return true;
        }
          
        // Subtracting D from N
        N -= D;
    }
      
    // Return false
    return false;
}
 
  
// Driver Code
public static void Main()
{
    int N = 24;
    int D = 7;
      
    if (check(N, D))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
      
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
Yes

时间复杂度: O(N)
空间复杂度: O(1)