1091: 回形方阵
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 165 Solved: 107
[Submit][Status][Web Board]
Description
输出一个回形方阵,具体可见样例
Input
一个整数n (0 < n < 10)
Output
一个方阵,每个数字的场宽为2
Sample Input
8
Sample Output
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 88 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 88 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 88 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 88 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 88 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 88 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 88 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 88 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 88 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 88 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 88 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 88 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 88 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 88 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 88 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
AC代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 100;
int e[N][N];
int main(){int n, p, q, t;cin >> n;p = 1;q = 2 * n + 1;t = n;for(int j = 0; j <= n; j++, p++, q--){for(int i = p; i <= q; i++){e[p][i] = t;}for(int i = p; i <= q; i++){e[i][q] = t;}for(int i = 2 * n - j; i >= p; i--){e[q][i] = t;}for(int i = 2 * n - j; i >= p + 1; i--){e[i][p] = t;}t--;}for(int i = 1; i <= 2 * n + 1; i++){for(int j = 1; j <= 2 * n + 1; j++){printf("%2d", e[i][j]);}printf("\n");}return 0;
}