java语言程序设计与数据结构(基础篇)第四章答案
4.1
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the length from the center to a vertex:"); double r = input.nextDouble(); double s = 2 * r * Math.sin(Math.PI / 5); double area = 5 * s * s / (4 * Math.tan(Math.PI / 5)); System.out.printf("The area of the pentagon is %.2f",area); } }
4.2
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter point 1(latitude and longitude) in degrees:"); double deX1 = input.nextDouble(),deY1 = input.nextDouble(); System.out.print("Enter point 2(latitude and longitude) in degrees:"); double deX2 = input.nextDouble(),deY2 = input.nextDouble(); double x1 = Math.toRadians(deX1),y1 = Math.toRadians(deY1); double x2 = Math.toRadians(deX2),y2 = Math.toRadians(deY2); final double r = 6371.01; double d = r * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2)); System.out.println("The distance between the two points is "+d+" km"); } }
4.3
4.4
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the side: "); double s = input.nextDouble(); double area = 6 * s * s / (4 * Math.tan(Math.PI / 6)); System.out.printf("The area of the hexagon is %.2f",area); } }
4.5
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of sides:"); int num = input.nextInt(); System.out.print("Enter the side:"); double s = input.nextDouble(); double area = num * s * s / (4 * Math.tan(Math.PI / num)); System.out.println("The area of the polygon is "+area); } }
4.6