#include <bits/stdc++.h>
using namespace std;
int mat[105][105];
int main (){
int n;
cin >> n;
if (n % 2 == 1){
int x = 1;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << x << " ";
x++;
}
cout << "\n";
}
}else {
if (n == 2){
cout << -1;
}else {
//GLAVNO
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
mat[i][j] = 0;
}
}
int k = 1;
int suma = 0;
for (int i = 0; i < n-1; i++){
mat[0][i] = k;
suma += k;
k++;
}
int pr = mat[0][n/2];
int x = (n*pr)-suma;
mat[0][n-1] = x;
for (int i = 1; i < n-1; i++){
mat[i][0] = mat[i-1][n-1] + 1;
int suma = mat[i][0];
for (int j = 1; j < n-1; j++){
mat[i][j] = mat[i][j-1] + 1;
suma += mat[i][j];
}
int pr = mat[i][n/2];
int x = (n*pr)-suma;
mat[i][n-1] = x;
}
for (int j = 0; j < n; j++){
int suma = 0;
for (int i = 0; i < n-1; i++){
suma += mat[i][j];
}
int pr = mat[n/2][j];
int x = (n*pr)-suma;
mat[n-1][j] = x;
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
452 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |