【发布时间】:2014-08-03 21:00:07
【问题描述】:
其基本思想是,在一个星球上存在三种不同的物种,这三种物种中只有两种可以聚集在一起生育,结果就是这对物种来说是第三物种的两个新对象。出生,例如我们有a b和c,物种a和b聚集在一起,让2 c新成员出生。
就像:1a 1b 和 1c (对不起语言)
当 a 和 b 想要生孩子时,他们走到一起死了,但有了两个孩子,这些新孩子来自 c 物种,所以结果是:
0a 0b 和 3c
在这个案例中,我们说行星成功并且 c 是主导物种。但是当我有 3a 4b 2c 时,我需要看看这三个物种中的任何一个能否在这个星球上取得成功。
我认为我可以使用递归解决方案来做到这一点,但我总是得到以下错误:
Exception in thread "main" java.lang.StackOverflowError
在我尝试使用递归的函数中。
这是我的代码,我知道有问题,但我不知道是什么。
公共类 Automata2 {
public static void main(String[] args) {
int N = 2;
while (N <= 14) {
partition(N);
N++;
}
}
public static void partition(int N) {
int n1,n2,n3;
for(n1=0;n1<=N;n1++){
for(n2=0;n2<=N;n2++){
for(n3=0;n3<=N;n3++){
if((n1+n2+n3)==N){
strPlanetA(n1, n2, n3);
}
}
}
}
}
public static void strPlanetA(int a, int b, int c){
int a2 = a, b2 = b, c2 = c;
while (!(a2!=0 && b2==0 && c2==0)) {
a2=a2+2;
b2--;
c2--;
if (a2==a && b2==b && c2==c) {
System.out.println("Not Fail");
}
if (a2!=0 && b2==0 && c2==0) {
System.out.println("Fail in A");
}
strPlanetA(a2, b2, c2);
}
}
}
分区只是为了得到这个星球上所有可能的人口,我需要看看飞机是否在一个人口从2到14的星球上成功。
【问题讨论】: