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
}