📌  相关文章
📜  生成一个序列,使得数组元素的浮点划分最大化

📅  最后修改于: 2021-09-03 14:38:53             🧑  作者: Mango

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

例子:

方法:这个想法是基于这样的观察,即对于每个除法,只有当分母最小时,结果才最大。因此,任务简化为以分母最小的方式放置括号和运算符。考虑以下示例来解决问题:

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

  • 将字符串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)