چه جالب
منم این الگوریتم رو زمانیکه قسمت آرایه ها رو تموم کردم به عنوان تمرین آخر فصل پیاده سازی کرده بودم ( با جاوا ).
امیدوارم به دردتون بخوره :
/**
*
* author Esmaeil Ashrafi <>
*/
public class MagicSquare {
private String state = new String();
public MagicSquare() {
}
public String getState () {
return state ;
}
/**
* determines there is repeated numbers in the table or not
* @param a - the two dimensional array indicates the table
* @return true if table doesnt have any repeated number
*/
public boolean hasRepeated ( int[][] a ) {
/* java.util.Vector v = new java.util.Vector();
for (int i=0 ; i < a.length ; i++ )
for (int j=0 ; j < a.length ; j++ ){
v.addElement( new Integer(a[i][j]) );
if( v.indexOf(a[i][j]) != v.lastIndexOf(a[i][j]) )
return true ;
}
return false ;
*/
// another algorithm,more complicated,but maybe maybe more sufficient
for (int i=0 ; i < a.length ; i++ )
for (int j=0 ; j < a.length ; j++ )
for (int m=0 ; m <= i ; m++ )
for (int n=0 ; n < a.length ; n++ )
if ( a[i][j] == a[m][n] && !(m==i && n==j) )
return true;
return false;
}
public boolean isMagic( int[][] arr ) {
for(int i=0; i<arr.length; i++)
if ( arr.length != arr[i].length ){
state = "Not a square table";
return false;
}
for (int i=0 ; i < arr.length ; i++ )
for (int j=0 ; j < arr.length ; j++ )
if ( arr[i][j] < 1 || arr[i][j] > arr.length*arr.length ){
state = "some numbers out of range";
return false;
}
if ( hasRepeated(arr) ) {
state="the table has repeated numbers";
return false;
}
/* total : total value should every row,column or diagonal has
* row : total value of a row
* col : total value of a column
* ldt : left top 2 right diameter buttom toal value
* ldt : right top 2 left diameter buttom toal value
* r : row number
* c : colomn number
*/
int total , row , col , ldt , rdt , r , c ;
total=row=col=ldt=rdt=0;
/*
* set the value of sum of first row values 2 total
* and then compares every row and colomn to this value
*/
for(int i=0 ; i < arr.length ; i++ )
total+=arr[i][0]; // value of the first row
for (int i=0 ; i < arr.length ; i++ ) {
for (int j=0 ; j < arr.length ; j++ ) {
row+=arr[i][j]; col+=arr[j][i];
}
// if row==total and col==total,then row+col is 2*total
if ( (row+col) != (2*total) ){
state = "row and column values mismatch in row and column " + (i+1) ;
return false ;
}
row=col=0;
}
for (int i=0 ; i < arr.length ; i++ ) {
ldt+=arr[i][i];
rdt+=arr[i][arr.length-i-1];
}// calculates both diameter
if ( rdt+ldt != 2*total ){
state = "diameters values mismatch";
return false ;
}
return true ;
}
public static void main(String[] args){
System.out.println("\n*******************result of exercise 'Magic Square' :");
int[][] s = { {6,7,2},{1,5,9},{3,8,4} };
for(int i=0;i<s.length;i++){
for(int j=0;j<s[i].length;j++)
System.out.print(s[i][j]+" ");
System.out.println();
}
MagicSquare ms = new MagicSquare();
if ( ms.isMagic(s) )
System.out.println("Congratulation ! It's magic");
else
System.out.println( ms.getState() );
}
}
در ضمن این برای n*n هست.( البته n*n نباشه هم حتمن یه پیغامی میده )
اگه کسی ایرادی پیدا کرد(یعنی یه جدول داد و نتیجه ی غلط برگردوند بهم بگه،اینو خیلی وقت پیش نوشتم،احتمالن ایرادی نداره چون حتمن خودم 10 رقم تستش کردم...اگه داشت حتمن یادم میموند بعدن برطرفش کنم.ولی خب کار دیگه
---------------------
بچه ها من الان داشتم با خودم فکر میکردم منظور شما احتمالن ساختن یک مربع جادویی بوده ...
این برنامه میاد یک ماتریس m*n رو در قالب یه آرایه میگیره و تشخیص میده که جادویی هست یا نه !
پس شرمنده م .