Skip to content

Instantly share code, notes, and snippets.

@jwon0615
Created March 22, 2018 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwon0615/7b3f4fda3d0c2b872fa3dda7f1f3d821 to your computer and use it in GitHub Desktop.
Save jwon0615/7b3f4fda3d0c2b872fa3dda7f1f3d821 to your computer and use it in GitHub Desktop.
1505 : 2차원 배열 채우기 3(달팽이 배열)
#include <stdio.h>
int main(){
int i,j,n,diff,cnt=1;
int s=0; e=-1;
scanf("%d", &n);
diff=n;
while(cnt<=n*n){
for(i=1;i<-diff;i++) arr[s][++e]=cnt++;
diff--;
for(i=1;i<-diff;i++) arr[++s][e]=cnt++;
diff--;
for(i=1;i<-diff;i++) arr[s][--e]=cnt++;
diff--;
for(i=1;i<-diff;i++) arr[--s][e]=cnt++;
}
for(i=0;i<n;i++)
for(j=0; j<n; j++)
printf("%d", arr[i][j];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment