Introduction to Programming in Java - An Interdisciplinary Approach 计算机科学导论-跨学科方法 部分练习答案2.1

2.1.16 Write a static method scale() that takes a double array as its argument and has the side effect of scaling the array so that each element is between 0 and 1 (by subtracting the minimum value from each element and then dividing each element by the difference between the minimum and maximum values). Use the max() method defined in the table in the text, and write and use a matching min() method.

public class Work{
    public static double max(double[] a){
        double max = Double.NEGATIVE_INFINITY;
        for(int i = 0; i<a.length; i++){
            if(a[i]>max){
                max=a[i];
            }
        }
        return max;
    }
    public static double min(double[] a){
        double min = Double.POSITIVE_INFINITY;
        for(int i = 0; i<a.length; i++){
            if(a[i]<min){
                min=a[i];
            }
        }
        return min;
    }
    public static double[] scale(double[] a){
        double[] b = new double[a.length];
        double low = min(a);
        double high = max(a);
        for(int i = 0; i<a.length; i++){
            b[i]=(a[i]-low)/(high-low);
        }
        return b;
    }
    public static double[] randomArray(int n){
        double[] a = new double[n];
        for(int i = 0; i<n; i++){
            a[i]=Math.random()*10;
        }
        return a;
    }
    public static void main(String[] args){
        int n = Integer.parseInt(args[0]);
        double[] a = randomArray(n);
        double[] b = scale(a);
        for(int i = 0; i<n; i++){
            System.out.print(b[i]+" ");
        }
 
    }
 
}

2.1.17 Write a static method reverse() that takes an array of strings as its argument and returns a new array with the strings in reverse order. (Do not change the order of the strings in the argument array.) Write a static method reverseInplace() that takes an array of strings as its argument and produces the side effect of reversing the order of the strings in the argument array.

public class Work {
 
    /* 反转数组*/
    static void reverse(int a[], int n)
    {
        int[] b = new int[n];
        int j = n;
        for (int i = 0; i < n; i++) {
            b[j - 1] = a[i];
            j = j - 1;
        }
        /*输入反转数组*/
        System.out.println("reversed array:");
        for (int k = 0; k < n; k++) {
            System.out.print(b[k]+" ");
        }
    }

    static void reverseInplace(int a[], int n)
    {
        int i, k, t;
        for (i = 0; i < n / 2; i++) {
            t = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = t;
        } 
        System.out.println("reversed array:");
        for (k = 0; k < n; k++) {
            System.out.print(a[k]+" ");
        }
    }
 
    public static void main(String[] args)
    {
        int [] arr = {10, 20, 30, 40, 50};
        reverse(arr, arr.length);
        System.out.println("\n reversing the order of the strings in the argument array:");
        reverseInplace(arr, arr.length);
    }
}

Java 实例 – 数组反转 | 菜鸟教程 (runoob.com)


2.1.19 Write a static method histogram() that takes an int array a[] and an integer m as arguments and returns an array of length m whose ith element is the number of times the integer i appeared in a[]. Assuming the values in a[] are all between 0 and m-1, the sum of the values in the returned array should equal a.length.

public class Work{
    public static int[] histogram(int[] a,int m){
        int[] b = new int[m];
        int n = a.length;
        for(int i = 0; i<n; i++){
            for(int j = 0; j<m; j++){
                if(a[i]==j){
                    b[j]++;
                }
            }
        }
        return b;
    }
    public static void main(String[] args){

        int m = Integer.parseInt(args[0]);
        int[] a = {1,2,2,3,3};
        int[] c = histogram(a, m);
        int n = a.length;
        for(int i = 0; i<n; i++){
            System.out.print(a[i]+" ");}
        System.out.print("\n");
        for(int j = 0; j<m; j++){
            System.out.print(c[j]+" ");
        }
    
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值