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. 憋了很久,终于弄懂什么是IOC(控制反转)

    导航 共享充电宝 IOC思想 复杂的依赖关系 IOC定义 Spring中的IOC IOC与工厂模式 IOC容器的技术剖析 结语 参考   本节是摘自<Spring Boot 实战纪实>的第 ...

  2. JDK源码阅读(3):AbstractStringBuilder、StringBuffer、StringBuilder类阅读笔记

    AbstractStringBuilder abstract class AbstractStringBuilder implements Appendable, CharSequence{ ... ...

  3. java/ kotlin下的单例模式

    单例模式属于创建型模式, 顾名思义,就是说整个系统中只有一个该对象的实例. 为什么要使用单例模式? 1, 对于一些需要频繁创建,销毁的对象, 使用单例模式可以节省系统资源 2, 对于全局持有的对象,单 ...

  4. C语言实现简易计算器(可作加减乘除)

    C语言实现简易计算器(加减乘除) 计算器作为课设项目,已完成答辩,先将代码和思路(注释中)上传一篇博客 已增添.修改.整理至无错且可正常运行 虽使用了栈,但初学者可在初步了解栈和结构语法后理解代码 # ...

  5. 阿里云函数计算发布新功能,支持容器镜像,加速应用 Serverless 进程

    我们先通过一段视频来看看函数计算和容器相结合后,在视频转码场景下的优秀表现.点击观看视频 >> FaaS 的门槛 Serverless 形态的云服务帮助开发者承担了大量复杂的扩缩容.运维. ...

  6. Java基础之(五):数据类型

    Java快捷键 首先我们先来介绍下IDEA的一些快捷键 psvm=public static void main(String[] args) {} sout=System.out.println(& ...

  7. CF49E Common ancestor(dp+dp+dp)

    纪念卡常把自己卡死的一次自闭模拟赛 QWQ 一开始看这个题,以为是个图论,仔细一想,貌似可以直接dp啊. 首先,因为规则只有从两个变为1个,貌似可以用类似区间\(dp\)的方式来\(check\)一段 ...

  8. git GUI Clients

    git GUI Clients Git 自带用于提交 (git-gui) 和浏览 (gitk) 的内置 GUI 工具,但也有一些第三方工具供寻求特定平台体验的用户使用. References Git ...

  9. 短短 29 天,应对高峰 100W+ 访问,看浙大如何交出满分答卷

    疫情期间"停课不停教,停课不停学",线上开课第一天,浙江大学网上开课平台访问量即突破100 万次,访客数3万余人,最高峰达 1.1万人同时在线,发起课程直播2000余场,然而系统却 ...

  10. 使用json-path解析json

    在我们的日常开发中,有时候需要从一个json字符串中获取一个值,或者从一段json字符串中获取到某些值,如果先使用Gson或Jackson转换成java对象在获取值,有些时候是很麻烦的,那么有没有一种 ...