Imagination
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
For each test case , there will be a line which contains a integer N (1<=N<=100).
Output
integer but no blank space after the last integer in a line.
Sample Input
4 1 2 3 4
Sample Output
1 1 2 4 3 1 2 3 8 7 4 9 6 5 1 2 3 4 12 11 10 5 13 14 9 6 16 15 8 7
AC代码:
#include<iostream>
using namespace std;
int p[100][100];
int main()
{
int n,i,j,k,x,y,t;
cin>>t;
while(t--)
{
cin>>n;
k=0;
x=0;
if(n==1) cout<<1<<endl;
else
{
while(1)
{
if(x==n) break;
x++;
for(i=1;i<=n-x+1;i++)
{
p[x][i]=++k;
}
for(i=x+1;i<=n;i++)
{
p[i][n-x+1]=++k;
}
if(x==n) break;
x++;
for(i=n;i>=x;i--)
{
p[i][n-x+1]=++k;
}
for(i=n-x;i>=1;i--)
{
p[x][i]=++k;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(j==n) cout<<p[i][j]<<endl;
else cout<<p[i][j]<<" ";
}
}
}
}
return 0;
}