Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
3510 | 未知用户 | 螺旋矩阵 | C++ | 解答错误 | 0 | 0 MS | 296 KB | 724 | 2019-12-19 20:50:04 |
#include<iostream> int main( ){ int i, j, a[100][100]={0}, n, k = 1; printf("please input a number n:"); while(scanf("%d", &n)!=EOF){ k = 1; for (i = 1; i <= n / 2; ++ i){ for (j = i; j <= n - i; ++ j) /* 左侧 */ a[i][j] = k ++; for (j = i; j <= n - i; ++ j) /* 下方 */ a[j][n + 1 - i] = k ++; for (j = n - i + 1; j >= i + 1; j = j - 1) /* 右侧 */ a[n + 1 - i][j] = k ++; for (j = n - i + 1; j >= i + 1; j = j - 1) /* 上方 */ a[j][i] = k ++; } if(n != 1){ for (i = 1; i <= n; ++ i){ for (j = 1; j <= n; ++ j) printf("%5d", a[i][j]); printf("\n"); } } else printf("%5d\n",n); printf("please input a number n:"); } return 0; }