#define M 4
#define N 3
#define Z M+N-1
int main(int argc, char const *argv[]) {
int mat[M][N] = {
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9},
{10, 11, 12}
};
int result[Z] = {0};
// z varia de 0 a M+N-1, exclusive
// Indica a linha na matriz resultados:
for (int z = 0; z < Z; z++) {
// i varia de 0 a M, exclusive
// Indica a linha na matriz mat:
for (int i = 0; i < M; i++) {
// j varia de 0 a N, exclusive
// Indica a coluna na matriz mat:
for (int j = 0; j < N; j++) {
// Se a soma da posição for igual a z:
if (i + j == z) {
// Incrementa a soma:
result[z] += mat[i][j];
}
}
}
// Exibe o resultado da soma:
}
return 0;
}
I2RlZmluZSBNIDQKI2RlZmluZSBOIDMKI2RlZmluZSBaIE0rTi0xCgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogICAgaW50IG1hdFtNXVtOXSA9IHsKICAgICAgICB7IDEsICAyLCAgM30sCiAgICAgICAgeyA0LCAgNSwgIDZ9LAogICAgICAgIHsgNywgIDgsICA5fSwKICAgICAgICB7MTAsIDExLCAxMn0KICAgIH07CgogICAgaW50IHJlc3VsdFtaXSA9IHswfTsKCiAgICAvLyB6IHZhcmlhIGRlIDAgYSBNK04tMSwgZXhjbHVzaXZlCiAgICAvLyBJbmRpY2EgYSBsaW5oYSBuYSBtYXRyaXogcmVzdWx0YWRvczoKICAgIGZvciAoaW50IHogPSAwOyB6IDwgWjsgeisrKSB7CgogICAgICAgIC8vIGkgdmFyaWEgZGUgMCBhIE0sIGV4Y2x1c2l2ZQogICAgICAgIC8vIEluZGljYSBhIGxpbmhhIG5hIG1hdHJpeiBtYXQ6CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBNOyBpKyspIHsKCiAgICAgICAgICAgIC8vIGogdmFyaWEgZGUgMCBhIE4sIGV4Y2x1c2l2ZQogICAgICAgICAgICAvLyBJbmRpY2EgYSBjb2x1bmEgbmEgbWF0cml6IG1hdDoKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBOOyBqKyspIHsKCiAgICAgICAgICAgICAgICAvLyBTZSBhIHNvbWEgZGEgcG9zacOnw6NvIGZvciBpZ3VhbCBhIHo6CiAgICAgICAgICAgICAgICBpZiAoaSArIGogPT0geikgewoKICAgICAgICAgICAgICAgICAgICAvLyBJbmNyZW1lbnRhIGEgc29tYToKICAgICAgICAgICAgICAgICAgICByZXN1bHRbel0gKz0gbWF0W2ldW2pdOwoKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgIH0KCiAgICAgICAgfQoKICAgICAgICAvLyBFeGliZSBvIHJlc3VsdGFkbyBkYSBzb21hOgogICAgICAgIHByaW50ZigiJTJkXG4iLCByZXN1bHRbel0pOwoKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==