1 条题解
-
0
C :
#include <stdio.h> int main (void) { int i, n, k, ix, iy; scanf("%d", &n); int A[n][n]; k = 0; for(i = 0; i <= n / 2; i++) { for(ix = i, iy = n - i - 1; ix < n - i - 1; ix++) { k++; A[ix][iy] = k; } for(ix = n - i - 1, iy = n - i - 1; iy > i; iy--) { k++; A[ix][iy] = k; } for(ix = n - i - 1, iy = i; ix > i; ix--) { k++; A[ix][iy] = k; } for(ix = i, iy = i; iy < n - i - 1; iy++) { k++; A[ix][iy] = k; } } if(n % 2 == 1) A[n / 2][n / 2] = ++k; for(ix = 0; ix < n; ix++) { for(iy = 0; iy < n; iy++) { if(iy > 0) printf(" "); if(A[ix][iy] < 10&&(iy == 0||A[ix][iy - 1] < 10)) printf(" "); printf("%d", A[ix][iy]); } printf("\n"); } return 0; }
C++ :
#include <iostream> #include <string> #include <cstdio> using namespace std; int n; int map[200][200]={0}; void fill_map(int t,int l,int s) { for(int i=l+1;i<=s;i++) map[i][s]=t++; for(int i=s-1;i>=l+1;i--) map[s][i]=t++; for(int i=s-1;i>=l+1;i--) map[i][l+1]=t++; for(int i=l+2;i<=s-1;i++) map[l+1][i]=t++; if(s>=0) fill_map(t,l+1,s-1); } void print_map1() { for(int i=1;i<=n;i++) { for(int j=1;j<n;j++) printf("%2d ",map[i][j]); printf("%2d\n",map[i][n]); } } void print_map2() { for(int i=1;i<=n;i++) { for(int j=1;j<n;j++) printf("%d ",map[i][j]); printf("%d\n",map[i][n]); } } int main() { cin>>n; fill_map(1,0,n); if(n!=10) print_map1(); else print_map2(); return 0; }
Python :
# coding=utf-8 #!/usr/bin/python3 def solve(N): matrix = [[0 for i in range(0, N)] for i in range(0, N)] num = 1 N_2 = int((N+1)/2) for i in range(0, N_2): x, y = i, N - i - 1 matrix[x][y] = num while x < y: matrix[x][y] = num num += 1 x += 1 while y > i: matrix[x][y] = num num += 1 y -= 1 while x > i: matrix[x][y] = num num += 1 x -= 1 while y < N - i -1: matrix[x][y] = num num += 1 y += 1 return matrix def print_ans(matrix): n = len(matrix) for i in range(0, n): for j in range(0, n): if j == 0 and n <= 3: print('{:2d}'.format(matrix[i][j]), sep='', end='') elif j == 0: print('{:d}'.format(matrix[i][j]), sep='', end='') elif n <= 3: print(' {:2d}'.format(matrix[i][j]), sep='', end='') else: print(' {:d}'.format(matrix[i][j]), sep='', end='') print('') def main(): s = input() N = int(s) print_ans(solve(N)) if __name__ == '__main__': main()
- 1
信息
- ID
- 166
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 10
- 标签
- 递交数
- 11
- 已通过
- 1
- 上传者