## 5个编程问题，第五题

https://www.oschina.net/news/87258/solve-five-problems-in-5minutes

123-4-5-6-7+8-9
123+45-67+8-9
1+23-4+5+6+78-9
1+2+34-5+67-8+9
1+23-4+56+7+8+9
12+3-4+5+67+8+9
1+2+3-4+5+6+78+9
123+4-5+67-89
12+3+4+5-6-7+89
12-3-4+5-6+7+89
123-45-67+89

class Node {
private int value1;
private String operator;
private int value2;
private int factor;
private List<Node> childNodes;
private Node parent;
public Node(int result,int maxFactor) {
this(result,null,0,maxFactor,null);
}
private Node(int value1,String operator,int value2,int factor,Node parent) {
if(operator==null) {
this.value1 = value1;
List<Node> childNodesTemp = new ArrayList<Node>();
this.childNodes = childNodesTemp;
} else {
this.value1 = value1;
this.operator = operator;
this.value2 = value2;
this.factor = factor;
this.parent = parent;
if(factor>2) {
if("".equals(operator)) {
List<Node> childNodesTemp = new ArrayList<Node>();
this.childNodes = childNodesTemp;
}else {
List<Node> childNodesTemp = new ArrayList<Node>();
this.childNodes = childNodesTemp;
}
} else if(factor==2) {
if(isRight(this)) {
print(this.getValue1(),this,"");
}
}
}
}
private void print(int begin,Node node,String exp) {
if(node!=null&&node.getOperator()!=null) {
if(begin!=0) {
exp += begin;
}
if(!"".equals(node.getOperator())) {
exp += node.getOperator()+node.getValue2();
}
print(0,node.getParent(),exp);
} else {
System.out.println(exp);
}
}
private boolean isRight(Node parent) {
if((!"".equals(parent.getOperator())&&parent.getValue1()==1)||("".equals(parent.getOperator())&&parent.getValue1()==parent.getValue2()+(int)Math.pow(10, 1+Math.floor(Math.log10(parent.getValue2()))))) {
return true;
}
return false;
}
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
public int getFactor() {
return factor;
}
public void setFactor(int factor) {
this.factor = factor;
}
public List<Node> getChildNodes() {
return childNodes;
}
public void setChildNodes(List<Node> childNodes) {
this.childNodes = childNodes;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
}

0

c

0
u

``````	public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
searchExprs(list, 2);
}

public static void searchExprs(List<Integer> list, int n) {
int size = list.size();

if (n < 9) {
searchExprs(list, n + 1);
} else if (sum(list) == 100) {
print(list);
}
list.remove(size);

if (n < 9) {
searchExprs(list, n + 1);
} else if (sum(list) == 100) {
print(list);
}
list.remove(size);

int lastIndex = size - 1;
int val = list.get(lastIndex);
if (val > 0) {
list.set(lastIndex, list.get(lastIndex) * 10 + n);
} else {
list.set(lastIndex, list.get(lastIndex) * 10 - n);
}
if (n < 9) {
searchExprs(list, n + 1);
} else if (sum(list) == 100) {
print(list);
}
}

private static int sum(List<Integer> list) {
return list.stream().collect(Collectors.summingInt(Integer::intValue));
}

private static void print(List<Integer> list) {
StringBuilder sb = new StringBuilder();
for (Integer e : list) {
if (sb.length() == 0) {
sb.append(e);
} else if (e > 0) {
sb.append(" + ").append(e);
} else {
sb.append(" - ").append(-e);
}
}
System.out.println(sb);
}
``````

0

0

1、提供最新的视频学习资源

2、给初学者提供学习线路以及对应学习资源

3、培训机构15000元的学习课程，我们免费送你200G+视频学习资源

4、CSDN网络课堂2000-3000元的课程，我们就是一百度云链接。

V薪关注【八戒程序猿】就够了，并没有培训班那么贵。

0

0
c