题目描述 Description
小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该n行n列正方形矩阵以及其的对角线数字之和.
输入描述 Input Description
n(即n行n列)
输出描述 Output Description
n+1行,n行为组成的矩阵,最后一行为对角线数字之和
样例输入 Sample Input
3
样例输出 Sample Output
5 4 3
6 1 2
7 8 9
25
数据范围及提示 Data Size & Hint
#include <iostream>
using namespace std;
int main()
{
int n,a[100][100],j,i,k=0,x,p,sum=0,t;
cin >> n;
t=n*n;
a[n/2][n/2]=1;
i=n-1;j=n-1;
for(x=n/2;x>=1;x=x-1)
{
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
j--;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
i--;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
j++;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
i++;
}
j--;i--;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
if(i==j||i+j==n-1) sum=sum+a[i][j];
}
cout<<endl;
}
cout<<sum;
return 0;
}
using namespace std;
int main()
{
int n,a[100][100],j,i,k=0,x,p,sum=0,t;
cin >> n;
t=n*n;
a[n/2][n/2]=1;
i=n-1;j=n-1;
for(x=n/2;x>=1;x=x-1)
{
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
j--;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
i--;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
j++;
}
for(p=0;p<2*x;p++)
{
a[i][j]=t;
t--;
i++;
}
j--;i--;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
if(i==j||i+j==n-1) sum=sum+a[i][j];
}
cout<<endl;
}
cout<<sum;
return 0;
}