Java語言程式設計與資料結構(基礎篇)課後複習題 第八章(四)
8.30
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 a(4): ");
double[][] a = new double[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
a[i][j] = input.nextDouble();
System.out.print("Enter b(2): ");
double[] b = new double[2];
for(int i=0;i<2;i++)
b[i] = input.nextDouble();
if(linearEquation(a, b)==null)
System.out.print("No roots");
else
System.out.print("x: "+linearEquation(a, b)[0]+" , "+"y: "+linearEquation(a, b)[1]);
}
public static double[] linearEquation(double[][] a , double[] b){
if(a[0][0]*a[1][1]-a[0][1]*a[1][0]==0)
return null;
double[] roots = new double[2];
double x = (b[0]*a[1][1]-b[1]*a[0][1])/(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
double y = (b[1]*a[0][0]-b[0]*a[1][0])/(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
roots[0] = x;
roots[1] = y;
return roots;
}
}
8.31
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
System.out.println("Enter the coordinates of the four points: ");
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.nextInt();
}
}
double array[]=getIntersectingPoint(points);
System.out.print("crossover point: (");
for(int i=0;i<2;i++) {
if (i == 1) {
System.out.print(array[i] + ") ");
} else {
System.out.print(array[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 = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]);
double K2 = (points[3][1] - points[2][1]) / (points[3][0] - points[2][0]);
double[] arr=new double[2];
if (K1 != K2) {
double B1 = y1 - K1 * x1;
double B2 = y3 - K2 * x3;
arr[0]= (B2 - B1) / (K1 - K2);
arr[1]= (B2 * K1 - B1 * K2) / (K1 - K2);
} else {
System.out.println("NULL");
}
return arr;
}
}
8.32
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
System.out.println("Enter the values of three coordinates: ");
double[][] points = new double[3][2];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
points[i][j] = input.nextDouble();
}
}
if(Double.isNaN(getTriangleArea(points)))
System.out.print("The three points are on the same line");
else
System.out.printf("The area of the triangle is %.2f",getTriangleArea(points));
}
public static double getTriangleArea(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 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((x1*y2 + x2*y + x*y1 - x1*y - x2*y1 - x*y2) / 2);
S[1] = Math.abs((x1*y4 + x4*y + x*y1 - x1*y - x4*y1 - x*y4) / 2);
S[2] = Math.abs((x3*y2 + x2*y + x*y3 - x3*y - x2*y3 - x*y2) / 2);
S[3] = Math.abs((x3*y4 + x4*y + x*y3 - x3*y - x4*y3 - x*y4) / 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"},
{"Oregon", "Salem"},
{"Pennslyvania", "Harrisburg"},
{"Rhode Island", "Providence"},
{"South Carolina", "Columbia"},
{"South Dakota", "Pierre"},
{"Tennessee", "Nashville"},
{"Texas", "Austin"},
{"Utah", "Salt Lake City"},
{"Vermont", "Montpelier"},
{"Virginia", "Richmond"},
{"Washington", "Olympia"},
{"West Virginia", "Charleston"},
{"Wisconsin", "Madison"},
{"Wyoming", "Cheyenne"}
};
int correctCount = 0;//用於計數統計出答對的次數
for (int i = 0; i < stateCapital.length; i++) {
// JOptionPane提示框。showMessaDialog(只有一個確認按鈕的訊息提示框)
String capital = JOptionPane.showInputDialog("What is the capital of " + stateCapital[i][0] + "?");
if (capital.equals(stateCapital[i][1])){
JOptionPane.showMessageDialog(null, "Your answer is correct");
correctCount++;
}else
JOptionPane.showMessageDialog(null,"The correct answer should be " + stateCapital[i][1]);
}
JOptionPane.showMessageDialog(null,"The correct count is " + correctCount);
}
}
第八章習題 完
相關文章
- Go語言結構體(struct)物件導向程式設計基礎篇Go結構體Struct物件程式設計
- 重學C語言_資料結構與基礎語法C語言資料結構
- 《Python程式設計》第八章部分課後練習題Python程式設計
- 《Java語言程式設計(基礎篇)(原書第10版)》第2~4章部分程式設計練習題程式碼Java程式設計
- Java語言程式設計(基礎篇)第十版 5.14Java程式設計
- java課後題複習Java
- 第一章-JAVA基礎-課後總結和課後習題Java
- 資料結構 課程設計 員工管理系統(C語言)資料結構C語言
- Golang基礎程式設計(一)-基本結構、資料型別、流程語句Golang程式設計資料型別
- 1097: C語言程式設計教程(第三版)課後習題10.4C語言程式設計
- D程式語言基礎篇
- 《計算機基礎與程式設計》第四周學習總結計算機程式設計
- 問題 1011: C語言程式設計教程(第三版)課後習題6.1C語言程式設計
- JavaScript基礎複習(一) 語言特性及資料型別JavaScript資料型別
- 資料結構與演算法——基礎篇(一)資料結構演算法
- Python學習之旅(核心程式設計基礎篇6基礎資料型別③)Python程式設計資料型別
- 資料結構與演算法分析(c 語言描述)習題 1.2資料結構演算法
- 資料結構與演算法分析(c 語言描述)習題 1.3資料結構演算法
- 資料結構與演算法分析(c 語言描述)習題 1.1資料結構演算法
- Kenneth A.Lambert著的資料結構(用python語言描述)的第一章課後程式設計答案資料結構Python程式設計
- Java課後習題總結Java
- 《Linux程式設計基礎》第四章習題Linux程式設計
- 資料庫基礎知識整理與複習總結資料庫
- Java面試題-基礎篇四Java面試題
- Java 程式設計技巧之資料結構Java程式設計資料結構
- Java網路程式設計基礎學習與整理Java程式設計
- python第八章課後習題Python
- C語言 第八章 結構體與共用體 重點 典型題C語言結構體
- C 語言程式設計習慣總結程式設計
- 華南農業大學C語言程式設計課後習題(第六章)C語言程式設計
- Java程式設計基礎31——MySql資料庫Java程式設計MySql資料庫
- 資料 結構客觀題複習題集
- 軟體設計師:程式設計語言基礎知識程式設計
- SQL語言基礎(資料控制語言)SQL
- 資料結構基礎學習之時間複雜度分析資料結構時間複雜度
- 《java程式設計基礎》例題5.6Java程式設計
- Java 函數語言程式設計(一)初識篇Java函數程式設計
- Perl語言學習(四)Perl控制結構