给定一个由N个整数组成的数组arr[] ,任务是通过使用括号‘(‘和‘)’和除法运算符‘/’来找到表达式,以最大化数组元素后续浮点除法的表达式的值。
例子:
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.
Javascript
1000/(100/10/2)
时间复杂度: O(N)
辅助空间: O(N)