JAVA "magic square"判定算法的实现。

package com.qiqi;

import java.util.Scanner;

/**
 * 
 * Magic squares. An n*n matrix that is filled with the numbers 1, 2, 3, . . . , n2 is a
 * magic square if the sum of the elements in each row, in each column, and in the two
 * diagonals is the same value. For example,
 * 
 *  16   3    2    13
 *  5    10   11   8
 *  9    6    7    12
 *  4    15   14   1
 * 
 * Write a program that reads in n2 values from the keyboard and tests whether they
 * orm a magic square when arranged as a square matrix. You need to test three
 * features:
 * • Did the user enter n2 numbers for some n?
 * • Do each of the numbers 1, 2, . . . , n2 occur exactly once in the user input?
 * • When the numbers are put into a square, are the sums of the rows, columns,
 * and diagonals equal to each other?
 * If the size of the input is a square, test whether all numbers between 1 and n2 are
 * present. Then compute the row, column, and diagonal sums. Implement a class
 * Square with methods
 * public void add(int i)
 * public boolean isMagic()
 * 
 * @author Administrator
 *
 */
public class Magic_squares {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Square_n square_4 = new Square_n(4);
		square_4.print();
		System.out.println("whether all numbers between 1 and n*n are present:  " + square_4.is_everyone_exist());
		System.out.println("is this square a magic one:  " + square_4.isMagic());
	}
}
/**
 * the class use to store a square matrix,and some other informations.
 * with some function we can do the test job.
 * @author Administrator
 *
 */
class Square_n {
	/**
	 * square used to store the square matrix.
	 * flag help us to check whether all numbers between 1 and n*n are present or not.
	 * n is the rank of the square. 
	 */
	int[][] square;
	int[] flag;
	int n;
	
	/**
	 * non-parameter constructor.we set n = 4 as the default rank.
	 * we will initialize the square and flag first and the input the
	 * number of the square.
	 */
	Square_n() {
		n = 4;
		square = new int[n][n];
		flag = new int[n * n];
		input_the_square();
	}

	/**
	 * constructor with a parameter l.please check out upstairs.
	 * @param l
	 * l is the rank of the square.
	 */
	Square_n(int l) {
		n = l;
		square = new int[n][n];
		flag = new int[n * n];
		input_the_square();
	}
	
	/**
	 * this function help us print the square out as an standard matrix format.
	 */
	public void print() {
		// TODO Auto-generated method stub
		for(int row = 0; row < n; row ++){
			for(int column = 0; column < n; column ++)
				System.out.print(square[row][column] + " ");
			System.out.println();
		}
	}
	
	/**
	 * accept the numbers form the keyboard.
	 */
	private void input_the_square() {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		for (int row = 0; row < n; row ++){
			for (int  column= 0; column < n; column ++){
				square[row][column] = scanner.nextInt();
				flag[row * n + column] = square[row][column];
			}
		}
		scanner.close();
		System.out.println();
		System.out.println("you have already enter n*n numbers");
	}

	/**
	 * here we can test do each of the numbers 1, 2, . . . , n*n
	 * occur exactly once in the user input.in this method we sort 
	 * the flag by bubble sort, and then check if every number equal
	 * to the index or not.we return true for every number exist once.
	 * @return
	 */
	public boolean is_everyone_exist(){
		int max;
		int tmp;
		for(int i = flag.length - 1; i > 0; i --){
			max = 0;
			for(int j = 0; j <= i; j ++){
				if(flag[max] < flag[j]) max = j;
			}
			tmp = flag[i];
			flag[i] = flag[max];
			flag[max] = tmp;
		}
		
		for (int index = 0; index < flag.length; index ++){
			if(flag[index] != index + 1) 
				return false;
		}
		return true;
	}
	
	/**
	 * to tell the truth i don't know why do we should have a function add,
	 * it's useless for my part so I suspend it.
	 * @param i
	 */
	public void add(int i){}
	
	/**
	 * we just check every sum one by one,it's easy.
	 * true for what we input is a magic one.
	 * @return
	 */
	public boolean isMagic(){
		if (!is_everyone_exist()) return false;
		else {
			int sum = 0;
			int sum_temp = 0;
			
			for (int index = 0; index < n; index ++)
				sum += square[index][index];
			
			for (int index = 0; index < n; index ++)
				sum_temp += square[index][n - 1 - index];
			if (sum_temp != sum) return false;
			
			for (int row = 0; row < n; row ++){
				sum_temp = 0;
				for (int column = 0; column < n; column ++)
					sum_temp += square[row][column];
				if (sum_temp != sum) return false;
			}
			
			for (int column = 0; column < n; column ++){
				sum_temp = 0;
				for (int row = 0; row < n; row ++)
					sum_temp += square[row][column];
				if (sum_temp != sum) return false;
			}
			
			return true;
		}
	}
}

你可能感兴趣的:(算法,算法,JAVA,magic,square)