Java语言程序设计(第3版)沈泽刚主编第5章课后习题答案

5.1 编写程序,从键盘输入5个整数,存到一个数组,计算所有元素的最大值,最小值和平均值。

import java.util.Scanner;

/**
 * 5.1 编写程序,从键盘输入5个整数,存到一个数组,计算所有元素的最大值,最小值和平均值。
 */
public class IntArray {
    public static void main(String[] args){
        System.out.print("键盘输入5个整数:") ;
        Scanner input = new Scanner(System.in) ;
        int [] num = new int [5] ;
        int sum = 0 ;
        int max = Integer.MIN_VALUE ;
        int min = Integer.MAX_VALUE ;
        for(int i=0; i<num.length; i++){
            num[i] = input.nextInt() ;
        }
        for(int i=0; i<num.length; i++){
            if(max <= num[i]){
                max = num[i] ;
            }
            if(min >= num[i]){
                min = num[i] ;
            }
            sum += num[i] ;
        }
        System.out.println("最大值:" + max) ;
        System.out.println("最小值:" + min) ;
        System.out.println("平均值:" + (1.0 * sum / num.length)) ;
    }
}

5.2 编写程序,随机产生100个16之间的整数,统计每个数出现的次数,然后随机产生1000个16之间的整数,统计每个数出现的次数。

public class RandomTest {
    public static void main(String[] args){
        int [] num3 = new int [6] ; //统计次数
        int [] num4 = new int [6] ;

        for(int k=0; k<100; k++){
           int r = (int)(Math.random() * 6) + 1 ;
            switch(r){
                case 1 : num3[0] ++ ; break ;
                case 2 : num3[1] ++ ; break ;
                case 3 : num3[2] ++ ; break ;
                case 4 : num3[3] ++ ; break ;
                case 5 : num3[4] ++ ; break ;
                case 6 : num3[5] ++ ; break ;
            }
        }
        for(int m=0; m<1000; m++){
            int r = (int)(Math.random() * 6) + 1 ;
            switch(r){
                case 1 : num4[0] ++ ; break ;
                case 2 : num4[1] ++ ; break ;
                case 3 : num4[2] ++ ; break ;
                case 4 : num4[3] ++ ; break ;
                case 5 : num4[4] ++ ; break ;
                case 6 : num4[5] ++ ; break ;
            }
        }
        System.out.print("100个整数中,1~6出现的次数分别为:" ) ;
        for(int k=0; k<num3.length; k++){
            System.out.print(num3[k] + " ") ;
        }
        System.out.println() ;
        System.out.print("1000个整数中,1~6出现的次数分别为:" ) ;
        for(int k=0; k<num3.length; k++){
            System.out.print(num4[k] + " ") ;
        }
    }
}

5.3 编写一个方法,求一个double型数组的最小值。

import java.util.Scanner;

/**
 * 5.3 编写一个方法,求一个double型数组的最小值。
 */
public class ArrayDemo {
    public static Double min(double [] array){
        double min = Double.MAX_VALUE ;
        for(int i=0; i<array.length; i++){
            if(min >= array[i]){
                min = array[i] ;
            }
        }
        return min ;
    }
    public static void main(String[] args){
        double [] array = new double [5] ;
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入5个double型的数字:") ;
        for(int i=0; i<array.length; i++){
            array[i] = input.nextDouble() ;
        }
        System.out.println("最小值:" + min(array)) ;
    }
}

5.4 定义一个有10个元素的数组,前5个元素与后5个元素交换,第1个与第10个交换,依次类推。

public class ArrayChange {
    public static void main(String[] args){
        int [] array = {1,2,3,4,5,6,7,8,9,10} ;
        for(int arr : array){
            System.out.print(arr + " ") ;
        }
        System.out.println() ;
        for(int i=0; i<array.length/2; i++){
            int temp = array[i] ;
            array[i] = array[array.length-1-i] ;
            array[array.length-1-i] = temp ;
        }
        for(int arr1 : array){
            System.out.print(arr1 + " ") ;
        }

    }
}

5.5 定义一个含有8个元素的数组,选择排序进行升序排序。

public class ArraySort {
    public static void main(String[] args){
        int [] array = {2,1,4,3,5,6,8,7} ;
        for(int i=0; i<array.length; i++){
            int key = array[i] ;
            for(int j=i+1; j<array.length; j++){
                if(key > array[j]){
                    int temp = key ;
                    key = array[j] ;
                    array[j] = temp ;
                }
            }
            array[i] = key ;
        }
        for(int arr : array){
            System.out.print(arr + " ") ;
        }
    }
}

5.6 编程打印斐波那契数列的前20个数字。

public class Fibonacci {
    public static void main(String[] args){
        int [] f = new int [20] ;
        f[0] = 1 ;
        f[1] = 1 ;
        for(int i=2; i<f.length; i++){
            f[i] = f[i-1] + f[i-2] ;
        }
        for(int f1 : f){
            System.out.print(f1 + " ") ;
        }
    }
}

5.7 编写一个方法,计算两个数组之和。

import java.util.Arrays;

/**
 * 5.7 编写一个方法,计算两个数组之和。
 */
public class SumArray {
    public static int[] sumArray(int [] a, int [] b){
        int length = Math.max(a.length, b.length) ;
        a = Arrays.copyOf(a,length) ;
        b = Arrays.copyOf(b,length) ;
        int [] r = new int [length] ;
        for(int i=0; i<length; i++){
            r[i] = a[i] + b[i] ;
        }
      return r ;
    }
    public static void main(String[] args){
        int [] a = {1,2,4} ;
        int [] b = {2,4,6,8} ;
        int [] c = sumArray(a,b) ;
        for(int d : c){
            System.out.print(d + " ") ;
        }
    }
}

5.8 编写方法,合并两个数组,并按升序返回合并后的数组。

import java.util.Arrays;

/**
 * 5.8 编写方法,合并两个数组,并按升序返回合并后的数组。
 */
public class ArrayMerge {
    public static int[] arrayMerge(int [] a, int [] b){
        int [] array = new int[a.length + b.length] ;
        for(int i=0; i<a.length; i++){
            array[i] = a[i] ;
        }
        for(int j=array.length-a.length-3, k=0; j<array.length; j++,k++){
            array[j] = b[k]  ;
        }
        Arrays.sort(array) ;
        return array ;
    }
    public static void main(String[] args){
        int [] a = {16,13,15,18} ;
        int [] b = {9, 13, 15, 16, 29, 36, 100} ;
        int [] array = arrayMerge(a, b) ;
        for(int arr : array){
            System.out.print(arr + " ") ;
        }
    }
}

5.9 编写程序,使用方法求解一元二次方程根的个数。

import java.util.Scanner;

/**
 * 5.9 编写程序,使用方法求解一元二次方程根的个数。
 */
public class SolveQuadratic {
    public static int solveQuadratic(double [] eqn, double [] roots){
        double discriminant = (eqn[1] * eqn[1]) - (4 * eqn[0] * eqn[2]) ;
        if(discriminant > 0){
            roots[0] = ((-eqn[1]) + Math.sqrt(discriminant)) / (2 * eqn[0])  ;
            roots[1] = ((-eqn[1]) - Math.sqrt(discriminant)) / (2 * eqn[0])  ;
            return 2 ;

        }else if(discriminant == 0){
            roots[0] = (-eqn[1]) / (2 * eqn[0]) ;
            roots[1] = (-eqn[1]) / (2 * eqn[0]) ;
            return 1 ;
        }else{
            return 0 ;
        }
    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入一元二次方程的系数:a,b,c:") ;
        double [] roots = new double [2] ;
        double [] eqn = new double [3] ;
        for(int i=0; i<eqn.length; i++){
            eqn[i] = input.nextDouble() ;
        }
        System.out.println("一元二次方程有" + solveQuadratic(eqn,roots) + "个根") ;
    }
}

5.10 编写程序,筛选出2~100的所有素数。

public class PrimeNumber {
    public static boolean isPrime(int n){
        for(int i=2; i<n; i++){
            if(n % i == 0){
                return false ;
            }
        }
        return true ;
    }
    public static void main(String[] args){
        for(int i=2; i<=100; i++){
            if(isPrime(i)){
                System.out.print(i + " ") ;
            }
        }
    }
}

5.11 编写程序,List1和list2数组长度相同,判断数组list1和list2是否完全相同

public class equals {
    public static boolean equals(int [] list1, int [] list2){
        for(int i=0; i<list1.length; i++){
            if(list1[i] != list2[i]){
                return false ;
            }
        }
        return true ;
    }
    public static void main(String[] args){
        int [] list1 = {1,2,3,4,5} ;
        int [] list2 = {1,2,3,4,5} ;
        int [] list3 = {1,2,4,3,5} ;
        System.out.println(equals(list1, list2)) ;
        System.out.println(equals(list1, list3)) ;
    }
}

5.12 编写求解约瑟夫问题。有12个人排成一圈,从1号开始报数,数到5的人就离开,然后继续报数,试问最后剩下的一个人是谁?

