본문 바로가기

IT전공관련

배열 달팽이

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
#include<stdio.h>
 
int main() {
    int arr[10][10]; 
    int direction = 1;
    int count = 1;
    int max = 10;
    int a = 0, b = -1;
 
    while (max > 0) {
        for (int i = 0; i < max; i++) {
            b = b + direction;
            arr[a][b] = count;
            count++;
        }
        max--;
        for (int i = 0; i < max; i++) {
            a = a + direction;
            arr[a][b] = count;
            count++;
        }
        direction *= -1;
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            printf("%4d", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}
cs


'IT전공관련' 카테고리의 다른 글

strcpy, strlen, strcmp 총정리 보고서  (0) 2017.04.02
배열 숙제  (0) 2017.03.29
함수 정리 보고서  (0) 2017.03.28
배열 보고서  (0) 2017.03.27
별찍기  (1) 2017.03.26