c# - How can I get the sum of numbers from a diagonal line? -
here sum diagonal top-left bottom-right:
public int sumardiagonal() { int x = 0; (int f = 0; f < filas; f++) { (int c = 0; c < columnas; c++) { if (f == c) { x += m[f,c]; } } } return x; }
how can go top-right bottom-left?
your original code 2 nested loops not efficient. better this:
public int sumardiagonal() { int x = 0; (int = 0; < math.min(filas,columnas); ++i) x += m[i,i]; return x; } public int sumarantidiagonal() { int x = 0; (int = 0; < math.min(filas,columnas); ++i) x += m[filas - 1 - i,i]; return x; }
Comments
Post a Comment