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

double x2 = points[1][0];

double y2 = points[1][1];

double x3 = points[2][0];

double y3 = points[2][1];

double s1 = Math.pow((x1-x2)(x1-x2)+(y1-y2)(y1-y2),0.5);

double s2 = Math.pow((x2-x3)(x2-x3)+(y2-y3)(y2-y3),0.5);

double s3 = Math.pow((x1-x3)(x1-x3)+(y1-y3)(y1-y3),0.5);

double s = (s1+s2+s3)/2;

double a = Math.pow(s*(s-s1)(s-s2)(s-s3),0.5);

return a;

}

}

8.33

=================================================================

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {

System.out.println("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");

double[][] points = new double[4][2];

Scanner input = new Scanner(System.in);

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 2; j++) {

points[i][j] = input.nextDouble();

}

}

double array[] = getIntersectingPoint(points);

// System.out.print(“Intersecting point: (”);

// for (int i = 0; i < 2; i++) {

// if (i == 1) {

// System.out.print(array[i] + ") ");

// } else {

// System.out.print(array[i] + “,”);

// }

// }

double x = array[0];

double y = array[1];

double x1 = points[0][0];

double y1 = points[0][1];

double x2 = points[1][0];

double y2 = points[1][1];

double x3 = points[2][0];

double y3 = points[2][1];

double x4 = points[3][0];

double y4 = points[3][1];

double[] S = new double[4];

S[0] = Math.abs((x1y2 + x2y + xy1 - x1y - x2y1 - xy2) / 2);

S[1] = Math.abs((x1y4 + x4y + xy1 - x1y - x4y1 - xy4) / 2);

S[2] = Math.abs((x3y2 + x2y + xy3 - x3y - x2y3 - xy2) / 2);

S[3] = Math.abs((x3y4 + x4y + xy3 - x3y - x4y3 - xy4) / 2);

bubble(S);

System.out.println();

System.out.print("The areas are ");

for (int i = 0; i < 4; i++) {

if (i == 3) {

System.out.printf(“%.2f”,S[i]);

} else {

System.out.printf("%.2f ",S[i]);

}

}

}

public static double[] getIntersectingPoint(double[][] points) {

double x1 = points[0][0];

double y1 = points[0][1];

double x2 = points[1][0];

double y2 = points[1][1];

double x3 = points[2][0];

double y3 = points[2][1];

double x4 = points[3][0];

double y4 = points[3][1];

double K1 = (y1 - y3) / (x1 - x3);

double K2 = (y2 - y4) / (x2 - x4);

double[] arr = new double[2];

double B1 = y1 - K1 * x1;

double B2 = y2 - K2 * x2;

arr[0] = (B2 - B1) / (K1 - K2);

arr[1] = (B2 * K1 - B1 * K2) / (K1 - K2);

return arr;

}

public static void bubble(double[] S) {

double temp;

for (int i = 0; i < S.length; i++) {

for (int j = S.length - 1; j > i; j–) {

if (S[j] < S[j - 1]) {

temp = S[j - 1];

S[j - 1] = S[j];

S[j] = temp;

}

}

}

}

}

8.34

=================================================================

import java.util.Scanner;

public class dibazhang {

public static void main(String args[]) {

Scanner input = new Scanner(System.in);

System.out.print(“Enter 6 points:”);

double[][] points = new double[6][2];

for (int i = 0; i < 6; ++i) {

points[i][0] = input.nextDouble();

points[i][1] = input.nextDouble();

}

double[] point = getRightmostLowestPoint(points);

System.out.println(“The rightmost lowest point is (”+point[0] + “,” + point[1]+“)”);

}

public static double[] getRightmostLowestPoint(double[][] points) {

double[][] sum = new double[6][3];

for (int i = 0; i < 6; ++i) {

sum[i][0] = points[i][0];

sum[i][1] = points[i][1];

sum[i][2] = Math.abs(points[i][0]) + Math.abs(points[i][0]);

}

selectSort(sum);

double[] result = sum[5];

for (int i = 5; i >= 0; --i) {

if (sum[i][0] >= 0 && sum[i][1] <= 0) {

result = sum[i];

break;

}

}

return result;

}

public static void selectSort(double[][] num) {

for (int i = 0; i < 5; ++i) {

for (int j = i + 1; j < 6; ++j) {

if (num[i][2] > num[j][2]) {

double[] temp = num[i];

num[i] = num[j];

num[j] = temp;

}

}

}

}

}

