我的代码被削减,我不明白为什么

时间:2017-05-03 02:19:01

标签: java arrays matrix multidimensional-array indexoutofboundsexception

public static int EncontrarCadena(char arrayTriangulo[][]) {
        int aux = 0;
        int area = 0;
        int cantidadFilas = arrayTriangulo.length;
        for (int i = 0; i < cantidadFilas; i++) {
            int cantidadColumnas = arrayTriangulo[i].length;
            for (int k = 0; k < cantidadColumnas; k++) {
                if (arrayTriangulo[i][k] == '-') {
                    aux++;
                } else if (arrayTriangulo[i][k] == '#') {
                    aux = 0;
                }
                if (aux%2!=0) {
                    int aux2 = ((aux + 1) / 2) - 1;
                    int aux4 = ((aux + 1) / 2) - 1;
                    int aux3 = aux;
                    int aux5 = aux3;
                    int a = 0;
                    int k2 = k;
                    while (aux2 != 0) {
                        for (int i2 = i + 1; i2 < cantidadFilas; i2++) {
                            for (int j = k2 - aux5 + 1; j < 2 * aux2 - 1; j++) {
                                if (arrayTriangulo[i2][j] == '-') {
                                    aux3++;
                                } else if (arrayTriangulo[i2][j] == '#') {
                                    break;
                                }
                            }
                            if (aux3 == aux + 2 * aux4 - 1) {
                                a++;
                                k2 = k2 + aux3 + 1;
                                aux2 = aux2 - 1;
                                aux4 = aux4 + 2 * ((aux + 1) / 2) - a;
                                aux5 = aux3;
                            }
                        }
                    }
                    if (aux2 == 0 && area < aux3) {
                        area = aux3;
                    }
                }
            }
            aux = 0;
        }
        return area;
    }
public static void main(String[] args) {
    int filas = 5;
    int columnas = 2 * filas - 1;
    char arrayTriangulo[][] = new char[filas][columnas];
    for (int i = (filas - 1); i > -1; i--) {
        int aux = 2 * (filas - i) - 1;
        for (int k = 0; k < columnas; k++) {
            if (aux < 2 * filas - 1) {
                if (k > (2 * filas - (aux)) / 2 && k < (columnas - (aux)) / 2 + aux) {
                    arrayTriangulo[i][k] = '-';

                } else {
                    arrayTriangulo[i][k] = '-';
                }
            }
        }
    }
    int area = EncontrarCadena(arrayTriangulo);
    System.out.println(area);
}

因此,此代码在具有以下格式的二维数组中获得三角形

---------
 ------- 
  -----  
   ---   
    -    

它应该计算它内部较小三角形的面积,然后返回最重要三角形的部分,它们将是25(所有' - '的总和)。事情是代码不起作用,我不明白为什么。

while (aux2 != 0) {
                            for (int i2 = i + 1; i2 < cantidadFilas; i2++) {
                                for (int j = k2 - aux5 + 1; j < 2 * aux2 - 1; j++) {
                                    if (arrayTriangulo[i2][j] == '-') {
                                        aux3++;
                                    } else if (arrayTriangulo[i2][j] == '#') {
                                        break;
                                    }
                                }
                                if (aux3 == aux + 2 * aux4 - 1) {
                                    a++;
                                    k2 = k2 + aux3 + 1;
                                    aux2 = aux2 - 1;
                                    aux4 = aux4 + 2 * ((aux + 1) / 2) - a;
                                    aux5 = aux3;
                                }
                            }
                        }
如果我没有弄错的话

在这部分失败

0 个答案:

没有答案