📌  相关文章
📜  生成序列,以使数组元素的浮点除法最大化

📅  最后修改于: 2021-04-17 14:11:49             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是通过使用括号“(”“)”和除法运算符“ /”来查找表达式,以使数组元素的后续float除法的表达式的值最大化。

例子:

方法:这个想法基于以下观察:对于每个除法,仅当分母为最小值时,结果才为最大值。因此,任务减少了放置括号和运算符的方式,使分母最小。请考虑以下示例来解决此问题:

因此,从以上示例可以得出结论,需要将括号放在第一个整数之后的序列上,以使从第二个整数开始的整个序列减小到最小值。
请按照以下步骤解决问题:

  • 将字符串S初始化为“” ,以存储最终表达式。
  • 如果N等于1 ,则以字符串形式打印整数。
  • 否则,在S中附加arr [0]“ /(” ,然后附加以“ /”分隔的arr []的所有其余整数。
  • 最后,在字符串S后面附加“)” ,并打印字符串S作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to place the parenthesis
// such that the result is maximized
void generateSequence(int arr[], int n)
{
    // Store the required string
    string ans;
 
    // Add the first integer to string
    ans = to_string(arr[0]);
 
    // If the size of array is 1
    if (n == 1)
        cout << ans;
 
    // If the size of array is 2, print
    // the 1st integer followed by / operator
    // followed by the next integer
    else if (n == 2) {
        cout << ans + "/"
             << to_string(arr[1]);
    }
 
    // If size of array is exceeds two,
    // print 1st integer concatenated
    // with operators '/', '(' and next
    // integers with the operator '/'
    else {
        ans += "/(" + to_string(arr[1]);
 
        for (int i = 2; i < n; i++) {
            ans += "/" + to_string(arr[i]);
        }
 
        // Add parenthesis at the end
        ans += ")";
 
        // Print the final expression
        cout << ans;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1000, 100, 10, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    generateSequence(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
// Function to place the parenthesis
// such that the result is maximized
static void generateSequence(int arr[], int n)
{
   
    // Store the required string
    String ans;
 
    // Add the first integer to string
    ans = Integer.toString(arr[0]);
 
    // If the size of array is 1
    if (n == 1)
        System.out.println(ans);
 
    // If the size of array is 2, print
    // the 1st integer followed by / operator
    // followed by the next integer
    else if (n == 2) {
        System.out.println(ans + "/"
            + Integer.toString(arr[1]));
    }
 
    // If size of array is exceeds two,
    // print 1st integer concatenated
    // with operators '/', '(' and next
    // integers with the operator '/'
    else {
        ans += "/(" + Integer.toString(arr[1]);
 
        for (int i = 2; i < n; i++) {
            ans += "/" + Integer.toString(arr[i]);
        }
 
        // Add parenthesis at the end
        ans += ")";
 
        // Print the final expression
        System.out.println(ans);
    }
}
 
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1000, 100, 10, 2 };
    int N = arr.length;
    generateSequence(arr, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to place the parenthesis
# such that the result is maximized
def generateSequence(arr, n):
     
    # Store the required string
    ans = ""
 
    # Add the first integer to string
    ans = str(arr[0])
 
    # If the size of array is 1
    if (n == 1):
        print(ans)
 
    # If the size of array is 2, print
    # the 1st integer followed by / operator
    # followed by the next integer
    elif (n == 2):
        print(ans + "/"+str(arr[1]))
     
    # If size of array is exceeds two,
    # pr1st integer concatenated
    # with operators '/', '(' and next
    # integers with the operator '/'
    else:
        ans += "/(" + str(arr[1])
 
        for i in range(2, n):
            ans += "/" + str(arr[i])
 
        # Add parenthesis at the end
        ans += ")"
 
        # Prthe final expression
        print(ans)
 
# Driver Code
if __name__ == '__main__':
    arr = [1000, 100, 10, 2]
    N = len(arr)
    generateSequence(arr, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to place the parenthesis
// such that the result is maximized
static void generateSequence(int []arr, int n)
{
   
    // Store the required string
    string ans="";
 
    // Add the first integer to string
    ans = arr[0].ToString();
 
    // If the size of array is 1
    if (n == 1)
        Console.WriteLine(ans);
 
    // If the size of array is 2, print
    // the 1st integer followed by / operator
    // followed by the next integer
    else if (n == 2) {
        Console.WriteLine(ans + "/"
            + arr[1].ToString());
    }
 
    // If size of array is exceeds two,
    // print 1st integer concatenated
    // with operators '/', '(' and next
    // integers with the operator '/'
    else {
        ans += "/(" + arr[1].ToString();
 
        for (int i = 2; i < n; i++) {
            ans += "/" + arr[i].ToString();
        }
 
        // Add parenthesis at the end
        ans += ")";
 
        // Print the final expression
        Console.WriteLine(ans);
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int []arr = { 1000, 100, 10, 2 };
    int N = arr.Length;
    generateSequence(arr, N);
}
}
 
// This code is contributed by chitranayal.


输出:
1000/(100/10/2)

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