- //回旋数组
- #import < Foundation / Foundation.h > #define SIZE 7 int main(int argc, const char * argv[]) {@autoreleasepool {
- int array[SIZE][SIZE];
- int direction = 0;
- int i,
- j,
- k;
- for (i = 1, j = 0, k = 0; i <= SIZE * SIZE; i++) {
- array[j][k] = i;
- if ((j + k == SIZE - 1)) {
- if (j > k) direction = 1;
- else direction = 3;
- }
- if ((j == k) && (j >= SIZE / 2)) {
- direction = 2;
- }
- if ((j == k - 1) && (j < SIZE / 2)) {
- direction = 0;
- }
- switch (direction) {
- case 0:
- //DOWN
- j++;
- break;
- case 1:
- //right
- k++;
- break;
- case 2:
- //UP
- j--;
- break;
- case 3:
- //LEFT
- k--;
- break;
- }
- }
- for (int i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++) {
- if (array[i][j] < 10) {
- printf("0%d ", array[i][j]);
- } else {
- printf("%d ", array[i][j]);
- }
- }
- printf("\n");
- }
- }
- return 0;
- }
来源: http://lib.csdn.net/snippet/objective-c/48767