用java中vector类,用递归方法求解幂集
时间: 2024-05-11 07:17:38 浏览: 94
好的,这个问题可以回答。以下是使用递归方法求解幂集的Java代码示例:
```
import java.util.Vector;
public class PowerSet {
public static Vector<Vector<Integer>> getPowerSet(Vector<Integer> v) {
Vector<Vector<Integer>> powerSet = new Vector<Vector<Integer>>();
powerSet.add(new Vector<Integer>());
for (int i = 0; i < v.size(); i++) {
Vector<Vector<Integer>> newPowerSet = new Vector<Vector<Integer>>();
for (Vector<Integer> subset : powerSet) {
newPowerSet.add(subset);
Vector<Integer> newSubset = new Vector<Integer>(subset);
newSubset.add(v.get(i));
newPowerSet.add(newSubset);
}
powerSet = newPowerSet;
}
return powerSet;
}
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(1);
v.add(2);
v.add(3);
Vector<Vector<Integer>> powerSet = getPowerSet(v);
System.out.println("Power set of " + v + ": ");
for (Vector<Integer> subset : powerSet) {
System.out.println(subset);
}
}
}
```
阅读全文