标签:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n,i,T=1,j;
cout<<"请输入一个n"<<endl;
cin>>n;
int a[100][100]={0};
int n1 = 0,n2 = 0;
int q = 1;
for(i = 1;i<=n*n;i++){
//右
if(T == 1){
a[n1][n2] = i;
n2++;
if(n2>(n-q)){
T = 2;
n2--;
}
}
if(T == 2){
a[n1][n2] = i;
n1++;
if(n1 >(n-q)){
T = 3;
n1--;
}
}
if(T == 3){
a[n1][n2] = i;
n2--;
if(n2 < q - 1){
T = 4;
n2++;
}
}
if(T == 4){
a[n1][n2] = i;
n1--;
if(n1 < q ){
T = 1;
q++;
n1++;
n2++;
}
}
}
for(i = 0;i<n;i++){
for(j = 0;j<n;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
标签:
原文地址:http://www.cnblogs.com/eat-too-much/p/5944330.html