8.35

=================================================================

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 rows in the matrix:”);

int n = input.nextInt();

int[][] test = new int[n][n];

System.out.println(“Enter the matrix row by row:”);

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

test[i][j]=input.nextInt();

int[] jesus = findLargestBlock(test);

System.out.printf(“The maximum square submatrix is at (%d,%d) with size %d\n”,jesus[0],jesus[1],jesus[2]);

}

public static int[] findLargestBlock(int[][] m){

int[] res = new int[3];

int n = m.length;

for(int i=0;i<n;i++)

for(int j=0;j<n;j++){

for(int r=1;r<=Math.min(n-i,n-j);r++){

if(allOne(i,j,r,m)){

if(r>res[2]){

res[0]=i;

res[1]=j;

res[2]=r;

}

}

}

}

return res;

}

public static boolean allOne(int pinr,int pinc, int range,int[][] m){

boolean all1 = true;

for(int i=0;i<range;i++)

for(int j=0;j<range;j++){

if(m[pinr+i][pinc+j]==0){

all1 = false;

break;

}

}

return all1;

}

}

8.36

=================================================================

import java.util.Arrays;

import java.util.Scanner;

public class dibazhang {

public static void main(String args[]) {

System.out.print(“Enter number n:”);

Scanner input = new Scanner(System.in);

int n = input.nextInt();

char[][] matrix = new char[n][n];

String temp = null;

System.out.printf(“Enter %d rows of letters separated by spaces: \n”,n);

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

temp = input.next();

matrix[i][j] = temp.charAt(0);

}

}

int[] record = new int[n];

Arrays.fill(record, 0);

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

++record[(int) (matrix[i][j] - ‘A’)];

}

}

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

++record[(int) (matrix[j][i] - ‘A’)];

}

}

boolean flag = true;

for (int i = 0; i < n; ++i) {

if (record[i] != 2 * n) {

flag = false;

}

}

if(flag)

System.out.print(“The input array is a latin square”);

else

System.out.print(“Wrong input: the letters must be from A to C”);

}

}

8.37

=================================================================

这个题,自己做太麻烦,所以看了看网上怎么写的,借鉴一下。

import javax.swing.JOptionPane;

public class dibazhang {

public static void main(String[] args) {

String[][] stateCapital = {

//55个州和它们独自的首府

{“Alabama”, “Montgomery”},

{“Alaska”, “Juneau”},

{“Arizona”, “Phoenix”},

{“Arkansas”, “Little Rock”},

{“California”, “Sacramento”},

{“Colorado”, “Denver”},

{“Connecticut”, “Hartford”},

{“Delaware”, “Dover”},

{“Florida”, “Tallahassee”},

{“Georgia”, “Atlanta”},

{“Hawaii”, “Honolulu”},

{“Idaho”, “Boise”},

{“Illinois”, “Springfield”},

{“Indiana”, “Indianapolis”},

{“Iowa”, “Des Moines”},

{“Kansas”, “Topeka”},

{“Kentucky”, “Frankfort”},

{“Louisiana”, “Baton Rouge”},

{“Maine”, “Augusta”},

{“Maryland”, “Annapolis”},

{“Massachusettes”, “Boston”},

{“Michigan”, “Lansing”},

{“Minnesota”, “Saint Paul”},

{“Mississippi”, “Jackson”},

{“Missouri”, “Jefferson City”},

{“Montana”, “Helena”},

{“Nebraska”, “Lincoln”},

{“Nevada”, “Carson City”},

{“New Hampshire”, “Concord”},

{“New Jersey”, “Trenton”},

{“New York”, “Albany”},

{“New Mexico”, “Santa Fe”},

{“North Carolina”, “Raleigh”},

{“North Dakota”, “Bismark”},

{“Ohio”, “Columbus”},

{“Oklahoma”, “Oklahoma City”},

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
io", “Columbus”},

{“Oklahoma”, “Oklahoma City”},

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-LskSUnhL-1715776535998)]

[外链图片转存中…(img-HQbClzZW-1715776535999)]

[外链图片转存中…(img-b0MctEj6-1715776535999)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值