magic square
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 | #include <stdio.h> #define MAX_SIZE 4 int main (void) { int array[MAX_SIZE][MAX_SIZE] = { 0, }; // diagonal line int minimum = 0, maximum = 0; for (minimum = 0, maximum = MAX_SIZE - 1; minimum < MAX_SIZE; minimum++, maximum--) { array[minimum][minimum] = -1; array[minimum][maximum] = -1; } // insert a number int increase = 1, decrease = MAX_SIZE * MAX_SIZE; int row = 0, column = 0; for (column = 0; column < MAX_SIZE; column++) { for (row = 0; row < MAX_SIZE; row++) { if (array[column][row] == -1) array[column][row] = increase; else array[column][row] = decrease; increase++; decrease--; } } // print for (column = 0; column < MAX_SIZE; column++) { for (row = 0; row < MAX_SIZE; row++) { printf ("%d\t", array[column][row]); } printf ("\n"); } return 0; } | cs |
0 개의 댓글:
댓글 쓰기