#include
using namespace std;
struct numUnit
{
int value;
int x;
int y;
};
int main(int argc, char* argv[])
{
int num;
cout << "Enter a number:";
cin >> num;
numUnit* matrixUnit = new numUnit[num*num];
int xMark, yMark;
for ( int iBas=1; iBas <= num; iBas++ ){
xMark = iBas;
yMark = 1;
matrixUnit[iBas-1].value = iBas;
matrixUnit[iBas-1].x = xMark;
matrixUnit[iBas-1].y = yMark;
}
int i = num + 1;
bool ypp = true;
for ( int j=(num-1); j >= 1; j-- ){
if ( ypp == true){
for ( int ky=1; ky <= j; ky++){
matrixUnit[i-1].value = i;
matrixUnit[i-1].x = xMark;
matrixUnit[i-1].y = ++yMark;
i++;
}
for ( int kx=1; kx <=j; kx++){
matrixUnit[i-1].value = i;
matrixUnit[i-1].x = --xMark;
matrixUnit[i-1].y = yMark;
i++;
}
}
else {
for ( int ky=1; ky <= j; ky++){
matrixUnit[i-1].value = i;
matrixUnit[i-1].x = xMark;
matrixUnit[i-1].y = --yMark;
i++;
}
for ( int kx=1; kx <=j; kx++){
matrixUnit[i-1].value = i;
matrixUnit[i-1].x = ++xMark;
matrixUnit[i-1].y = yMark;
i++;
}
}
ypp = (!ypp);
}
for ( int sy=1; sy <= num; sy++ ){
for ( int sx=1; sx <= num; sx++ ){
for ( int sn=1;sn <= num*num; sn++ ){
if ( matrixUnit[sn-1].x == sx && matrixUnit[sn-1].y == sy){
printf("%4d",matrixUnit[sn-1].value);
}
}
}
cout << endl;
}
delete [] matrixUnit;
return 0;
}
--------------------next---------------------
阅读(955) | 评论(0) | 转发(0) |