用JAVA(面向对象思想)实现以下算法。是面向对象哦~~

jason_deng 2009-12-16 09:32:41
//在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
...全文
313 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
tempter1305 2009-12-18
  • 打赏
  • 举报
回复
。。。。。
这个是什么题目啊?
9阶以下,还凑合,
超过10阶程序。。。。。。
groovy2007 2009-12-16
  • 打赏
  • 举报
回复
楼上各位给的都是什么答案呀,人家要的是所有的方阵,你们的程序给出的只有一个结果。
我用的是回溯法,能够打印所有的方阵。
对于N=3时,有12个结果。N=4时,有576个结果。N=5时,结果太多了(>80000),打印要很久。
N=3时的结果如下:
1
123
231
312
================
2
123
312
231
================
3
132
213
321
================
4
132
321
213
================
5
213
132
321
================
6
213
321
132
================
7
231
123
312
================
8
231
312
123
================
9
312
123
231
================
10
312
231
123
================
11
321
132
213
================
12
321
213
132
================


class LaDing {
final int n;
int[][] m;
int count = 0;

LaDing(int n) {
this.n = n;
m = new int[n][n];
fill(0);
}

boolean check(int i, int j, int a) {
for(int col=0; col<j; col++)
if(m[i][col] == a) return false;
for(int row=0; row<i; row++)
if(m[row][j] == a) return false;
return true;
}

void fill(int i) {
int row = i/n;
int col = i%n;
for(int a=1; a<=n; a++) {
if(check(row, col, a)) {
m[row][col] = a;
if(i == n*n-1) {
count++;
System.out.println(count);
for(int[] line : m) {
for(int x : line)
System.out.print(x);
System.out.println();
}
System.out.println("================");
}
else fill(i+1);
}
}
}

public static void main(String[] args) {
new LaDing(3);
}
}
peanut942692 2009-12-16
  • 打赏
  • 举报
回复
import java.io.*;

class LaDing {
private int n;

public LaDing(int n) {
this.n = n;
}

public int get(int row,int col) {
return (row+ col)% n+1;
}

public void print() {
for (int i=0; i< n; i++) {
for (int j=0; j< n; j++) {
System.out.print(get(i, j)+" ");
}
System.out.println();
}
}

public static void main(String[] args) throws IOException {
int n = 1;
String num = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((num = br.readLine()) != null) {
n = Integer.parseInt(num);
break;
}
LaDing ld = new LaDing(n);
ld.print();

}
}

还不怎么完善,自己改进改进!
hiperion 2009-12-16
  • 打赏
  • 举报
回复
import java.io.IOException;
import java.io.*;

public class LatinSquareMatrix {

//方阵阶数
int n;
//矩阵数组
int[][] arr ;
public LatinSquareMatrix(int n) {
// TODO Auto-generated constructor stub
this.n = n;
arr = new int[n][n];
makeMatrix(n);
}
public void makeMatrix(int n){
for (int i=0;i<n;i++){
arr[0][i] = i+1;
}
for (int j=1;j<n;j++){
for (int k=0;k<n;k++){
if (arr[j-1][k]==n){
arr[j][k]=1;
} else {
arr[j][k]=arr[j-1][k]+1;
}
}
}
}
public void printMatrix(){
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
System.out.println("共"+n*n+"个数字");

}
public static void main(String[] args) throws IOException {
System.out.print("请输入:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String num ="";
try
{
num = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}

LatinSquareMatrix lsm = new LatinSquareMatrix(Integer.parseInt(num));
lsm.printMatrix();
}
}
运行结果:

请输入:5
12345
23451
34512
45123
51234
共25个数字
tempter1305 2009-12-16
  • 打赏
  • 举报
回复
。。。。。。
yuzuru 2009-12-16
  • 打赏
  • 举报
回复
上面那个还是笨了,第一感觉要用数组,其实完全没必要。

class LaDing {
private int n = 1;

public LaDing(int n) {
this.n = n;
}

public int get(int row, int col) {
return (row + col) % n + 1;
}

public void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(get(i, j) + " ");
}
System.out.println();
}
}
}
tempter1305 2009-12-16
  • 打赏
  • 举报
回复
汗,楼上的算法没看明白。。。
N阶拉丁只有唯一一个么。。。
tempter1305 2009-12-16
  • 打赏
  • 举报
回复
你要介绍下,五阶拉丁的特性啊。。。
中间有什么规律,否则很难写。
yuzuru 2009-12-16
  • 打赏
  • 举报
回复
这和对象有关吗?

class LaDing {
private int[] num = null;

public LaDing(int n) {
num = new int[n];
for (int i = 0; i < num.length; i++) {
num[i] = i + 1;
}
}

public int get(int row, int col) {
return num[(row + col) % num.length];
}

public void print() {
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
System.out.print(get(i, j) + " ");
}
System.out.println();
}
}
}
tonytone2008 2009-12-16
  • 打赏
  • 举报
回复
现在马上要考试,等我回来给你解决!!呵呵
tonytone2008 2009-12-16
  • 打赏
  • 举报
回复
自己想想吧!!就是在类里加个解决这个问题的算法!!!
justinavril 2009-12-16
  • 打赏
  • 举报
回复
算法自己写吧 所谓面向对象 就是把这个方法放到一个类中去 就是这么简单
tempter1305 2009-12-16
  • 打赏
  • 举报
回复
。。。。。
N!(N-1)!(N-2)···2!*1
tempter1305 2009-12-16
  • 打赏
  • 举报
回复
fucking。。。那个鸡巴设个下载分10的,弄的我下不了。。。郁闷。。。
另外,上面写错了点:
应该是 生成(N-1)!(N-2)!···2!*1组(重点)标记

所以生成的总数是:*N(N-1)!(N-2)···2!*1
n=3, 为12
n=4 288
chenxiaomingit 2009-12-16
  • 打赏
  • 举报
回复
比如说我 \(^o^)/~\(^o^)/~
chenxiaomingit 2009-12-16
  • 打赏
  • 举报
回复
所以用穷举的一般都是勤快的笨蛋
chenxiaomingit 2009-12-16
  • 打赏
  • 举报
回复
穷举是可行且最差的方法 特点是结果正确 但是效率极差
bawgiitx 2009-12-16
  • 打赏
  • 举报
回复
这还算法?直接打印不就行了
yuyeyi 2009-12-16
  • 打赏
  • 举报
回复
感觉像全排列加全排列
SambaGao 2009-12-16
  • 打赏
  • 举报
回复

class LaDing {
private int n = 1;

public LaDing(int n) {
this.n = n;
}

public int get(int row, int col) {
return (row + col) % n + 1;
}

public void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(get(i, j) + " ");
}
System.out.println();
}
}

public static void main(String args[]) {
new LaDing(5).print() ;
}
}
很强
加载更多回复(6)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