【C# 每日一题7】求最大的结果

pmars 2011-05-30 09:33:39
加精

Description

Give you a expression,you can add some parenthesis to maximize result.Example as following:
1 + 2 * 3
Its result is 7 in the example.But you can add a parenthesis like (1 + 2) * 3,then the result is 9,and so it is the maximization of the example.You job is to find the maximizaton of a expression. To simplify this problem,you can assume that expression contains only operator + and *,but note that integers may be negative.

Input

Input contains multiple test cases.Each case begin with a integer N(2<=N<=100), the number of integers of a expression.Then follows the expression.Integers and operator spearate with a single whitespace.

Output

For each case print a single line with the maximized value of the expression.

Sample Input

4
1 + 2 * 3 + -1

Sample Output

8


解释一下,输入一个数n代表有下面算式里面有n个数字,每个数字中间有一个符号(+ or * )
之后输入这个算式
在随意加括号之后求出算式可以得到的最大值!
如:
4
1 + 2 * 3 + -1
变为( 1 + 2 ) * 3 + -1 = 8
输出最大的值!!

求实现!哈哈!
...全文
1200 97 打赏 收藏 转发到动态 举报
写回复
用AI写文章
97 条回复
切换为时间正序
请发表友善的回复…
发表回复
DanaMeng 2011-06-02
  • 打赏
  • 举报
回复
来学习学习……
Dj_BoAer 2011-06-02
  • 打赏
  • 举报
回复
好东西,学习学习
tiger999 2011-06-02
  • 打赏
  • 举报
回复
1> turn the expression into reverse polish notation which can be implemented by building a expression tree and do a post order traversal.

2> use stack to evaluate the expression.
tiger999 2011-06-02
  • 打赏
  • 举报
回复
there is a easy way.

use stack
windear520 2011-06-01
  • 打赏
  • 举报
回复
应该以*号位中心点、、、再考虑如何让*号两边取得最大值、、、
huayangniahua 2011-06-01
  • 打赏
  • 举报
回复
悲剧,,居然看不懂是在干嘛
wofeizhenlong 2011-05-31
  • 打赏
  • 举报
回复
路过 学习一下
绿色夹克衫 2011-05-31
  • 打赏
  • 举报
回复
凑合写了一个


using System;
using System.Numerics;

namespace CSharpTest
{
class Program
{
public static void Main()
{
int[] Items = new int[] { 8, -9, -1, 2, -7 };
BigInteger[, ,] Matrix = new BigInteger[Items.Length, Items.Length, 2];

for (int i = 0; i < Items.Length; i++)
{
for (int j = 0; j + i < Items.Length; j++)
{
int start = j;
int end = i + j;

if (i == 0)
Matrix[start, end, 0] = Matrix[start, end, 1] = Items[start];
else
{
Matrix[start, end, 0] = int.MinValue;
Matrix[start, end, 1] = int.MaxValue;
}

for (int k = start; k < end; k++)
{
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 0] + Matrix[k + 1, end, 0]);
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 0] * Matrix[k + 1, end, 0]);
Matrix[start, end, 0] = Max(Matrix[start, end, 0], Matrix[start, k, 1] * Matrix[k + 1, end, 1]);

Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 1] + Matrix[k + 1, end, 1]);
Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 0] * Matrix[k + 1, end, 1]);
Matrix[start, end, 1] = Min(Matrix[start, end, 1], Matrix[start, k, 1] * Matrix[k + 1, end, 0]);
}
}
}

Console.WriteLine(Matrix[0, Items.Length - 1, 0]);
Console.ReadKey();
}

public static BigInteger Max(BigInteger a, BigInteger b)
{
return a > b ? a : b;
}

public static BigInteger Min(BigInteger a, BigInteger b)
{
return a < b ? a : b;
}
}
}
KLL 2011-05-31
  • 打赏
  • 举报
回复
都是牛人呀
gomoku 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 47 楼 litaoye 的回复:]
粗略看了一下gomoku同志的代码,似乎没有考虑两边为负数*之后更大的情况,不知道说对了没有,没说对的话,gomoku不要介意呦。
[/Quote]
你说的是:)
下面是改正错误的代码,对几万个随机表达式(最多10多个操作数),用穷举法核实过。

void Test()
{
int i = Maximize("-2 * 1 + 1 * -1"); // i=4
}
class Node
{
public Node(int value, char op) {this.Value = value; this.Operator = op;}
public int Value {get; set;}
public char Operator {get; set;}
public Node More { get; set; }
public static Node operator &(Node n1, Node n2)
{
if (n1 == null || n2 == null) return null;
switch(n1.Operator)
{
case '+': return new Node(n1.Value + n2.Value, n2.Operator );
case '*': return new Node(n1.Value * n2.Value, n2.Operator );
case '-': return new Node(n1.Value - n2.Value, n2.Operator );
}
throw new InvalidOperationException(n1.Operator + " operator not supported");
}
}

int Maximize(string expression)
{
string[] toks = (expression + " /").Split(' ');
int operandCount = toks.Length / 2;

Node[,] matrix = new Node[operandCount, operandCount];
for (int i = 0; i < operandCount; i++)
{
matrix[i, i] = new Node(int.Parse(toks[i + i]), toks[i + i + 1][0]);
}

for (int e = 0; e < operandCount; e++)
{
for (int s = e - 1; s >= 0; s--)
{
Node max = new Node(int.MinValue, '/');
Node min = new Node(int.MaxValue, '/');
for (int split = e - 1; split >= s; split--)
{
Node[] ns =
{
matrix[s, split] & matrix[split + 1, e],
matrix[s, split].More & matrix[split + 1, e],
matrix[s, split] & matrix[split + 1, e].More,
matrix[s, split].More & matrix[split + 1, e].More,
};
foreach (Node node in ns)
{
if (node != null && node.Value > max.Value) max = node;
if (node != null && node.Value < min.Value) min = node;
}
}
matrix[s, e] = max; if(max.Value != min.Value) max.More = min;
}
}
return matrix[0, matrix.GetLength(0)-1].Value;
}
hankanling123 2011-05-31
  • 打赏
  • 举报
回复
支持,,,,,,,,,,
jiangmurong 2011-05-31
  • 打赏
  • 举报
回复
不太懂,给点注释
hema_20110530 2011-05-31
  • 打赏
  • 举报
回复
应该是动态规划的问题
acd1214 2011-05-31
  • 打赏
  • 举报
回复
什么啊, 给点注释
cena_jin 2011-05-31
  • 打赏
  • 举报
回复
太复杂了!
swz1222 2011-05-31
  • 打赏
  • 举报
回复
关注一下
L19999 2011-05-31
  • 打赏
  • 举报
回复
支持。。。。。。。。。。
L19999 2011-05-31
  • 打赏
  • 举报
回复
学习



,,,,,,,,,






hww6891 2011-05-31
  • 打赏
  • 举报
回复
支持一下,向牛人学习
zhdi1029 2011-05-31
  • 打赏
  • 举报
回复
我看不明白
加载更多回复(72)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