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

}

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) {

  • 24
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值