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

Exploring Matrix的更多相关文章

  1. [转]第四章 使用OpenCV探测来至运动的结构——Chapter 4:Exploring Structure from Motion Using OpenCV

    仅供参考,还未运行程序,理解部分有误,请参考英文原版. 绿色部分非文章内容,是个人理解. 转载请注明:http://blog.csdn.net/raby_gyl/article/details/174 ...

  2. Searching a 2D Sorted Matrix Part I

    Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...

  3. A geometric interpretation of the covariance matrix

    A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...

  4. [NLP] cs224n-2019 Assignment 1 Exploring Word Vectors

      CS224N Assignment 1: Exploring Word Vectors (25 Points)¶ Welcome to CS224n! Before you start, make ...

  5. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  6. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  7. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  8. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  9. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

随机推荐

  1. mysql安装教程,mysql安装配置教程

    MySQL的安装教程 一.MYSQL的安装 首先登入官网下载mysql的安装包,官网地址:https://dev.mysql.com/downloads/mysql/ 一般下载这个就好,现在的最新版本 ...

  2. 由于坏块导致DG的mrp0进程中断

    由于坏块导致DG的mrp0进程中断1.Environment11.2.0.4 ADG 2.SymptomsDG的mrp0进程中断,重启后,还是卡住.查看alert,在1.47存在报错,进一步查看trc ...

  3. xLua中Lua调用C#

    xLua中Lua调用C# 1.前提 这里使用的是XLua框架,需要提前配置xlua,设置加载器路径: 可以参考之前的Blog:<xlua入门基础>: //调用段,所有的lua代码都写在Lu ...

  4. 题解 「BZOJ4919 Lydsy1706月赛」大根堆

    题目传送门 题目大意 给出一个 \(n\) 个点的树,每个点有权值,从中选出一些点,使得满足大根堆的性质.(即一个点的祖先节点如果选了那么该点的祖先节点的权值一定需要大于该点权值) 问能选出来的大根堆 ...

  5. 分布式/微服务必配APM系统,SkyWalking让你不迷路

    前言 如今分布式.微服务盛行,面对拆分服务比较多的系统,如果线上出现异常,需要快速定位到异常服务节点,假如还用传统的方式排查肯定效率是极低的,因为服务之间的各种通信会让定位更加繁琐:所以就急需一个分布 ...

  6. Python绘制Excel图表

    今天讲解下如何使用Python绘制各种Excel图表,下面我们以绘制饼状图.柱状图.水平图.气泡图.2D面积图.3D面积图为例来说明. import openpyxlfrom openpyxl imp ...

  7. 【二食堂】Alpha - Scrum Meeting 9

    Scrum Meeting 9 例会时间:4.19 13:00~13:20 进度情况 组员 昨日进度 今日任务 李健 1. "文本区域"栏目完成,可实现实体和关系的添加issue ...

  8. logstash的安装和简单使用

    logstash的安装和简单使用 一.安装 1.下载并解压 2.logstash 一些命令行参数 1.查看帮助信息 2.加载指定pipeline文件路径 3.检测配置文件语法是否有错误 4.热加载pi ...

  9. 编译qwt遇到的问题

    在windows下使用mingw编译从git上下载的qwt工程下的tests时一直提示一下错误: error: undefined reference to `qMain(int, char**)' ...

  10. TT模板的作用及使用

    一.假如你在ef中添加一个实体,没有模板,你需要在DAL层中新建一个"莫某Dal"和"I某某Dal"以及在公共的DbSession中加你的这个dal,然后需要在 ...