打印出具有下列规律的矩阵图
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
[此贴子已经被kai于2004-07-30 23:56:51编辑过]
#include<stdio.h> #include<iostream.h>
#define SIZE 8 #define ALL SIZE*SIZE
static int x;
class round{ public: char point; int i; void set_r(){point='r';} void set_d(){point='d';} void set_l(){point='l';} void set_u(){point='u';}
};
round a[SIZE][SIZE];
main() { int i,k,j; for(i=0;i<SIZE/2;i++){ a[i][i].set_r(); a[i+1][i].set_r(); a[SIZE-i-1][SIZE-i-1].set_l(); a[SIZE-i-1][i].set_u(); a[i][SIZE-i-1].set_d(); }
x=1; i=0; j=0; do{ while(a[i][j].point=='r'||a[i][j].point==NULL){ a[i][j].i=x; x++; j++; } while(a[i][j].point=='d'||a[i][j].point==NULL){ a[i][j].i=x; x++; i++; } while(a[i][j].point=='l'||a[i][j].point==NULL){ a[i][j].i=x; x++; j--; } while(a[i][j].point=='u'||a[i][j].point==NULL){ a[i][j].i=x; x++; i--; }
}while(x!=ALL+1);
for(i=0;i<SIZE;i++){ for(k=0;k<SIZE;k++){ printf("%4d",a[i][k].i); } printf("\n"); }
return 0; }