📌  相关文章
📜  打印所有可以通过将 A 或 B 与 N 相加精确 M 次而获得的数字

📅  最后修改于: 2021-09-22 10:30:19             🧑  作者: Mango

给定四个整数NMAB ,任务是打印通过将AB添加到N恰好M次可以获得的所有数字(按递增顺序)。

例子:

朴素的方法:解决这个问题的最简单的方法是使用递归。在每个步骤中,只有两个选择,即添加A或添加B

请按照以下步骤解决此问题:

  • 初始化一个集合,比如数字,来存储所有可以制作的数字。
  • 基本情况:如果M等于0,则唯一可能的数字是N
  • A添加到N 后,递归调用M – 1步。
  • B添加到N 后,递归调用M – 1步。
  • 最后,迭代设置的数字并打印所有数字。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly N times
void possibleNumbers(set& numbers,
                     int N, int M,
                     int A, int B)
{
    // If number of steps is 0 and
    // only possible number is N
    if (M == 0) {
        numbers.insert(N);
        return;
    }
 
    // Add A to N and make a
    // recursive call for M - 1 steps
    possibleNumbers(numbers, N + A, M - 1, A, B);
 
    // Add B to N and make a
    // recursive call for M-1 steps.
    possibleNumbers(numbers, N + B, M - 1, A, B);
}
 
