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是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
随机推荐
- 解决安装mysql 到start service出现未响应问题
mysql下载地址 链接: https://pan.baidu.com/s/1vYpsNkVjUHqOKPQl9Y9A9A 提取码: wngn 安装可以参考 今天下载了MySql5.5,没想到的是前面 ...
- 前端开发3年了,竟然不知道什么是 Vue 脚手架?(上)
一.脚手架认识和使用前提 CLI 是什么意思? CLI -- Command-Line Interface 命令行界面,俗称脚手架. 脚手架就是一个大概的框架,是建筑学上的一个概念. 1.1.什么是V ...
- MySQL8.0.20下载与安装详细图文教程,mysql安装教程
MySQL下载与安装(8.0.20版)教程 mysql安装包+mysql学习视频+mysql面试指南视频教程 下载地址: 链接:https://pan.baidu.com/s/1FmLFhGlajBQ ...
- 试题 算法训练 二进制数数 java解题
资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定L,R.统计[L,R]区间内的所有数在二进制下包含的"1"的个数之和. 如5的二进制为101,包含2个&q ...
- 1. SSTI(模板注入)漏洞(入门篇)
好久没更新博客了,现在主要在作源码审计相关工作,在工作中也遇到了各种语言导致的一些SSTI,今天就来大概说一下SSTI模板注入这个老生常谈的漏洞 前言 模板引擎 模板引擎(这里特指用于Web开发的模板 ...
- C 函数指针 函数指针数组 转移表
内容来自<c和指针>,整理后方便个人理解 高级声明 cdel程序可以方便的给出声明的释义 指向函数的指针 int ( *f ) ( int n_values, float amount ) ...
- 使用YApi搭建API接口管理工具(docker安装)
使用YApi搭建API接口管理工具(docker安装) 工具描述 YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布 ...
- C#特性知识图谱-一、委托
一. 委托 1.1 委托定义 委托可以看成是一个方法的容器,将某一具体的方法装入后就可以把它当成方法一样调用.一个委托类型的变量可以引用任何一个满足其要求的方法.委托类似于C语言中的函数指针,但并不完 ...
- Stream中的Pipeline理解
使用Stream已经快3年了,但是从未真正深入研究过Stream的底层实现. 今天开始把最近学到的Stream原理记录一下. 本篇文章简单描述一下自己对pipeline的理解. 基于下面一段代码: p ...
- 【UE4 设计模式】适配器模式 Adapter Pattern
概述 描述 将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper). 套路 Target(目标抽象类) 目标抽象类定义了客户所需要的接口,可 ...