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
Exploring Matrix的更多相关文章
- [转]第四章 使用OpenCV探测来至运动的结构——Chapter 4:Exploring Structure from Motion Using OpenCV
仅供参考,还未运行程序,理解部分有误,请参考英文原版. 绿色部分非文章内容,是个人理解. 转载请注明:http://blog.csdn.net/raby_gyl/article/details/174 ...
- 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 ...
- A geometric interpretation of the covariance matrix
A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...
- [NLP] cs224n-2019 Assignment 1 Exploring Word Vectors
CS224N Assignment 1: Exploring Word Vectors (25 Points)¶ Welcome to CS224n! Before you start, make ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- 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 ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
随机推荐
- 11.4.5 LVS负载均衡常见工作模式总结以及ipvsadm
NAT TUN DR RS any Tunneling Non-arp device RS network private LAN/WAN LAN RS number low(10-20) Hig ...
- 从单体迈向 Serverless 的避坑指南
作者 | 不瞋 导读:用户需求和云的发展两条线推动了云原生技术的兴起.发展和大规模应用.本文将主要讨论什么是云原生应用,构成云原生应用的要素是什么,什么是 Serverless 计算,以及 Serve ...
- Java基础之(十二):数组
数组 数组概述 定义 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干数据,按照一定的先后次序排列组合而成. 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 数组 ...
- 每日总结:charcter方法(2021.10.5)
\t 在文中该处插入一个tab键 \b在文中该处插入一个后退键 \n 换行 \r 在文中该处回车 \f 在文中该处插入换页符 方法: isLetter()是否是一个字母 isDigit()是否是一个 ...
- 题解 [HNOI2012]集合选数
题目传送门 题目大意 直接看题面吧. 思路 感觉挺水的一道题啊?怎么评到紫色的啊?考试的时候LJS出了这个题的加强版我就只想出这个思路,然后就爆了... 不难发现,我们可以构造矩阵: x 2x 4x ...
- bzoj3262陌上花开 (CDQ,BIT)
题目大意 给定n朵花,每个花有三个属性,定义\(f[i]\)为满足\(a_j \le a_i\)且\(b_j \le b_i\)且\(c_j \le c_i\)的j的数量, 求\(d \in [0,n ...
- cs224n 2019
视频链接 相关资源 Notes 笔记下载 笔记2 需要挂梯子,不然不显示图片,如果用ssr,要调到全局模式 转自:bitJoy CS224N(1.8)introduction and Word Vec ...
- [对对子队]会议记录4.10(Scrum Meeting 1)
本次每日例会的开会时间是4月10日晚上20:00,使用腾讯会议作为开会工具. 今天已完成的工作 何瑞 工作内容:制作UI界面的指令编辑系统,已大致实现指令的衔接 相关issue:实现用户指令编 ...
- NodeJs安装与环境配置
https://blog.csdn.net/qq_43285335/article/details/90696126 https://www.cnblogs.com/liuqiyun/p/813390 ...
- Envoy实现.NET架构的网关(四)集成IdentityServer4实现OAuth2认证
什么是OAuth2认证 简单说,OAuth 就是一种授权机制.数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据.系统从而产生一个短期的进入令牌(token),用来代替密码,供第三方应用使 ...