// Driver Code
int main()
{
    // Given Inputs
    int N = 5, M = 3, A = 4, B = 6;
 
    // Stores all possible numbers
    set numbers;
 
    // Function call
    possibleNumbers(numbers, N, M, A, B);
 
    // Print all possible numbers
    for (int x : numbers) {
        cout << x << " ";
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
public class MyClass{
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly N times
static void possibleNumbers(Set numbers, int N, int M, int A, int B)
{
    // If number of steps is 0 and
    // only possible number is N
    if (M == 0) {
        numbers.add(N);
        return;
    }
 
    // Add A to N and make a
    // recursive call for M - 1 steps
    possibleNumbers(numbers, N + A, M - 1, A, B);
 
    // Add B to N and make a
    // recursive call for M-1 steps.
    possibleNumbers(numbers, N + B, M - 1, A, B);
}
 
// Driver Code
  public static void main(String args[])
{
 
   // Given Inputs
    int N = 5, M = 3, A = 4, B = 6;
 
    // Stores all possible numbers
    Set numbers = new HashSet();
 
    // Function call
    possibleNumbers(numbers, N, M, A, B);
 
    // Print all possible numbers
    Iterator i = numbers.iterator();
        while (i.hasNext())
            System.out.print(i.next()+ " " );
}}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Function to find all possible
# numbers that can be obtained
# by adding A or B to N exactly N times
def possibleNumbers(numbers, N, M, A, B):
     
    # If number of steps is 0 and
    # only possible number is N
    if (M == 0):
        numbers.add(N)
        return
 
    # Add A to N and make a
    # recursive call for M - 1 steps
    possibleNumbers(numbers, N + A,
                    M - 1, A, B)
 
    # Add B to N and make a
    # recursive call for M-1 steps.
    possibleNumbers(numbers, N + B,
                    M - 1, A, B)
 
# Driver Code
if __name__ == '__main__':
     
    # Given Inputs
    N = 5
    M = 3
    A = 4
    B = 6
 
    # Stores all possible numbers
    numbers = set()
 
    # Function call
    possibleNumbers(numbers, N, M, A, B)
 
    # Print all possible numbers
    for x in numbers:
        print(x, end = " ")
         
# This code is contributed by SURENDRA_GANGWAR


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
void possibleNumbers(int N, int M,
                     int A, int B)
{
    // For maintaining
    // increasing order
    if (A > B) {
        swap(A, B);
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    cout << number << " ";
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B) {
 
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for (int i = 0; i < M; i++) {
            number = number - A + B;
            cout << number << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
    return 0;
}


Java
// Java program for tha above approach
import java.util.*;
 
class GFG{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                            int A, int B)
{
     
    // For maintaining
    // increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    System.out.print(number + " ");
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B)
    {
         
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for(int i = 0; i < M; i++)
        {
            number = number - A + B;
            System.out.print(number + " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}   
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find all possible
# numbers that can be obtained
# by adding A or B to N exactly M times
def possibleNumbers(N, M, A, B):
     
    # For maintaining
    # increasing order
    if (A > B):
        temp = A
        A = B
        B = temp
 
    # Smallest number that
    # can be obtained
    number = N + M * A
    print(number, end = " ")
 
    # If A and B are equal, then only
    # one number can be obtained, i.e. N + M*A
    if (A != B):
 
        # For finding others numbers,
        # subtract A from number once
        # and add B to number once
        for i in range(M):
            number = number - A + B
            print(number,end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    N = 5
    M = 3
    A = 4
    B = 6
 
    # Function Call
    possibleNumbers(N, M, A, B)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for tha above approach
using System;
 
class GFG{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M, int A, int B)
{
     
    // For maintaining
    // increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    Console.Write(number + " ");
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B)
    {
         
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for(int i = 0; i < M; i++)
        {
            number = number - A + B;
            Console.Write(number + " ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}   
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
void possibleNumbers(int N, int M,
                     int A, int B)
{
    // For maintaining increasing order
    if (A > B) {
        swap(A, B);
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    cout << number << " ";
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            cout << number << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
    return 0;
}


Java
// Java program for tha above approach
import java.util.*;
class GFG
{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                     int A, int B)
{
   
    // For maintaining increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    System.out.print(number +" ");
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            System.out.print(number +" ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# python 3 program for the above approach
 
# Function to find all possible
# numbers that can be obtained
# by adding A or B to N exactly M times
def possibleNumbers(N, M, A, B):
   
    # For maintaining increasing order
    if (A > B):
        temp = A
        A = B
        B = temp
 
    # Smallest number that can be achieved
    number = N + M * A
    print(number,end = " ")
 
    # If A and B are equal, the only number
    # that can be onbtained is N + M * A
    if (A != B):
 
        # For finding other numbers,
        # subtract A from number 1 time
        # and add B to number 1 time
        for i in range(M):
            number = number - A + B
            print(number,end= " ")
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    N = 5
    M = 3
    A = 4
    B = 6
 
    # Function Call
    possibleNumbers(N, M, A, B)
     
    # This code is contributed by ipg2016107.


C#
// C# program for tha above approach
using System;
class GFG
{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                     int A, int B)
{
   
    // For maintaining increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    Console.Write(number +" ");
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            Console.Write(number +" ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
17 19 21 23

时间复杂度: O(2 M )
辅助空间: O(M)

高效方法:这个问题具有重叠子问题属性和最优子结构属性。因此,可以使用动态规划来优化上述方法。

请按照以下步骤解决此问题:

  • 初始化一个向量,比如dp[][]和一个集合,比如数字,其中dp[i][j]存储是否 i步中是否访问了数字j
  • 基本情况:如果M等于0,则唯一可能的数字是N
  • 如果(N + A)尚未在(M-1)中获得步骤之前,再加入N之后使递归调用对于M – 1个步骤和标记DP [M – 1] [N + A]作为参观过。
  • 如果(N + B)尚未在(M – 1)得到的步骤以前,然后添加BN之后使递归调用对于M – 1个步骤和标记DP [M – 1] [N + B]参观过。
  • 最后,迭代设置的数字并打印所有数字。

下面是上述方法的实现

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
void possibleNumbers(int N, int M,
                     int A, int B)
{
    // For maintaining
    // increasing order
    if (A > B) {
        swap(A, B);
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    cout << number << " ";
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B) {
 
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for (int i = 0; i < M; i++) {
            number = number - A + B;
            cout << number << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
    return 0;
}

Java

// Java program for tha above approach
import java.util.*;
 
class GFG{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                            int A, int B)
{
     
    // For maintaining
    // increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    System.out.print(number + " ");
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B)
    {
         
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for(int i = 0; i < M; i++)
        {
            number = number - A + B;
            System.out.print(number + " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}   
}
 
// This code is contributed by sanjoy_62

蟒蛇3

# Python3 program for the above approach
 
# Function to find all possible
# numbers that can be obtained
# by adding A or B to N exactly M times
def possibleNumbers(N, M, A, B):
     
    # For maintaining
    # increasing order
    if (A > B):
        temp = A
        A = B
        B = temp
 
    # Smallest number that
    # can be obtained
    number = N + M * A
    print(number, end = " ")
 
    # If A and B are equal, then only
    # one number can be obtained, i.e. N + M*A
    if (A != B):
 
        # For finding others numbers,
        # subtract A from number once
        # and add B to number once
        for i in range(M):
            number = number - A + B
            print(number,end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    N = 5
    M = 3
    A = 4
    B = 6
 
    # Function Call
    possibleNumbers(N, M, A, B)
     
# This code is contributed by SURENDRA_GANGWAR

C#

// C# program for tha above approach
using System;
 
class GFG{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M, int A, int B)
{
     
    // For maintaining
    // increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that
    // can be obtained
    int number = N + M * A;
    Console.Write(number + " ");
 
    // If A and B are equal, then only
    // one number can be obtained, i.e. N + M*A
    if (A != B)
    {
         
        // For finding others numbers,
        // subtract A from number once
        // and add B to number once
        for(int i = 0; i < M; i++)
        {
            number = number - A + B;
            Console.Write(number + " ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}   
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出:
17 19 21 23

时间复杂度: O(M)
辅助空间: O(M*10 5 )

高效方法:上述方法可以进一步优化贪婪。请按照以下步骤解决此问题:

  • 如果A大于B ,则交换AB以保持递增顺序。
  • 初始化一个变量,比如numberN + M * A,即可以达到的最小数字,并打印number
  • 如果A不等于B ,则使用变量i在范围[0, M – 1] 上迭代并打印number – A + B

下面是上述方法的实现。

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
void possibleNumbers(int N, int M,
                     int A, int B)
{
    // For maintaining increasing order
    if (A > B) {
        swap(A, B);
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    cout << number << " ";
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            cout << number << " ";
        }
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
    return 0;
}

Java

// Java program for tha above approach
import java.util.*;
class GFG
{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                     int A, int B)
{
   
    // For maintaining increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    System.out.print(number +" ");
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            System.out.print(number +" ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}
}
 
// This code is contributed by shivanisinghss2110

蟒蛇3

# python 3 program for the above approach
 
# Function to find all possible
# numbers that can be obtained
# by adding A or B to N exactly M times
def possibleNumbers(N, M, A, B):
   
    # For maintaining increasing order
    if (A > B):
        temp = A
        A = B
        B = temp
 
    # Smallest number that can be achieved
    number = N + M * A
    print(number,end = " ")
 
    # If A and B are equal, the only number
    # that can be onbtained is N + M * A
    if (A != B):
 
        # For finding other numbers,
        # subtract A from number 1 time
        # and add B to number 1 time
        for i in range(M):
            number = number - A + B
            print(number,end= " ")
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    N = 5
    M = 3
    A = 4
    B = 6
 
    # Function Call
    possibleNumbers(N, M, A, B)
     
    # This code is contributed by ipg2016107.

C#

// C# program for tha above approach
using System;
class GFG
{
     
// Function to find all possible
// numbers that can be obtained
// by adding A or B to N exactly M times
static void possibleNumbers(int N, int M,
                     int A, int B)
{
   
    // For maintaining increasing order
    if (A > B)
    {
        int temp = A;
        A = B;
        B = temp;
    }
 
    // Smallest number that can be achieved
    int number = N + M * A;
    Console.Write(number +" ");
 
    // If A and B are equal, the only number
    // that can be onbtained is N + M * A
    if (A != B) {
 
        // For finding other numbers,
        // subtract A from number 1 time
        // and add B to number 1 time
        for (int i = 0; i < M; i++) {
 
            number = number - A + B;
            Console.Write(number +" ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given Input
    int N = 5, M = 3, A = 4, B = 6;
 
    // Function Call
    possibleNumbers(N, M, A, B);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出:
17 19 21 23

时间复杂度: O(M)
辅助空间: O(1)