#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int n,m;
scanf("%d %d", &n, &m);
int **arr = (int**)malloc(n*sizeof(int*));
for(int i=0; i<n; i++){
arr[i] = (int*)malloc(m*sizeof(int));
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
arr[i][j] = 0;
}
}
int a=1;
int i=0;
int j=0;
while(a<=n*m){
while(j<m && arr[i][j] == 0){
arr[i][j] = a++;
j++;
}
j--;
i++;
if(a>n*m) break;
while(i<n && arr[i][j] == 0){
arr[i][j] = a++;
i++;
}
i--;
j--;
if(a>n*m) break;
while(j>=0 && arr[i][j] == 0){
arr[i][j] = a++;
j--;
}
j++;
i--;
if(a>n*m) break;
while(i>=0 && arr[i][j] == 0){
arr[i][j] = a++;
i--;
}
i++;
j++;
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
printf("%3d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: