《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十一章练习题答案

public static void removeDuplicate(ArrayList list) { ArrayList temp = new ArrayList(); for (int i = 0; i < list.size(); i++) if (!temp.contains(list.get(i))) temp.add(list.get(i));

list.clear();

for (int i = 0; i < temp.size(); i++) list.add(temp.get(i)); } }

11.14

import java.util.Scanner; import java.util.ArrayList;

public class Exercise11_14 {

public static void main(String[] args) { Scanner input = new Scanner(System.in);

System.out.print(\); ArrayList list1 = new ArrayList<>(); for (int i = 0; i < 5; i++) list1.add(input.nextInt());

System.out.print(\); ArrayList list2 = new ArrayList<>(); for (int i = 0; i < 5; i++) list2.add(input.nextInt());

ArrayList list3 = add(list1, list2);

System.out.print(\); for (int i = 0; i < list3.size(); i++) System.out.print(list3.get(i) + \); }

public static ArrayList add(

ArrayList list1, ArrayList list2) { ArrayList result = new ArrayList<>();

for (int i = 0; i < list1.size(); i++) result.add(list1.get(i));

for (int i = 0; i < list2.size(); i++) result.add(list2.get(i));

return result; } }

11.15

import java.util.Scanner; import java.util.ArrayList;

public class Exercise11_15 {

public static void main(String[] args) { Scanner input = new Scanner(System.in);

System.out.print(\); int n = input.nextInt();

System.out.print(\); ArrayList x = new ArrayList<>(); ArrayList y = new ArrayList<>();

for (int i = 0; i < n; i++) { x.add(input.nextDouble()); y.add(input.nextDouble()); }

double total = 0;

for (int i = 1; i < n - 1; i++)

total += getArea(x.get(0), y.get(0), x.get(i), y.get(i), x.get(i + 1), y.get(i + 1));

System.out.print(\ + total); }

public static double getArea(double x1, double y1, double x2, double y2, double x3, double y3) {

double s1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 -y2)); double s2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -y3)); double s3 = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 -y2));

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

return Math.sqrt(s * (s - s1) * (s - s2) * (s - s3)); }

}

11.16

import java.util.Scanner; import java.util.ArrayList;

public class Exercise11_16 {

public static void main(String[] args) {

ArrayList list = new ArrayList();

int number1 = (int) (Math.random() * 10); int number2 = (int) (Math.random() * 10);

// Create a Scanner

Scanner input = new Scanner(System.in); System.out.print(

\ + number1 + \ + number2 + \); int answer = input.nextInt(); list.add(answer);

while (number1 + number2 != answer) { System.out.print(

\Try again. What is \ + number1 + \ + number2 + \); answer = input.nextInt();

if (list.contains(answer)) {

System.out.println(\ + answer); } else {

list.add(answer); } }

System.out.println(\); } }

11.17

import java.util.ArrayList; import java.util.Scanner;

public class Exercise11_17 {

public static void main(String[] args) { System.out.print(\); Scanner input = new Scanner(System.in); int m = input.nextInt();

ArrayList list = new ArrayList(); int number = m; int factor = 2;

while (factor <= number) { if (number % factor == 0) { list.add(factor);

number = number / factor; } else factor++; }

int n = 1; int i = 0;

while (i < list.size() - 1) {

if (list.get(i) != list.get(i + 1)) { n *= list.get(i); i += 1; } else i += 2; }

if (i == list.size() - 1) n *= list.get(i);

System.out.println(\is \ + n);

System.out.println(\ + m * n); } }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4