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);
}

}

第八章习题 完

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页