如何递归 authomata Strange Planet 练习? [英] How to recursive the authomata Strange Planet exercise?
问题描述
这个的基本思想是在一个星球上有三种不同的物种,这三个物种中只有两个可以一起繁殖,结果是这个物种死亡了第三个物种的两个新对象出生,例如我们有 ab 和 c,物种 a 和 b 组合在一起,让 2 c 新成员出生.
The basic idea of this is that in a planet there is three diferent kinds of species, only two of the three species can came together to procreate, and the result is that this to species die a two new sobjects of the third species born, for example we have a b and c, and the species a and b came together and let 2 c new members born.
就像:1a 1b 和 1c(抱歉语言不通)
It is like: 1a 1b and 1c (Sorry for the lenguage)
当 a 和 b 想要孩子时,他们走到一起死了,但有两个孩子,这些新孩子来自物种 c,所以结果是:
When a and b want to have kids they came together and die but have two kids, these new kids are from the species c, so the result is:
0a 0b 和 3 c
0a 0b and 3 c
在这个案例中,我们说行星成功了,c 是占主导地位的 spcecies.但是当我有 3a 4b 2c 并且我需要看看这三个物种中的任何一个是否可以在这个星球上成功.
In this casie we said that the planet succed and c is the dominating spcecies. But when I have 3a 4b 2c and I need to see if any of the three species could succed in the planet.
我认为我可以用递归解决方案来做到这一点,但我总是得到以下错误:
I thought that I could do it with a recursive solution, but I get always the error of:
Exception in thread "main" java.lang.StackOverflowError
在我尝试使用递归的函数中.
In the function I am trying to use recursive.
这是我的代码,我知道有问题但我不知道是什么.
This is my code, I know something is wrong but I don´t know what.
公共类 Automata2 {
public class 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 的星球上成功.
The partition is only to get all the population posible in the planet, I need to see if the plane succed in a planet with population from 2 until 14.
推荐答案
基本上我想要实现的是这个,应该有更好的方法来改进这个.
Basically what I wanted to achive was this, there shoul be better ways on how to improve this.
public class Automata2 {
/**
* @param args the command line arguments
*/
static int cont = 0;
public static void main(String[] args) {
int N = 2;
while (N <= 14) {
partition(N);
N++;
}
System.out.println("Cantidad de Respuestas= " + cont);
}
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){
if (strPlanetA(n1, n2, n3) == 1) {
cont++;
System.out.print("A falla en: ");
System.out.print(n1+"-");
System.out.print(n2+"-");
System.out.println(n3);
}
if (strPlanetB(n1, n2, n3) == 1) {
cont++;
System.out.print("B falla en: ");
System.out.print(n1+"-");
System.out.print(n2+"-");
System.out.println(n3);
}
if (strPlanetC(n1, n2, n3) == 1) {
cont++;
System.out.print("C falla en: ");
System.out.print(n1+"-");
System.out.print(n2+"-");
System.out.println(n3);
}
}
}
}
}
}
public static int strPlanetA(int a2, int b2, int c2){
if((a2!=0 && b2==0 && c2==0)||(c2 == b2)){
return 1;
}
if (b2>0 && c2>0) {
a2=a2+2;
b2--;
c2--;
if (a2!=0 && b2==0 && c2==0) {
return 1;
}
else{
strPlanetA(a2, b2, c2);
}
}
return 3;
}
public static int strPlanetB(int a2, int b2, int c2){
if((a2==0 && b2!=0 && c2==0)||(c2 == a2)){
return 1;
}
if (a2>0 && c2>0) {
a2--;
b2=b2+2;
c2--;
if (a2==0 && b2!=0 && c2==0) {
return 1;
}
else{
strPlanetB(a2, b2, c2);
}
}
return 3;
}
public static int strPlanetC(int a2, int b2, int c2){
if((a2==0 && b2==0 && c2!=0)||(a2 == b2)){
return 1;
}
if (a2>0 && b2>0){
a2--;
b2--;
c2=c2+2;
if (a2==0 && b2==0 && c2!=0) {
return 1;
}
else{
return strPlanetC(a2, b2, c2);
}
}
return 3;
}
}
感谢您对递归的推荐,这完全是一团糟.
Thanks for the recomendation aobve the recursive, it was totally a mess.
这篇关于如何递归 authomata Strange Planet 练习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!