Java语言程序设计与数据结构(基础篇)课后练习题 第八章(三)

8.22

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int[][] a = new int[6][6];
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 6; j++) {
			a[i][j] = (int) (Math.random() * 2);
			System.out.print(a[i][j] + " ");
			if (j == 5)
				System.out.println();
		}
	int count = 0;
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 6; j++) {
			if (a[i][j] == 1)
				count++;
		}
		System.out.println(i + " row: " + countJudge(count));
		count = 0;
	}

	int count2 = 0;
	for (int j = 0; j < 6; j++) {
		for (int i = 0; i < 6; i++) {
			if (a[i][j] == 1)
				count2++;
		}
		System.out.println(j + " column: " + countJudge(count2));
		count2 = 0;
	}
}

public static String countJudge(int count) {
	String s1 = "Yes";
	String s2 = "No";
	if (count == 0)
		count = 1;
	if (count % 2 == 0)
		return s1;
	else
		return s2;
}

}

8.23

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.println("Enter a 6-by-6 matrix row by row: ");
	int[][] a = new int[6][6];
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 6; j++) {
			a[i][j] = input.nextInt();
			if (j == 5)
				System.out.println();
		}
	int count = 0;
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 6; j++) {
			if (a[i][j] == 1)
				count++;
		}
		if(countJudge(count)=="No"){
			System.out.printf("The flipped cell is at ( %d , ", indexJudge(countJudge(count), i));
		}
		count=0;
	}
	
	int count2 = 0;
	for (int j = 0; j < 6; j++) {
		for (int i = 0; i < 6; i++) {
			if (a[i][j] == 1)
				count2++;
		}
		if(countJudge(count2) == "No"){
			System.out.printf("%d )", indexJudge(countJudge(count2),j));
		}
		count2 = 0;
	}
		
}

public static int indexJudge(String s,int n){
	int b = 0;
	if(s=="No")
		b=n;
	return b;
}

public static String countJudge(int count) {
	String s1 = "Yes";
	String s2 = "No";
	if (count == 0) //这个if大家可能不懂为啥要添加,作用就是排除都是0的情况。
		count = 1;
	if (count % 2 == 0)
		return s1;
	else
		return s2;
}
//要输入的矩阵直接复制就行,不用谢>.<
//1 1 1 0 1 1 
//1 1 1 1 0 0
//0 1 0 1 1 1
//1 1 1 1 1 1
//0 1 1 1 1 0
//1 0 0 0 0 1

}

8.24

没找到清单在哪。。。。

8.25

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.println("Enter a 3-by-3 matrix row by row: ");
	double[][] a = new double[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = input.nextDouble();
	if(isMarkovMatrix(a))
		System.out.print("It is a Markov matrix");
	else
		System.out.print("It is not a Markov matrix");
}	
public static boolean isMarkovMatrix(double[][] m){
	double sum = 0;
	int count =0;
	for(int j=0;j<3;j++){
		for(int i=0;i<3;i++)
			sum += m[i][j];
		
		if(sum==1){
			count++;
		}
		sum=0;
	}

	if(count==3)
		return true;
	else
		return false;
}

//0.15 0.875 0.375
//0.55 0.005 0.225
//0.30 0.12 0.4

//0.95 -0.875 0.375
//0.65 0.005 0.225
//0.30 0.22 -0.4

}

8.26

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.println("Enter a 3-by-3 matrix row by row: ");
	double[][] a = new double[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = input.nextDouble();
	double[][] b = new double[3][3];
	b=sortRows(a);
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			System.out.print(b[i][j]+" ");
			if(j==2)
				System.out.println();
		}
}
public static double[][] sortRows(double[][] m){
	double tmp;
	for(int i=0;i<3;i++){
		if(m[i][0]>m[i][1]){
			tmp = m[i][0];
			m[i][0] = m[i][1];
			m[i][1] = tmp;
		}
		if(m[i][0]>m[i][2]){
			tmp = m[i][0];
			m[i][0] = m[i][2];
			m[i][2] = tmp;
		}
		if(m[i][1]>m[i][2]){
			tmp = m[i][1];
			m[i][1] = m[i][2];
			m[i][2] = tmp;
		}
	}
	return m;		
}
//0.15 0.875 0.375
//0.55 0.005 0.225
//0.30 0.12 0.4

}

8.27

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.println("Enter a 3-by-3 matrix row by row: ");
	double[][] a = new double[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = input.nextDouble();
	double[][] b = new double[3][3];
	b=sortRows(a);
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			System.out.print(b[i][j]+" ");
			if(j==2)
				System.out.println();
		}
}	
public static double[][] sortRows(double[][] m){
	double tmp;
	for(int i=0;i<3;i++){
		if(m[0][i]>m[1][i]){
			tmp = m[0][i];
			m[0][i] = m[1][i];
			m[1][i] = tmp;
		}
		if(m[0][i]>m[2][i]){
			tmp = m[0][i];
			m[0][i] = m[2][i];
			m[2][i] = tmp;
		}
		if(m[1][i]>m[2][i]){
			tmp = m[1][i];
			m[1][i] = m[2][i];
			m[2][i] = tmp;
		}
	}
	return m;		
}

//0.15 0.875 0.375
//0.55 0.005 0.225
//0.30 0.12 0.4

}

8.28

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter list1: ");
	int[][] a = new int[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = input.nextInt();
	System.out.print("Enter list2: ");
	int[][] b = new int[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			b[i][j] = input.nextInt();
	if(equals(a, b))
		System.out.print("The two arrays are strictly identical");
	else
		System.out.print("The two arrays are not strictly identical");
}	
public static boolean equals(int[][] m1,int[][]m2){
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			if(m1[i][j] != m2[i][j])
				return false;
	return true;
}
//	51 22 25 6 1 4 24 54 6
//	51 25 25 6 1 4 24 54 6

}

8.29

import java.util.Arrays;
import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter list1: ");
	int[][] a = new int[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			a[i][j] = input.nextInt();
	System.out.print("Enter list2: ");
	int[][] b = new int[3][3];
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			b[i][j] = input.nextInt();
	if(equals(a, b))
		System.out.print("The two arrays are identical");
	else
		System.out.print("The two arrays are not identical");
}
public static boolean equals(int[][] m1,int[][]m2){
	int[] n1 = new int[9];
	int[] n2 = new int[9];
	int count1 = 0;
	int count2 = 0;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			n1[count1] = m1[i][j];
			count1++;
		}
	
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			n2[count2] = m2[i][j];
			count2++;
		}
	Arrays.sort(n1);
	Arrays.sort(n2);
	
	for(int i=0;i<9;i++)
		if(n1[i]!=n2[i])
			return false;
	return true;
}
//	51 22 25 6 1 4 24 54 6

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值