LINUX下编译,输出:
[JESSE@localhost tmp]$ ./a.out
N = 10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
4 5 6 7 8 9 10 1 2 3
5 6 7 8 9 10 1 2 3 4
6 7 8 9 10 1 2 3 4 5
7 8 9 10 1 2 3 4 5 6
8 9 10 1 2 3 4 5 6 7
9 10 1 2 3 4 5 6 7 8
10 1 2 3 4 5 6 7 8 9
/**************************************************************/
/* phalanx.c:
* 输出N阶拉丁方阵.
*
* <June 25, 2007> JESSE
*/
#include <stdio.h>
void phalanx(unsigned n, unsigned p[][n])
{
unsigned i, j;
unsigned num, rnum;
for (i=0, num=rnum=1; i < n; i++) {
for (j=0; j < n; j++, num++) {
p[i][j] = num;
if (num == n)
num = 0;
}
num = ++rnum;
}
}
/**************************************************************/
int main(void)
{
unsigned i, j, N;
printf("N = ");
scanf("%u", &N);
/*
* init...
*/
unsigned P[N][N];
for (i=0; i < N; i++)
for (j=0; j < N; j++)
P[i][j] = 0;
/*
* calculating...
*/
phalanx(N, P);
/*
* Print them!!!
*/
for (i=0; i < N; i++) {
for (j=0; j < N; j++)
printf("%u ", P[i][j]);
printf("\n");
}
return 0;
}
[此贴子已经被作者于2007-6-24 18:41:34编辑过]