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

8.1

package demo;
import java.util.*;
public class dibazhang {

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double[][] num = new double[3][4];
		System.out.print("Enter a 3-by-4 matrix by row: \n");
		for(int i=0;i<3;i++)
			for(int j=0;j<4;j++)
				num[i][j] = input.nextDouble();
		for(int i=0;i<4;i++)
			System.out.printf("Sum of the elements at column %d is %4.1f\n", i,sumColumn(num,i));
}

public static double sumColumn(double[][] m,int columnIndex){
		int rows = m.length;
		double sum = 0;
		for(int i=0;i<rows;i++)
			sum += m[i][columnIndex];
		return sum;
}

}

8.2

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double[][] num = new double[4][4];
		System.out.print("Enter a 4-by-4 matrix row by row: \n");
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
				num[i][j] = input.nextDouble();
		System.out.printf("Sum of the elements in the major diagonal is %.1f", sumMajorDiagonal(num));
}
public static double sumMajorDiagonal(double[][] m){
		int len = m.length;
		double sum =0;
		for(int i=0;i<len;i++)
			sum += m[i][i];
		return sum;
}

}

8.3

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		char[][] answers = {
				{'A','B','A','C','C','D','E','E','A','D'},
				{'A','B','C','D','E','A','B','C','D','E'},
				{'A','B','C','C','C','A','C','C','D','A'}};
		char[] keys = {'D','B','D','C','C','D','A','E','A','D'};
		for(int i=0;i<answers.length;i++){
			int correctCount = 0;
			for(int j=0;j<answers[i].length;j++){
				if(answers[i][j] == keys[j])
					correctCount++;
			}
			System.out.println("Student "+i+" is correct count is "+correctCount);
		}
}

}

8.4

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
			Scanner input = new Scanner(System.in);
			System.out.print("Enter the number of employee: ");
			int num = input.nextInt();
			int[][] workTime = new int[num][7];
			System.out.println("Enter workTime: ");
			for(int i=0;i<workTime.length;i++){
				for(int j=0;j<7;j++){
					workTime[i][j] = input.nextInt();
				}
			}
			int[] sum = Sum(workTime);
			int[] index = new int[num];
			for(int i=0;i<num;i++)
				index[i] = i;
			for(int i=0;i<workTime.length;i++){
				int k = i;
				for(int j=i+1;j<workTime.length;j++)
					if(sum[j]>sum[k])
						k = j;
				if( k!= i){
					int tmp = sum[k];
					sum[k] = sum[i];
					sum[i] = tmp;
					tmp = index[k];
					index[k] = index[i];
					index[i] = tmp;
				}
			}
			for(int i=0;i<workTime.length;i++)
				System.out.println(i+" employee "+index[i]+","+sum[i]);
}
public static int[] Sum(int[][] workTime){
		int[] s = new int[workTime.length];
		for(int i=0;i<workTime.length;i++){
			for(int j=0;j<workTime[i].length;j++)
				s[i] += workTime[i][j];
		}
		return s;
}

}

8.5

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double[][] m1 = new double[3][3];
		double[][] m2 = new double[3][3];
		System.out.println("Enter matrix1:");
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m1[i][j] = input.nextDouble();
		System.out.println("Enter matrix2:");
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m2[i][j] = input.nextDouble();
		double[][] nums = addMatrix(m1, m2);
		System.out.println(m1[0][0] + " " + m1[0][1] + " " + m1[0][2] + "   " + m2[0][0] + " " + m2[0][1] + " "
				+ m2[0][2] + "   " + nums[0][0] + " " + nums[0][1] + " " + nums[0][2]);
		System.out.println(m1[1][0] + " " + m1[1][1] + " " + m1[1][2] + " + " + m2[1][0] + " " + m2[1][1] + " "
				+ m2[1][2] + " = " + nums[1][0] + " " + nums[1][1] + " " + nums[1][2]);
		System.out.println(m1[2][0] + " " + m1[2][1] + " " + m1[2][2] + "   " + m2[2][0] + " " + m2[2][1] + " "
				+ m2[2][2] + "   " + nums[2][0] + " " + nums[2][1] + " " + nums[2][2]);
}

