解题思路:
int [][] arr = new int[n][m];
int i = 0, j = -1, temp = 1;
while (n * m > 0)
{
for (int p = 0; p < m; p++)//从左自右
arr[i][j=j+1] = temp++;
n--;
if (n * m == 0) break;
for (int p = 0; p < n; p++)//从上自下
arr[i=i+1][j] = temp++;
m--;
if (n * m == 0) break;
for (int p = 0; p < m; p++)//从右自左
arr[i][j=j-1] = temp++;
n--;
if (n * m == 0) break;
for (int p = 0; p < n; p++)//从下自上
arr[i=i-1][j] = temp++;
m--;
}
相关题目:
【洛谷】P5731