Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
31154 陈路垚 螺旋矩阵 C++ 解答错误 0 0 MS 248 KB 1017 2022-01-29 10:26:25

Tests(0/10):


#include <iostream> #include <string.h> using namespace std; int **snake(int n) { int i, j; int c = 0; int **num = new int *[n]; for(i = 0; i < n; i++) { num[i] = new int [n]; memset(num[i],0,sizeof(int)*n); } for(i = 0; i < n/2; i++) { for(j = i; j < n-i; j++) { c++; *(*(num+i)+j) = c; } for(j = i+1; j < n-i; j++) { c++; *(*(num+j)+n-i-1) = c; } for(j = n-i-2; j >= i; j--) { c++; *(*(num+n-i-1)+j) = c; } for(j = n-i-2; j > i; j--) { c++; *(*(num+j)+i) = c; } } if(n % 2 == 1) *(*(num+n/2)+n/2) = n*n; return num; } int main() { int t; cin >> t; while(t--) { int n, i, j; int **num; cin >> n; num = snake(n); for(i = 0; i < n; i++) { cout << num[i][0]; for(j = 1; j < n; j++) cout << ' ' << num[i][j]; cout << endl; } for(i = 0; i < n; i++) delete [] num[i]; delete [] num; } return 0; }


测评信息: