给定一个由N个整数组成的数组arr [] ,任务是通过使用括号“(”和“)”和除法运算符“ /”来查找表达式,以使数组元素的后续float除法的表达式的值最大化。
例子:
Input: arr[] = {1000, 100, 10, 2}
Output: “1000/(100/10/2)”
Explanation:
The value of the expression 1000/(100/10/2) can be calculated as 1000/((100/10)/2) = 200.
Input: arr[] = {2, 3, 4}
Output: “2/(3/4)”
方法:这个想法基于以下观察:对于每个除法,仅当分母为最小值时,结果才为最大值。因此,任务减少了放置括号和运算符的方式,使分母最小。请考虑以下示例来解决此问题:
Consider an expression 1000 / 100 / 10 / 2.
To make its value maximum, denominator needs to be minimized. Therefore, the denominators need to be in the sequence 100, 10, 2.
Now, consider the following cases:
- 100 / (10 / 2) = (100 × 2) / 10 = 20
- (100 / 10) / 2 = 10 / 2 = 5
Therefore, the minimized value for the expression is obtained for the second case. Therefore, 1000 / (100 / 10 / 2) is the required sequence.
因此,从以上示例可以得出结论,需要将括号放在第一个整数之后的序列上,以使从第二个整数开始的整个序列减小到最小值。
请按照以下步骤解决问题:
- 将字符串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)