zoukankan      html  css  js  c++  java
  • Exploring Matrix

    import java.util.Scanner;
    public class J714
    {
        /**
         * @taking input from user
         */
        public static void main(String[] args)
        {
            /**
             * @taking input from user
             */
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the length of a square matrix: ");
            int n = input.nextInt();
            /**
             * @declaring the array of n*n size
             */
            int[][] board = new int[n][n];
    
            boolean isSameOnARow = false; boolean isSameOnAColumn = false;
            boolean isSameOnADiagonal = false; boolean isSameOnASubdiagonal = false;
            /**
             * @performing the fucntions
             */
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    board[i][j] = (int)(Math.random() * 2.0D);
                    System.out.print(board[i][j]);
                }
                System.out.println();
            }
            /**
             * @taking a for loop
             */
            for (int i = 0; i < board.length; i++) {
                boolean same = true;
                for (int j = 1; j < board[0].length; j++) {
                    /**
                     * @checking the conditions
                     */
                    if (board[i][0] != board[i][j]) {
                        same = false;
                        break;
                    }
                }
                /**
                 * @checking the conditions
                 */
                if (same) {
                    System.out.println("All " + board[i][0] + "'s on row " + i);
                    isSameOnARow = true;
                }
            }
            for (int j = 0; j < board[0].length; j++) {
                boolean same = true;
                for (int i = 1; i < board.length; i++) {
                    if (board[0][j] != board[i][j]) {
                        same = false;
                        break;
                    }
                }
                if (same) {
                    System.out.println("All " + board[0][j] + "'s on column " + j);
                    isSameOnAColumn = true;
                }
            }
            boolean same = true;
            for (int i = 1; i < board.length; i++) {
                if (board[0][0] != board[i][i]) {
                    same = false;
                    break;
                }
            }
            if (same) {
                System.out.println("All " + board[0][0] + "'s on major diagonal");
                isSameOnADiagonal = true;
            }/**
             * @checking the conditions
             */
            same = true;
            for (int i = 1; i < board.length; i++) {
                if (board[0][(board.length - 1)] != board[i][(board.length - 1 - i)]) {
                    same = false;
                    break;
                }
            }
            /**
             * @checking the conditions and printing the required elements
             */
            if (same) {
                System.out.println("All " + board[0][(board.length - 1)] + "'s on sub-diagonal");
                isSameOnASubdiagonal = true;
            }
            if (!isSameOnARow) {
                System.out.println("No same numbers on a row");
            }
            if (!isSameOnAColumn) {
                System.out.println("No same numbers on a column");
            }
            if (!isSameOnADiagonal) {
                System.out.println("No same numbers on the major diagonal");
            }
            if (!isSameOnASubdiagonal)
                System.out.println("No same numbers on the sub-diagonal");
        }
    }
    import java.util.Scanner;
    public class ExploringMatrix
    {
      public static void main (String[] args)
      {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the size for the matrix: ");
        int size = scan.nextInt();
        int[][] m = new int[size][size];
         
        for (int i =0;i<size;i++)
        {
          for (int j =0;j<size;j++)
          {
            m[i][j] = (int) (Math.random()*2);
            System.out.print(m[i][j]);
          }
          System.out.println();
        }
        boolean letitbe;
        //Row
        int j =0;
         
        boolean row=false, column=false, major =false, sub = false;
        for(int i=0; i<size;i++)
        {
          letitbe = true;
          for (j =0;j<size-1;j++)
          {
            if (m[i][j] != m[i][j+1]) letitbe = false;
          }
          if (letitbe)
          {
            System.out.println("All " + m[i][j] + "s on row " + i);
            row = true;
          }
        }
        //Column
        for(int i=0; i<size;i++)
        {
          letitbe = true;
          for (j =0;j<size-1;j++)
          {
            if (m[j][i] != m[j+1][i]) letitbe = false;
          }
          if (letitbe)
          {
            System.out.println("All " + m[j][i] + "s on column " + i);
            column = true;
          }
           
        }
        //Major diagonal, there is only one eh?
        letitbe = true;
        for (int  i=0;i<size-1;i++)
        {
          if (m[i][i]!= m[i+1][i+1]) letitbe = false;
        }
        if (letitbe)
        {
          System.out.println("All " + m[0][0] + "s on major diagonal");
          major = true;
        }
         
        letitbe = true;
        for (int  i=0;i<size-1;i++)
        {
          if (m[i][size-i-1]!= m[i+1][size-i-1-1]) letitbe = false;
        }
        if (letitbe)
        {
          System.out.println("All " + m[0][size-1] + "s on sub-diagonal");
          sub = true;
        }
       
        if ( column == false) System.out.println("No same numbers on a column");
        if ( row == false) System.out.println("No same numbers on a row");
        if ( major == false) System.out.println("No same numbers on the major diagonal");
        if ( sub == false) System.out.println("No same numbers on the sub-diagonal");
      }
    }
    Exploring Matricies in Java
    Problem:
    Write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix,
    prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Output: Enter the size
    for the matrix: 4 1000 0111 0101 0100 No same numbers on a column No same numbers on a row No same numbers on the major diagonal No same numbers on the sub-diagonal
  • 相关阅读:
    html5 canvas雨点打到窗玻璃动画
    html5跟随鼠标炫酷网站引导页动画特效
    如何实现复选框的全选和取消全选效果
    CSS3透明属性opacity
    jQuery实现方式不一样的跳转到底部
    ul li设置横排,并除去li前的圆点
    jQuery美女幻灯相册轮播源代码
    微软modern.IE网站,多版本IE免费测试工具集
    css中position与z-index
    C#一个方法返回多个值
  • 原文地址:https://www.cnblogs.com/zhangyongjian/p/3632463.html
Copyright © 2011-2022 走看看