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

public String getDescription() { return description; } }

11.9

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

public class Exercise11_09 {

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

System.out.println(\); int[][] matrix = new int[n][n];

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

matrix[i][j] = (int)(Math.random() * 2); System.out.print(matrix[i][j]); }

System.out.println(); }

// Check rows

int rowSum = sumRow(matrix[0]);

ArrayList list = new ArrayList(); list.add(0);

for (int i = 1; i < n; i++) { if (rowSum < sumRow(matrix[i])) { rowSum = sumRow(matrix[i]); list.clear(); list.add(i); }

else if (rowSum == sumRow(matrix[i])) { list.add(i); } }

System.out.print(\);

for (int i = 0; i < list.size() - 1; i++) System.out.print(list.get(i) + \); System.out.print(list.get(list.size() - 1));

// Check columns

int columnSum = sumColumn(matrix, 0); list.clear(); list.add(0);

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

if (columnSum < sumColumn(matrix, i)) { columnSum = sumColumn(matrix, i); list.clear(); list.add(i); }

else if (columnSum == sumColumn(matrix, i)) { list.add(i); } }

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

public static int sumRow(int row[]) { int sum = 0;

for (int i = 0; i < row.length; i++) sum += row[i]; return sum; }

public static int sumColumn(int matrix[][], int column) { int sum = 0;

for (int i = 0; i < matrix.length; i++) sum += matrix[i][column]; return sum; } }

11.10

import java.util.Scanner;

public class Exercise11_10 {

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

MyStack stack = new MyStack();

for (int i = 0; i < 5; i++) stack.push(input.nextLine());

System.out.println(stack.getSize() + \); while (!stack.isEmpty())

System.out.println(stack.pop()); } }

// MyStack.java: Implementing a stack using inheritance class MyStack extends java.util.ArrayList { public boolean isEmpty() { return super.isEmpty(); }

public int getSize() { return size(); }

public Object peek() { return get(getSize() - 1); }

public Object pop() {

return remove(getSize() - 1); }

public Object push(Object o) { add(o); return o; }

public int search(Object o) { return indexOf(o); }

public String toString() { return \ + toString();

} }

11.11

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

public class Exercise11_11 {

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

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

sort(list);

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

public static void sort(ArrayList list) { for (int i = 0; i < list.size() - 1; i++) {

// Find the minimum in the list[i..list.length-1] int currentMin = list.get(i); int currentMinIndex = i;

for (int j = i + 1; j < list.size(); j++) { if (currentMin > list.get(j)) { currentMin = list.get(j); currentMinIndex = j; } }

// Swap list.get(i) with list.get(currentMinIndex) if necessary; if (currentMinIndex != i) {

list.set(currentMinIndex, list.get(i)); list.set(i, currentMin); } } } }

11.12

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

public class Exercise11_12 {

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

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

System.out.println(\ + sum(list)); }

public static double sum(ArrayList list) { double sum = 0;

for (int i = 0; i < list.size(); i++) sum += list.get(i); return sum; } }

11.13

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

public class Exercise11_13 {

public static void main(String[] args) { System.out.print(\); ArrayList list = new ArrayList<>(); Scanner input = new Scanner(System.in); for (int i = 0; i < 10; i++) { list.add(input.nextInt()); }

removeDuplicate(list);

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

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