推荐先看我的一篇介绍Set去重的博文地址是
http://blog.csdn.net/bug_moving
看了这个之后,再来看下面的程序基本就能看懂了
题目
我也不太记得,因为是朋友给我口述的,然后给了我一个截图,看了图片大致也能知道题目要我们做什么
package yn;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
class Composition extends ArrayList<Integer> {
@Override
public boolean equals(Object other) {
Composition comp = (Composition) other;
Collections.sort(this);
Collections.sort(comp);
if (this.isEmpty() || comp.isEmpty() || this.size() != comp.size())
return false;
for (int i = 0; i < this.size(); i++)
if (this.get(i) != comp.get(i))
return false;
return true;
}
@Override
public int hashCode() {
return 0;
}
}
/**
* 用递归法,比如2=1+1,3=1+(2的所有组成法),5需要分解1+4,2+3,因为3+2和2+3是一样的,for循环只要到i<=n/2就够了.
然后就是剔除1+1+2和1+2+1的情况,继承set的特性重写了Composition(每个拆分的方式)的equals.
懒得读取n值了,直接在main里面赋值给n
* @author yxx
*
*/
public class lhy {
public static void main(String[] args) {
int n = 5;
System.out.println(toStr(calc(n)));
}
public static Set<Composition> calc(int n) {
Set<Composition> possibility = new HashSet<Composition>();
Composition composition = new Composition();
switch (n) {
case 1:
composition.add(1);
possibility.add(composition);
return possibility;
case 2:
composition.add(1);
composition.add(1);
possibility.add(composition);
return possibility;
default:
for (int i = 1; i <= n / 2; i++) {
composition = new Composition();
composition.add(i);
composition.add(n - i);
possibility.add(composition);
if (i <= n - i) {
Set<Composition> partial_pos = calc(n - i);
for (Composition pos : partial_pos) {
pos.add(i);
possibility.add(pos);
}
}
}
return possibility;
}
}
public static String toStr(Set<Composition> possibility) {
String str = "total : " + possibility.size() + "\n";
for (Composition pos : possibility)
str += toStr(pos);
return str;
}
public static String toStr(Composition composition) {
String str = composition.get(0) + "";
for (int i = 1; i < composition.size(); i++)
str += (" + " + composition.get(i));
str += "\n";
return str;
}
}
运行截图
至此这个问题基本解决,我也没有考虑效率问题啥,也不知道是不是效率超标了,先就这样吧。