📌  相关文章
📜  检查N是否可以表示为从集合{A,B}中选择的整数之和

📅  最后修改于: 2021-06-26 16:11:50             🧑  作者: Mango

给定三个整数NAB ,任务是确定N是否可以表示为AB的总和。

例子:

方法:一种有效的解决方案是调用以零开头的递归函数(因为总是可能为零)。如果函数调用为fun(x),则递归调用fun(x + a)fun(x + b) (因为如果x可行,则x + ax + b也可行)。如果x> n,则返回该函数。

下面是上述方法的实现:

C++
// CPP program to find if number N can
// be represented as sum of a's and b's
#include 
using namespace std;
 
// Function to find if number N can
// be represented as sum of a's and b's
void checkIfPossibleRec(int x, int a, int b,
                   bool isPossible[], int n)
{
    // base condition
    if (x > n)
        return;
 
    // if x is already visited
    if (isPossible[x])
        return;
 
    // set x as possible
    isPossible[x] = true;
 
    // recursive call
    checkIfPossibleRec(x + a, a, b, isPossible, n);
    checkIfPossibleRec(x + b, a, b, isPossible, n);
}
 
bool checkPossible(int n, int a, int b)
{
    bool isPossible[n + 1] = { false };
    checkIfPossibleRec(0, a, b, isPossible, n);
    return isPossible[n];
}
 
// Driver program
int main()
{
    int a = 3, b = 7, n = 8;
    if (checkPossible(a, b, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to find if number N can
// be represented as sum of a's and b's
 
import java.util.*;
class solution
{
 
// Function to find if number N can
// be represented as sum of a's and b's
static void checkIfPossibleRec(int x, int a, int b,
                                boolean isPossible[], int n)
{
    // base condition
    if (x > n)
        return;
 
    // if x is already visited
    if (isPossible[x])
        return;
 
    // set x as possible
    isPossible[x] = true;
 
    // recursive call
    checkIfPossibleRec(x + a, a, b, isPossible, n);
    checkIfPossibleRec(x + b, a, b, isPossible, n);
}
 
static boolean checkPossible(int n, int a, int b)
{
    boolean isPossible[]=new boolean[n + 1];
    for(int i=0;i<=n;i++)
    isPossible[i]=false;
    checkIfPossibleRec(0, a, b, isPossible, n);
    return isPossible[n];
}
 
// Driver program
public static void main(String args[])
{
    int a = 3, b = 7, n = 8;
    if (checkPossible(a, b, n))
        System.out.print("Yes");
    else
        System.out.print( "No");
 
}
 
}
//contributed by Arnab Kundu


Python3
# Python3 program to find if number N can
# be represented as sum of a's and b's
 
# Function to find if number N can
# be represented as sum of a's and b's
def checkIfPossibleRec(x, a, b, isPossible, n):
 
    # base condition
    if x > n:
        return
 
    # If x is already visited
    if isPossible[x]:
        return
 
    # Set x as possible
    isPossible[x] = True
 
    # Recursive call
    checkIfPossibleRec(x + a, a, b, isPossible, n)
    checkIfPossibleRec(x + b, a, b, isPossible, n)
 
def checkPossible(n, a, b):
 
    isPossible = [False] * (n + 1)
    checkIfPossibleRec(0, a, b, isPossible, n)
    return isPossible[n]
 
 
# Driver Code
if __name__ == "__main__":
 
    a, b, n = 3, 7, 8
    if checkPossible(a, b, n):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Rituraj Jain


C#
// C# program to find if number N can
// be represented as sum of a's and b's
using System;
 
class GFG
{
// Function to find if number N can
// be represented as sum of a's and b's
static void checkIfPossibleRec(int x, int a, int b,
                               bool []isPossible, int n)
{
    // base condition
    if (x > n)
        return;
 
    // if x is already visited
    if (isPossible[x])
        return;
 
    // set x as possible
    isPossible[x] = true;
 
    // recursive call
    checkIfPossibleRec(x + a, a, b, isPossible, n);
    checkIfPossibleRec(x + b, a, b, isPossible, n);
}
 
static bool checkPossible(int n, int a, int b)
{
    bool []isPossible = new bool[n + 1];
    for(int i = 0; i <= n; i++)
    isPossible[i] = false;
        checkIfPossibleRec(0, a, b, isPossible, n);
    return isPossible[n];
}
 
// Driver Code
static public void Main ()
{
    int a = 3, b = 7, n = 8;
    if (checkPossible(a, b, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine( "No");
}
}
 
// This code is contributed by Sach_Code


PHP
 $n)
        return;
 
    // if x is already visited
    if ($isPossible == true)
        return;
 
    // set x as possible
    $isPossible[$x] = true;
 
    // recursive call
    checkIfPossibleRec($x + $a, $a, $b,
                       $isPossible, $n);
    checkIfPossibleRec($x + $b, $a, $b,
                       $isPossible, $n);
}
 
function checkPossible($n, $a, $b)
{
    $isPossible[$n + 1] = array(false);
    checkIfPossibleRec(0, $a, $b, $isPossible, $n);
    return $isPossible;
}
 
// Driver Code
$a = 3;
$b = 7;
$n = 8;
if (checkPossible($a, $b, $n))
    echo "No";
else
    echo "Yes";
 
// This code is contributed by Sach_Code
?>


Javascript


输出:
No

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。