📌  相关文章
📜  构造一个由A和B组成的AP系列,其第N项的个数最少

📅  最后修改于: 2021-05-14 01:37:39             🧑  作者: Mango

给定两个整数A,B(它们是算术级数序列的任意两项)和整数N ,任务是构造大小为N的算术级数序列,以使其必须同时包括AB以及第N项。 AP应该是最小的。

例子:

方法: AP的第N个项X N = X +(N – 1)* d给出,其中X是第一个项, d是一个共同的差。要使最大元素最小,请同时最小化xd 。可以观察到X的值不能大于min(A,B)并且 d的值不能大于abs(A – B)

  1. 现在,使用相同的公式为x(1min(A,B) )和d (从1abs(A – B) )的每个可能值构造AP。
  2. 现在,将数组arr []构造为{x,x + d,x + 2d,…,x + d *(N – 1)}
  3. 检查其中是否存在AB以及第N元素 最小可能与否。如果发现为真,则通过构造的数组arr []更新ans []
  4. 否则,请进一步迭代并检查xd的其他值。
  5. 最后,打印ans []作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if both a and
// b are present in the AP series or not
bool check_both_present(int arr[], int N,
                        int a, int b)
{
    bool f1 = false, f2 = false;
 
    // Iterate over the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If a is present
        if (arr[i] == a) {
            f1 = true;
        }
 
        // If b is present
        if (arr[i] == b) {
            f2 = true;
        }
    }
 
    // If both are present
    if (f1 && f2) {
        return true;
    }
 
    // Otherwise
    else {
        return false;
    }
}
 
// Function to print all the elements
// of the Arithmetic Progression
void print_array(int ans[], int N)
{
    for (int i = 0; i < N; i++) {
        cout << ans[i] << " ";
    }
}
 
// Function to construct AP series
// consisting of A and B with
// minimum Nth term
void build_AP(int N, int a, int b)
{
    // Stores the resultant series
    int arr[N], ans[N];
 
    // Initialise ans[i] as INT_MAX
    for (int i = 0; i < N; i++)
        ans[i] = INT_MAX;
 
    int flag = 0;
 
 // Maintain a smaller than b
    if (a > b) {
        swap(a, b);
    }
 
    // Difference between a and b
    int diff = b - a;
 
    // Check for all possible combination
    // of start and common difference d
    for (int start = 1;
         start <= a; start++) {
 
        for (int d = 1;
             d <= diff; d++) {
 
            // Initialise arr[0] as start
            arr[0] = start;
 
            for (int i = 1; i < N; i++) {
 
                arr[i] = arr[i - 1] + d;
            }
 
            // Check if both a and b are
            // present or not and the Nth
            // term is the minimum or not
            if (check_both_present(arr, N, a, b)
                && arr[N - 1] < ans[N - 1]) {
 
                // Update the answer
                for (int i = 0; i < N; i++) {
                    ans[i] = arr[i];
                }
            }
        }
    }
 
    // Print the resultant array
    print_array(ans, N);
}
 
