y*a
2 楼
遍历所有的可能性,计算。
public int maxResult(int[]A) {
int[]rel=new int[1];rel[0]=Integer.MIN_VALUE;
dfs(A, new int[A.length-1], 0, rel);
return rel[0];
}
void dfs(int[]A, int[]op, int i, int[] max) {
if (i==op.length) {
StringBuilder s=new StringBuilder();
s.append(Integer.toString(A[0]));
char[] opc=new char[4];
opc[0]='+';opc[1]='-';opc[2]='*';opc[3]='/';
for (int j=0;j s.append(opc[op[j]]);
s.append(A[j+1]);
}
max[0]=Math.max(max[0], eval(s.toString()));
return;
}
for (int j=0;j<4;++j){
op[i]=j;
dfs(A, op, i+1, max);
}
}
int eval(String s) {
List rel=new ArrayList<>();
//0 : operator 1: numbers
for (int i=0, n=s.length();i if (rel.size()==0||rel.get(rel.size()-1).charAt(0)=='0') {
int j=i+1;
while (j rel.add("1,"+s.substring(i,j));
i=j;
} else {
rel.add("0,"+s.substring(i,i+1));
++i;
}
}
return evaluateInorder(rel);
}
int evaluateInorder(List s) {
String[] rp=new String[s.size()];
Queuestc=Collections.asLifoQueue(new ArrayDeque());
int j=0;
for (int i=0;i if (s.get(i).charAt(0)=='0') {
char c=s.get(i).charAt(2);
if (c=='/'||c=='*') {
rp[j++]=s.get(i+1).split(",")[1];
rp[j++]=s.get(i).split(",")[1];
i+=2;
} else stc.offer(s.get(i++).split(",")[1]);
} else rp[j++]=s.get(i++).split(",")[1];
}
while (!stc.isEmpty())rp[j++]=stc.poll();
for (int i=0;i if (rp[i].length()==1&&!Character.isDigit(rp[i].charAt(0))) {
int b=Integer.parseInt(stc.poll());
int a=Integer.parseInt(stc.poll());
char c=rp[i].charAt(0);
if (c=='*') stc.offer(Integer.toString(a*b));
if (c=='/') stc.offer(Integer.toString(a/b));
if (c=='+') stc.offer(Integer.toString(a+b));
if (c=='-') stc.offer(Integer.toString(a-b));
} else stc.offer(rp[i]);
}
return Integer.parseInt(stc.poll());
}
public static void main(String[] args) {
int[] A={1,1,2,2,1};
System.out.println(new MaxNumberWithOperators().maxResult(A));
}
public int maxResult(int[]A) {
int[]rel=new int[1];rel[0]=Integer.MIN_VALUE;
dfs(A, new int[A.length-1], 0, rel);
return rel[0];
}
void dfs(int[]A, int[]op, int i, int[] max) {
if (i==op.length) {
StringBuilder s=new StringBuilder();
s.append(Integer.toString(A[0]));
char[] opc=new char[4];
opc[0]='+';opc[1]='-';opc[2]='*';opc[3]='/';
for (int j=0;j
s.append(A[j+1]);
}
max[0]=Math.max(max[0], eval(s.toString()));
return;
}
for (int j=0;j<4;++j){
op[i]=j;
dfs(A, op, i+1, max);
}
}
int eval(String s) {
List
//0 : operator 1: numbers
for (int i=0, n=s.length();i
int j=i+1;
while (j
i=j;
} else {
rel.add("0,"+s.substring(i,i+1));
++i;
}
}
return evaluateInorder(rel);
}
int evaluateInorder(List
String[] rp=new String[s.size()];
Queue
int j=0;
for (int i=0;i
char c=s.get(i).charAt(2);
if (c=='/'||c=='*') {
rp[j++]=s.get(i+1).split(",")[1];
rp[j++]=s.get(i).split(",")[1];
i+=2;
} else stc.offer(s.get(i++).split(",")[1]);
} else rp[j++]=s.get(i++).split(",")[1];
}
while (!stc.isEmpty())rp[j++]=stc.poll();
for (int i=0;i
int b=Integer.parseInt(stc.poll());
int a=Integer.parseInt(stc.poll());
char c=rp[i].charAt(0);
if (c=='*') stc.offer(Integer.toString(a*b));
if (c=='/') stc.offer(Integer.toString(a/b));
if (c=='+') stc.offer(Integer.toString(a+b));
if (c=='-') stc.offer(Integer.toString(a-b));
} else stc.offer(rp[i]);
}
return Integer.parseInt(stc.poll());
}
public static void main(String[] args) {
int[] A={1,1,2,2,1};
System.out.println(new MaxNumberWithOperators().maxResult(A));
}
相关阅读
大家怎么收到的OPT receipt number?大家学生的时候找工作跟自己老板讲吗?请教大家关于感谢信的回复这两天L总是给我match G的Partner Technology Manager (Mobile), Wallet一般挑几个有兴趣的职位给HR比较合适运车的话,车上可以放几个大行李箱么?面试题:cross process singleton怎么实现?想问下ICC的问题求NSC OPT加急的fax 和email看起来似乎background check很慢不想做题了咋办呢?同学们来帮忙解个题吧~国内码工 VS 花旗码工,请各位大牛数一数两地 IT 精英不同招各种软硬件工程师、生产工程师、会计、市场、供应链PwC advisory-operation consulting 怎么样今天灌水不踊跃,出道题吧最近有材料/化工的面试Du Pont吗?H1b名额还能撑几天?Quantcast这个公司怎么样呀?如果今年h1b满了,是不是明年10月份才能开始新的h1b工作