728x90
반응형
문제 : https://codeup.kr/problem.php?id=1506
문제 설명
평소 호기심이 많은 남호는 정보고 사이트에 있는 달팽이 배열 문제를 다르게 만들고 싶었다.
입력 예시와 출력 예시를 참고 하여 n을 입력 받아 출력하는 프로그램을 작성하시오.
# include <stdio.h>
int main()
{
int arr[101][101] = {0,};
int i,j,n,number=1,x=1,y=0;
scanf("%d",&n);
for(i = 0; i<=n; i++)
{
arr[0][i] = arr[n+1][i] = 1;
arr[i][0] = arr[i][n+1] = 1;
}
while(number <= n*n)
{
while(arr[x][y+1] == 0)
{
y++;
arr[x][y] = number++;
}
while(arr[x+1][y] == 0)
{
x++;
arr[x][y] = number++;
}
while(arr[x][y-1] == 0)
{
y--;
arr[x][y] = number++;
}
while(arr[x-1][y] == 0)
{
x--;
arr[x][y] = number++;
}
}
for(i = 1; i<=n; i++)
{
for(j = 1; j<=n; j++)
{
printf("%d ",arr[j][i]);
}
printf("\n");
}
return 0;
}
728x90
반응형
최근댓글