4. 在N行N列的数阵中, 数K(1〈=K〈=N)在每行和每列中出现且仅
出现一次,这样的数阵叫N阶拉丁方阵。例如下图就是一个五阶拉丁方阵。
编一程序,从键盘输入N值后,打印出所有不同的N阶拉丁方阵,并统计个数。
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
这一题非常简单, 就是打印行列之和即可
//--------------------------------------------------
//--------------------------------------------------
// Author: freshui
// Date : 2007-05-23
// Dev-C++ 4.9.9.2
//--------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
...{
int num;
while(cin>>num)
...{
if(num>1 || num<80) // num too large, we can not display in the screen
...{ break;}
cout<<"The input number should between 1 ~ 80!"<<endl;
}
for(int row=0; row<num; row++)
...{
for( int col=0; col<num; col++)
...{
if ((col+row)<num)
printf("%4d", col+row + 1);
else
printf("%4d", (col+row) - num +1);
}
printf(" ");
}
system("PAUSE");
return EXIT_SUCCESS;
}
//--------------------------------------------------
// Author: freshui
// Date : 2007-05-23
// Dev-C++ 4.9.9.2
//--------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
...{
int num;
while(cin>>num)
...{
if(num>1 || num<80) // num too large, we can not display in the screen
...{ break;}
cout<<"The input number should between 1 ~ 80!"<<endl;
}
for(int row=0; row<num; row++)
...{
for( int col=0; col<num; col++)
...{
if ((col+row)<num)
printf("%4d", col+row + 1);
else
printf("%4d", (col+row) - num +1);
}
printf(" ");
}
system("PAUSE");
return EXIT_SUCCESS;
}