public static double[][] addMatrix(double[][] a, double[][] b) {
	double[][] nums = new double[3][3];
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			nums[i][j] = a[i][j] + b[i][j];
	return nums;
}

}

8.6

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double[][] m1 = new double[3][3];
		double[][] m2 = new double[3][3];
		System.out.println("Enter matrix1:");
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m1[i][j] = input.nextDouble();
		System.out.println("Enter matrix2:");
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m2[i][j] = input.nextDouble();
		double[][] nums = multiMatrix(m1, m2);
		System.out.println(m1[0][0] + " " + m1[0][1] + " " + m1[0][2] + "   " + m2[0][0] + " " + m2[0][1] + " "
				+ m2[0][2] + "   " + nums[0][0] + " " + nums[0][1] + " " + nums[0][2]);
		System.out.println(m1[1][0] + " " + m1[1][1] + " " + m1[1][2] + " * " + m2[1][0] + " " + m2[1][1] + " "
				+ m2[1][2] + " = " + nums[1][0] + " " + nums[1][1] + " " + nums[1][2]);
		System.out.println(m1[2][0] + " " + m1[2][1] + " " + m1[2][2] + "   " + m2[2][0] + " " + m2[2][1] + " "
				+ m2[2][2] + "   " + nums[2][0] + " " + nums[2][1] + " " + nums[2][2]);
}

public static double[][] multiMatrix(double[][] a,double[][] b){
    int rows = a.length;
    int columns = b[0].length;
    double[][] nums = new double[rows][columns];
    for(int i=0;i<rows;i++)
        for(int j=0;j<rows;j++){
            for(int k=0;k<a[0].length;k++)
            	nums[i][j]+=a[i][k]*b[k][j];
        }
    return nums;
}

}

8.7

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of points: ");
		int num = input.nextInt();
		System.out.println("Enter " + num + " points: ");
		double[][] points = getArray(num);
		int p1 = 0;
		int p2 = 1;
		double shortDistance = distance(points[p1][0], points[p1][1], points[p1][2], points[p2][0], points[p2][1],
				points[p2][2]);
		for (int i = 0; i < points.length; i++) {
			for (int j = i + 1; j < points.length; j++) {
				double distance = distance(points[i][0], points[i][1], points[i][2], points[j][0], points[j][1],
						points[j][2]);
				if (shortDistance > distance) {
					p1 = i;
					p2 = j;
					shortDistance = distance;
				}
			}
		}
		System.out.println("Two recent point (" + points[p1][0] + ", " + points[p1][1] + ", " + points[p1][2] + ")and("
				+ points[p2][0] + ", " + points[p2][1] + ", " + points[p2][2] + ")");
		System.out.println("The distance is " + shortDistance);
}

public static double[][] getArray(int n) {
	Scanner input = new Scanner(System.in);
	double[][] points = new double[n][3];
	for (int i = 0; i < points.length; i++)
		for (int j = 0; j < 3; j++)
			points[i][j] = input.nextDouble();
	return points;
}

public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
	return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
}

}

8.8

开始看错题目了,费了好大劲写了个三维的,结果看题是二维,人都傻了。。。。现在,又改过来了。

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the point of number: ");
		int num = input.nextInt();
		System.out.println("Enter " + num + " points: ");
		double[][] points = getArray(num);
		int p1 = 0;
		int p2 = 1;
		double shortDistance = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]);
		for (int i = 0; i < points.length; i++) {
			for (int j = i + 1; j < points.length; j++) {
				double distance = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
				if (shortDistance > distance) {
					p1 = i;
					p2 = j;
					shortDistance = distance;
				}
			}
		}
		System.out.println("Their distance is " + shortDistance);
	for (int i = 0; i < points.length; i++) {
		for (int j = i + 1; j < points.length; j++) {
			double distance = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
			if (distance == shortDistance) {
				p1 = i;
				p2 = j;
				System.out.println("The closest two points are (" + points[p1][0] + ", " + points[p1][1] + ")and("
						+ points[p2][0] + ", " + points[p2][1] + ")");
			}
		}
	}

	System.out.println("Their distance is " + shortDistance);
}

public static double[][] getArray(int n) {
	Scanner input = new Scanner(System.in);
	double[][] points = new double[n][2];
	for (int i = 0; i < points.length; i++)
		for (int j = 0; j < 2; j++)
			points[i][j] = input.nextDouble();
	return points;
}