public class Josephus {
    public static void main(String[] args) {
        int[] array = new int[12];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        int n = 0;
        int count = 0;
        while (true) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] != 0) {
                    n++;
                    if (n % 5 == 0) {
                        System.out.println(array[i] + "离开!");
                        array[i] = 0;
                        count++;
                    }
                }
            }
            if(count == 11){
                break ;
            }
        }

        for(int j=0; j<array.length; j++){
            if(array[j] != 0){
                System.out.println(array[j]) ;
            }
        }
    }
}

5.13 编写程序,从一副牌中选出4张,计算它们的和,程序显示得到和为24的选牌次数。

public class TwentyFour {
    public static void main(String[] args){
        int [] cards = new int [52] ;
        for(int i=0; i<cards.length; i++){
            cards[i] = i ;
        }
        int sum = 0, count = 0 ;
        while(sum != 24){
            for(int i=0; i<4; i++){
                int j = (int)(Math.random() * 52) ;
                int temp = cards[i] ;
                cards[i] = cards[j] ;
                cards[j] = temp ;
            }
            sum = 0;
            for(int i=0; i<4; i++){
                sum += cards[i] ;
            }
            count ++ ;
        }
        System.out.println(sum) ;
        System.out.println("选牌次数为:" + count) ;
    }
}

5.14 编写程序,提示用户从键盘输入一个整数,然后使用栈以降序输出该数的最小因子。

import java.util.Scanner;
import java.util.Stack;

/**
 * 5.14 编写程序,提示用户从键盘输入一个整数,然后使用栈以降序输出该数的最小因子。
 */
public class UseOfStack {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        Stack<Integer> stack = new Stack<>() ;
        System.out.print("请输入一个整数:") ;
        int n = input.nextInt() ;
        int value = n ;
        for(int i=2; i<n; i++){
            if(value % i == 0){
                stack.push(i) ;
                value = value / i ;
                i -- ;
            }
        }
        while(!stack.isEmpty()){
            System.out.println(stack.pop()) ;
        }
    }
}

5.15 计算两个矩阵A,B的和,差,转置。

public class MatrixDemo {
    public static void main(String[] args) {
        int [][] A = {{1, 3, 5}, {-3, 6, 0}, {13, -5, 7}, {-2, 19, 25}};
        int [][] B = {{0, -1, -2}, {7, -1, 6}, {-6, 13, 2}, {12, -8, -13}};
        int [][] sum = new int[4][3];
        int [][] sub = new int[4][3];
        int [][] tran = new int [3][4] ;

        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < A[0].length; j++) {
                sum[i][j] = A[i][j] + B[i][j];
                sub[i][j] = A[i][j] - B[i][j] ;
            }
        }
        for(int i=0; i<A.length; i++) {
            for (int j = 0; j < A[0].length; j++) {
                System.out.print(sum[i][j] +  " ") ;
            }
            System.out.println() ;
        }
        for(int i=0; i<A.length; i++) {
            for (int j = 0; j < A[0].length; j++) {
                System.out.print(sub[i][j] +  " ") ;
            }
            System.out.println() ;
        }
        for(int i=0; i<tran.length; i++) {
            for (int j = 0; j < tran[0].length; j++) {
                tran[i][j] = A[j][i] ;
            }
        }


        for(int i=0; i<tran.length; i++){
            for(int j=0; j<tran[0].length; j++){
                System.out.print(tran[i][j] + " ") ;
            }
            System.out.println() ;
        }

    }
}

5.16 编写一个方法,返回二维数组最大元素的位置

import java.util.Scanner;

/**
 * 5.16 编写一个方法,返回二维数组最大元素的位置
 */
public class MaxLocation {
    public static int [] locateLargest(double [][] a){
        int [] index = new int [2] ;
        double max = Double.MIN_VALUE ;
        for(int i=0; i<a.length; i++){
            for(int j=0; j<a[0].length; j++){
                if(max <= a[i][j]){
                    max = a[i][j] ;
                    index[0] = i ;
                    index[1] = j ;
                }
            }
        }
        return index ;
    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入数组的行数和列数:") ;
        int row = input.nextInt() ;
        int col = input.nextInt() ;
        System.out.print("请输入数组中的元素:") ;
        double [][] array = new double[row][col] ;
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                array[i][j] = input.nextDouble() ;
            }
        }
        int [] index = locateLargest(array) ;
        System.out.println("最大元素的位置是:" + "(" + index[0] + "," + index[1] + ")") ;

    }
}