1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <stdio.h> void print_layer_top_right(int a[][4], int x1, int y1, int x2, int y2); void print_layer_bottom_left(int a[][4], int x1, int y1, int x2, int y2); int main(void) { int a[5][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}, {17,18,19,20} }; print_layer_top_right(a,0,0,3,4); } // // prints the top and right shells of the matrix // void print_layer_top_right(int a[][4], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print the row for(i = x1; i<=x2; i++) { printf("%d,", a[y1][i]); } //print the column for(j = y1 + 1; j <= y2; j++) { printf("%d,", a[j][x2]); } // see if we have more cells left if(x2-x1 > 0) { // 'recursively' call the function to print the bottom-left layer print_layer_bottom_left(a, x1, y1 + 1, x2-1, y2); } } // // prints the bottom and left shells of the matrix // void print_layer_bottom_left(int a[][4], int x1, int y1, int x2, int y2) { int i = 0, j = 0; //print the row of the matrix in reverse for(i = x2; i>=x1; i--) { printf("%d,", a[y2][i]); } // print the last column of the matrix in reverse for(j = y2 - 1; j >= y1; j--) { printf("%d,", a[j][x1]); } if(x2-x1 > 0) { // 'recursively' call the function to print the top-right layer print_layer_top_right(a, x1+1, y1, x2, y2-1); } } |
I2luY2x1ZGUgPHN0ZGlvLmg+CnZvaWQgcHJpbnRfbGF5ZXJfdG9wX3JpZ2h0KGludCBhW11bNF0sIGludCB4MSwgaW50IHkxLCBpbnQgeDIsIGludCB5Mik7CnZvaWQgcHJpbnRfbGF5ZXJfYm90dG9tX2xlZnQoaW50IGFbXVs0XSwgaW50IHgxLCBpbnQgeTEsIGludCB4MiwgaW50IHkyKTsKCmludCBtYWluKHZvaWQpCnsKICAgICAgIGludCBhWzVdWzRdID0gewogICAgICAgICAgICAgICAgICAgICAgIHsxLDIsMyw0fSwKICAgICAgICAgICAgICAgICAgICAgICB7NSw2LDcsOH0sCiAgICAgICAgICAgICAgICAgICAgICAgezksMTAsMTEsMTJ9LAogICAgICAgICAgICAgICAgICAgICAgIHsxMywxNCwxNSwxNn0sCiAgICAgICAgICAgICAgICAgICAgICAgezE3LDE4LDE5LDIwfQogICAgICAgICAgICAgICAgICAgfTsKCiAgICAgICBwcmludF9sYXllcl90b3BfcmlnaHQoYSwwLDAsMyw0KTsKfQoKLy8KLy8gcHJpbnRzIHRoZSB0b3AgYW5kIHJpZ2h0IHNoZWxscyBvZiB0aGUgbWF0cml4Ci8vCnZvaWQgcHJpbnRfbGF5ZXJfdG9wX3JpZ2h0KGludCBhW11bNF0sIGludCB4MSwgaW50IHkxLCBpbnQgeDIsIGludCB5MikKewogICAgICBpbnQgaSA9IDAsIGogPSAwOwoKICAgICAgLy8gcHJpbnQgdGhlIHJvdwogICAgICBmb3IoaSA9IHgxOyBpPD14MjsgaSsrKQogICAgICB7CiAgICAgICAgIHByaW50ZigiJWQsIiwgYVt5MV1baV0pOwogICAgICB9CiAKICAgICAgLy9wcmludCB0aGUgY29sdW1uICAgICAgICAgCiAgICAgIGZvcihqID0geTEgKyAxOyBqIDw9IHkyOyBqKyspICAgICAgICAgCiAgICAgIHsKICAgICAgICAgcHJpbnRmKCIlZCwiLCBhW2pdW3gyXSk7CiAgICAgIH0KCiAgICAgIC8vIHNlZSBpZiAgd2UgaGF2ZSBtb3JlIGNlbGxzIGxlZnQgICAgICAgICAKICAgICAgaWYoeDIteDEgPiAwKQogICAgICB7CiAgICAgICAgICAvLyAncmVjdXJzaXZlbHknIGNhbGwgdGhlIGZ1bmN0aW9uIHRvIHByaW50IHRoZSBib3R0b20tbGVmdCBsYXllcgogICAgICAgICAgcHJpbnRfbGF5ZXJfYm90dG9tX2xlZnQoYSwgeDEsIHkxICsgMSwgeDItMSwgeTIpOwogICAgICB9Cn0KCi8vCi8vIHByaW50cyB0aGUgYm90dG9tIGFuZCBsZWZ0IHNoZWxscyBvZiB0aGUgbWF0cml4Ci8vCnZvaWQgcHJpbnRfbGF5ZXJfYm90dG9tX2xlZnQoaW50IGFbXVs0XSwgaW50IHgxLCBpbnQgeTEsIGludCB4MiwgaW50IHkyKQp7CiAgICAgICBpbnQgaSA9IDAsIGogPSAwOwoKICAgICAgIC8vcHJpbnQgdGhlIHJvdyBvZiB0aGUgbWF0cml4IGluIHJldmVyc2UKICAgICAgIGZvcihpID0geDI7IGk+PXgxOyBpLS0pCiAgICAgICB7CiAgICAgICAgICAgICAgIHByaW50ZigiJWQsIiwgYVt5Ml1baV0pOwogICAgICAgfQoKICAgICAgIC8vIHByaW50IHRoZSBsYXN0IGNvbHVtbiBvZiB0aGUgbWF0cml4IGluIHJldmVyc2UKICAgICAgIGZvcihqID0geTIgLSAxOyBqID49IHkxOyBqLS0pCiAgICAgICB7CiAgICAgICAgICAgICAgIHByaW50ZigiJWQsIiwgYVtqXVt4MV0pOwogICAgICAgfQoKICAgICAgIGlmKHgyLXgxID4gMCkKICAgICAgIHsKICAgICAgICAgICAgICAgLy8gJ3JlY3Vyc2l2ZWx5JyBjYWxsIHRoZSBmdW5jdGlvbiB0byBwcmludCB0aGUgdG9wLXJpZ2h0IGxheWVyCiAgICAgICAgICAgICAgIHByaW50X2xheWVyX3RvcF9yaWdodChhLCB4MSsxLCB5MSwgeDIsIHkyLTEpOwogICAgICAgfQp9Cg==
-
upload with new input
-
result: Success time: 0.01s memory: 2724 kB returned value: 0
3;3;123456789
1,2,3,4,8,12,16,20,19,18,17,13,9,5,6,7,11,15,14,10,
-
result: Success time: 0s memory: 2724 kB returned value: 0
1,2,3,4,8,12,16,20,19,18,17,13,9,5,6,7,11,15,14,10,