// Driver Code
int main()
{
    int N = 5, A = 20, B = 50;
 
    // Function Call
    build_AP(N, A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to check if both a and
// b are present in the AP series or not
public static boolean check_both_present(int[] arr,
                                         int N, int a,
                                         int b)
{
    boolean f1 = false, f2 = false;
 
    // Iterate over the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If a is present
        if (arr[i] == a)
        {
            f1 = true;
        }
 
        // If b is present
        if (arr[i] == b)
        {
            f2 = true;
        }
    }
 
    // If both are present
    if (f1 && f2)
    {
        return true;
    }
 
    // Otherwise
    else
    {
        return false;
    }
}
 
// Function to print all the elements
// of the Arithmetic Progression
public static void print_array(int[] ans, int N)
{
    for(int i = 0; i < N; i++)
    {
        System.out.print(ans[i] + " ");
    }
}
 
// Function to construct AP series
// consisting of A and B with
// minimum Nth term
public static void build_AP(int N, int a, int b)
{
     
    // Stores the resultant series
    int[] arr = new int[N];
    int[] ans = new int[N];
 
    // Initialise ans[i] as INT_MAX
    for(int i = 0; i < N; i++)
        ans[i] = Integer.MAX_VALUE;
 
    int flag = 0;
 
    // Maintain a smaller than b
    if (a > b)
    {
         
        // swap(a and b)
        a += (b - (b = a));
    }
 
    // Difference between a and b
    int diff = b - a;
 
    // Check for all possible combination
    // of start and common difference d
    for(int start = 1; start <= a; start++)
    {
        for(int d = 1; d <= diff; d++)
        {
             
            // Initialise arr[0] as start
            arr[0] = start;
 
            for(int i = 1; i < N; i++)
            {
                arr[i] = arr[i - 1] + d;
            }
 
            // Check if both a and b are
            // present or not and the Nth
            // term is the minimum or not
            if (check_both_present(arr, N, a, b) &&
                arr[N - 1] < ans[N - 1])
            {
                 
                // Update the answer
                for(int i = 0; i < N; i++)
                {
                    ans[i] = arr[i];
                }
            }
        }
    }
 
    // Print the resultant array
    print_array(ans, N);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5, A = 20, B = 50;
 
    // Function call
    build_AP(N, A, B);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
import sys
 
# Function to check if both a and
# b are present in the AP series or not
def check_both_present(arr, N, a, b):
     
    f1 = False
    f2 = False
 
    # Iterate over the array arr[]
    for i in range(0, N):
 
        # If a is present
        if arr[i] == a:
            f1 = True
 
        # If b is present
        if arr[i] == b:
            f2 = True
 
    # If both are present
    if f1 and f2:
        return True
 
    # Otherwise
    else:
        return False
 
# Function to print all the elements
# of the Arithmetic Progression
def print_array(ans, N):
     
    for i in range(0, N):
        print(ans[i], end = " ")
 
# Function to construct AP series
# consisting of A and B with
# minimum Nth term
def build_AP(N, a, b):
     
    INT_MAX = sys.maxsize
 
    # Stores the resultant series
    arr = [None for i in range(N)]
 
    # Initialise ans[i] as INT_MAX
    ans = [INT_MAX for i in range(N)]
 
    flag = 0
 
    # Maintain a smaller than b
    if a > b:
         
        # Swap a and b
        a, b = b, a
 
    # Difference between a and b
    diff = b - a
 
    # Check for all possible combination
    # of start and common difference d
    for start in range(1, a + 1):
        for d in range(1, diff + 1):
 
            # Initialise arr[0] as start
            arr[0] = start
 
            for i in range(1, N):
                arr[i] = arr[i - 1] + d
 
            # Check if both a and b are
            # present or not and the Nth
            # term is the minimum or not
            if ((check_both_present(arr, N, a, b) and
                 arr[N - 1] < ans[N - 1])):
 
                # Update the answer
                for i in range(0, N):
                    ans[i] = arr[i]
 
    # Print the resultant array
    print_array(ans, N)
 
# Driver Code
if __name__ == "__main__":
     
    N = 5
    A = 20
    B = 50
 
    # Function call
    build_AP(N, A, B)
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if both a and
// b are present in the AP series or not
static bool check_both_present(int[] arr, int N,
                               int a, int b)
{
    bool f1 = false, f2 = false;
 
    // Iterate over the array arr[]
    for(int i = 0; i < N; i++)
    {
 
        // If a is present
        if (arr[i] == a)
        {
            f1 = true;
        }
 
        // If b is present
        if (arr[i] == b)
        {
            f2 = true;
        }
    }
 
    // If both are present
    if (f1 && f2)
    {
        return true;
    }
 
    // Otherwise
    else
    {
        return false;
    }
}
 
// Function to print all the elements
// of the Arithmetic Progression
static void print_array(int[] ans, int N)
{
    for(int i = 0; i < N; i++)
    {
        Console.Write(ans[i] + " ");
    }
}
 
// Function to construct AP series
// consisting of A and B with
// minimum Nth term
static void build_AP(int N, int a, int b)
{
     
    // Stores the resultant series
    int[] arr = new int[N];
    int[] ans = new int[N];
 
    // Initialise ans[i] as INT_MAX
    for(int i = 0; i < N; i++)
        ans[i] = int.MaxValue;
 
    // Maintain a smaller than b
    if (a > b)
    {
         
        // Swap a and b
        a += (b - (b = a));
    }
 
    // Difference between a and b
    int diff = b - a;
 
    // Check for all possible combination
    // of start and common difference d
    for(int start = 1; start <= a; start++)
    {
        for(int d = 1; d <= diff; d++)
        {
             
            // Initialise arr[0] as start
            arr[0] = start;
 
            for(int i = 1; i < N; i++)
            {
                arr[i] = arr[i - 1] + d;
            }
 
            // Check if both a and b are
            // present or not and the Nth
            // term is the minimum or not
            if (check_both_present(arr, N, a, b) &&
                arr[N - 1] < ans[N - 1])
            {
                 
                // Update the answer
                for(int i = 0; i < N; i++)
                {
                    ans[i] = arr[i];
                }
            }
        }
    }
 
    // Print the resultant array
    print_array(ans, N);
}
 
// Driver Code
static public void Main()
{
    int N = 5, A = 20, B = 50;
 
    // Function call
    build_AP(N, A, B);
}
}
 
// This code is contributed by akhilsaini


输出:
10 20 30 40 50





时间复杂度: O(N 3 )
辅助空间: O(N)