Java语言程序设计(基础篇)第八章(2 19 36)

第二题(求矩阵主对角线元素的和)

import java.util.Scanner;
public class Text {

	public static void main(String[] args) {
		System.out.println("Enter a 4-by-4 matrix row by row:");
		Scanner input = new Scanner(System.in);
		double[][] m=new double[4][4];
		for(int i=0;i<4;i++) {
			for(int j=0;j<4;j++) 
				m[i][j]=input.nextDouble();
			}
		 input.close();
		System.out.print("Sum of tne elements in the major diagonal is "+sumMajorDiagonal(m));
		}
	public static double sumMajorDiagonal(double[][] m) {
		return m[0][0]+m[1][1]+m[2][2]+m[3][3];
		
	}
}

结果如下:


第十九题(模式识别:连续的四个相等的数)

import java.util.Scanner;
public class Text {

	public static void main(String[] args) {
		int[][] matrix = creatMatrix();
        boolean fourEqualNumbers = isHasFourEqualNumbers(matrix);
        if (fourEqualNumbers)
          System.out.println("false");
        else 
        System.out.println("true");
    }
   //创建并接收二维数组
    public static int[][] creatMatrix()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows in the  array: ");
        int row = input.nextInt();
        System.out.print("Enter the number of columns in the  array: ");
        int column = input.nextInt();
        input.close();

        int[][] matrix = new int[row][column];
        System.out.println("Enter this matrix: ");
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < column; j++)
            {
                matrix[i][j] = input.nextInt();
            }
        }

        return matrix;
    }
    //判断是否有连续的四个数
    public static boolean isHasFourEqualNumbers(int[][] matrix)
    {
        return lineHas(matrix) || columnHas(matrix) || principalDiagonalHas(matrix) || viceDiagonalHas(matrix);
    }
    //判断行(水平)
    public static boolean lineHas(int[][] matrix)
    {
        for (int row = 0; row < matrix.length; row++)
        {
            for (int column = 0; column <= matrix[row].length - 4; column++)
            {
                if ((matrix[row][column] == matrix[row][column + 1]) &&
                    (matrix[row][column] == matrix[row][column + 2]) &&
                    (matrix[row][column] == matrix[row][column + 3]))
                
                    return true;
               
            }
        }

        return false;
    }
    //判断列(垂直)
    public static boolean columnHas(int[][] matrix)
    {
        for (int column = 0; column < matrix[0].length; column++)
        {
            for (int row = 0; row <= matrix.length - 4; row++)
            {
                if ((matrix[row][column] == matrix[row + 1][column]) &&
                    (matrix[row][column] == matrix[row + 2][column]) &&
                    (matrix[row][column] == matrix[row + 3][column]))
               
                    return true;
                
            }
        }

        return false;
    }
    //判断主对角线
    public static boolean principalDiagonalHas(int[][] matrix)
    {
        for (int row = 0; row <= matrix.length - 4; row++)
        {
            for (int column = 0; column <= matrix[0].length - 4; column++)
            {
                if ((matrix[row][column] == matrix[row + 1][column + 1]) &&
                    (matrix[row][column] == matrix[row + 2][column + 2]) &&
                    (matrix[row][column] == matrix[row + 3][column + 3]))
                
                    return true;
                
            }
        }

        return false;
    }
    //判断副对角线
    public static boolean viceDiagonalHas(int[][] matrix)
    {
        for (int row = 3; row < matrix.length; row++)
        {
            for (int column = 0; column <= matrix[0].length - 4; column++)
            {
                if ((matrix[row][column] == matrix[row - 1][column + 1]) &&
                    (matrix[row][column] == matrix[row - 2][column + 2]) &&
                    (matrix[row][column] == matrix[row - 3][column + 3]))
                
                    return true;
                
            }
        }

        return false;
    }
}

结果如下:

第三十六题(拉丁正方形)

import java.util.Scanner;  
import java.util.Arrays;  
public class Text {

	public static void main(String[] args) {
		System.out.print("Enter number n:");  
        Scanner input = new Scanner(System.in);  
        int n = input.nextInt();  
        System.out.println("Enter "+n+" rows of letters separated by spaces:");  
        char[][] matrix = new char[n][n]; //二维数组 char型
        String temp = null;  
        for(int i = 0; i < n; ++i){  
            for(int j = 0; j < n; ++j){  
                temp = input.next(); 
                matrix[i][j] = temp.charAt(0); //将字符串转换成数组 
                if(matrix[i][j]>('A'+n-1)) System.out.println("Wrong input: the letters separated must be from A to "+((char)('A'+n-1)));
        }
	  }  
        int[] record = new int[n];  
        Arrays.fill(record, 0);  //将0填充到record中
         
        for(int i = 0; i < n; ++i){  
            for(int j = 0; j < n; ++j){  
                ++record[(int)(matrix[i][j]-'A')];  
            }  
        }  
          
        for(int i = 0; i < n; ++i){  
            for(int j = 0; j < n; ++j){  
                ++record[(int)(matrix[j][i] - 'A')];  
            }  
        }  
        boolean flag = true;  
        for(int i = 0; i < n; ++i){  
            if(record[i] != 2*n){  
                flag = false;  
            }  
        }  
          if(flag==true)
        System.out.println("The input array is a Latin square"); 
          else  System.out.println("The input array is not a Latin square"); 
    }  
}  

结果如下:

(以上若有错误请指出 谢谢)

展开阅读全文

没有更多推荐了,返回首页