public static double distance(double x1, double y1, double x2, double y2) {
	return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

}

8.9

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
			Scanner input = new Scanner(System.in);
			char[][] str = new char[3][3];
			for(int i=0;i<3;i++)
				for(int j=0;j<3;j++)
					str[i][j] = 'a';
			int xo1 = 0;
			char[] xo2 ={'X','O'};
			while(true){
				printMap(str);
				int row;
				int column;
				boolean a = true;
				while(a){
					System.out.println("Enter a row (0,1, or 2) for player "+xo2[xo1%2]+":");
					row = input.nextInt();
					System.out.println("Enter a column (0,1, or 2) for player "+xo2[xo1%2]+":");
					column = input.nextInt();
					if(str[row][column] != 'X'&&str[row][column] != 'O'){
						str[row][column]=xo2[xo1%2];
						break;
					}
					System.out.print("it has been selected,pick another!\n");
				}
				xo1++;
				if(checkWin(str,'X')){
					System.out.println("X player won");
					break;
				}else if(checkWin(str,'O')){
					System.out.println("O player won");
					break;
				}
			}
	}
public static void printMap(char[][] str){
		System.out.println("..........");
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				System.out.print("|");
				if(str[i][j]=='a')
					System.out.print(" ");
				else
					System.out.print(str[i][j]);
			}
			System.out.println("|");
			System.out.println("..........");
		}
}
public static boolean checkWin(char[][] str,char xo){
		if(xo=='X'){
	        for(int i=0;i<3;i++)
	            if(str[i][0]=='X'&&str[i][1]=='X'&&str[i][2]=='X')
	                return true;
	        for(int i=0;i<3;i++)
	            if(str[0][i]=='X'&&str[1][i]=='X'&&str[2][i]=='X')
	                return true;
	        if(str[0][0]=='X'&&str[1][1]=='X'&&str[2][2]=='X')
	            return true;
	        if(str[0][2]=='X'&&str[1][1]=='X'&&str[2][0]=='X')
	            return true;
	        return false;
	    }else{
	        for(int i=0;i<3;i++)
	            if(str[i][0]=='O'&&str[i][1]=='O'&&str[i][2]=='O')
	                return true;
	        for(int i=0;i<3;i++)
	            if(str[0][i]=='O'&&str[1][i]=='O'&&str[2][i]=='O')
	                return true;
	        if(str[0][0]=='O'&&str[1][1]=='O'&&str[2][2]=='O')
	            return true;
	        if(str[0][2]=='O'&&str[1][1]=='O'&&str[2][0]=='O')
	            return true;
	        return false;
	    }
}

}

8.10

package demo;

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		int[][] ary = new int[4][4];
		for (int i = 0; i < ary.length; i++) {
			for (int j = 0; j < ary[i].length; j++) {
				ary[i][j] = (int) (Math.random() * 2);
				System.out.print(ary[i][j]);
			}
			System.out.println();
		}
		int maxR = 0;
		int maxC = 0;
		int row = 0;
		int col = 0;
		for (int i = 0; i < ary.length; i++) {
			int numR = 0;
			int numC = 0;
			for (int j = 0; j < ary[i].length; j++) {
				if (ary[i][j] == 1) {
					numR++;
				}
				if (ary[j][i] == 1) {
					numC++;
				}
			}
			if (numR > maxR) {
				maxR = numR;
				row = i;
			}
			if (numC > maxC) {
				maxC = numC;
				col = i;
			}
		}
		System.out.println("The largest row index: " + (row + 1));
		System.out.println("The largest column index: " + (col + 1));
}

}

8.11

package demo;

import java.util.Scanner;

public class dibazhang {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int str;
		System.out.print("Enter a number between 0 and 511: ");
		str = input.nextInt();
		int[] strs = new int[9];
		for (int i = 0; i < 9; i++) {
			strs[i] = str % 2;
			str /= 2;
		}
		while (true) {
			int count=0;
			for (int i = 8; i >= 0; i--) {
				if (strs[i] == 0)
					System.out.print("H");
				else
					System.out.print("T");
				count++;
				if(count%3==0)
					System.out.println();
			}
			break;
		}
}

}

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xupengboo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值