admin管理员组

文章数量:1652183

1.4.4 Write a code fragment that reverses the order of the values in a one-dimensional string array. Do not create another array to hold the result. Hint : Use the code in the text for exchanging the values of two elements.

public class Work {  
    public static void main(String[] args) { 
        int[] a = {1,2,3,4,5};//在这里改要逆序的数组
        int n = a.length;
        for (int i = 0; i < n/2; i++){
            int temp = a[i];
            a[i] = a[n-1-i];
            a[n-1-i] = temp;
        }
        for (int i = 0; i <= n-1; i++){
            System.out.print(a[i]+" ");//不能用单引号' '
        }
        //System.out.println();
    }
}


1.4.15 Write a code fragment to transpose a square two-dimensional array in place without creating a second array.

public class Transpose {

    public static void main(String[] args) {

        // create n-by-n matrix
        int n = Integer.parseInt(args[0]);
        int[][] a = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = n*i + j;
            }
        }

        // print out initial matrix
        System.out.println("Before");
        System.out.println("------");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%4d", a[i][j]);
            }
            System.out.println();
        }

        // transpose in-place
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int temp = a[i][j];
                a[i][j] = a[j][i];
                a[j][i] = temp;
            }
        }

        // print out transposed matrix
        System.out.println();
        System.out.println("After");
        System.out.println("------");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%4d", a[i][j]);
            }
            System.out.println();
        }

    }
}

输入:3

输出:

Before
------
   0   1   2
   3   4   5
   6   7   8

After
------
   0   3   6
   1   4   7
   2   5   8

Transpose.java (princeton.edu)


1.4.33 Find a duplicate. Given an integer array of length n, with each value between 1 and n, write a code fragment to determine whether there are any duplicate values. You may not use an extra array (but you do not need to preserve the contents of the given array.)

public class Repeated {
    public static void main(String[] args) 
    {
        int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2, 9, 2};
        findDupicateInArray(my_array);
 
    }
 
    public static void findDupicateInArray(int[] a) {
        int count=0;
        for(int j=0;j<a.length;j++) {
            for(int k =j+1;k<a.length;k++) {
                if(a[j]==a[k]) {
                    count++;
                }
            }
            if(count==1)
               System.out.println("repeated:"+a[j]);
            count = 0;
        }
    }
}

Java 实例 – 查找数组中的重复元素 | 菜鸟教程 (runoob)


1.4.41 Binomial distribution. Write a program that takes an integer command-line argument n and creates a two-dimensional ragged array a[][] such that a[n] [k] contains the probability that you get exactly k heads when you toss a fair coin n times. These numbers are known as the binomial distribution: if you multiply each element in row i by 2 n, you get the binomial coefficients—the coefficients of x k in (x+1)n—arranged in Pascal’s triangle. To compute them, start with a[n][0] = 0.0 for all n and a[1][1] = 1.0, then compute values in successive rows, left to right, with a[n][k] = (a[n-1][k] + a[n-1][k-1]) / 2.0.

Pascal’s triangle                                      binomial distribution
1                                                              1
11                                                             1/2 1/2
1 2 1                                                         1/4 1/2 1/4
1 3 3 1                                                      1/8 3/8 3/8 1/8
1 4 6 4 1                                                   1/16 1/4 3/8 1/4 1/16

public class BinomialCoefficients {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);

        int[][] pascal = new int[n+1][];

        // initialize first row
        pascal[1] = new int[1 + 2];
        pascal[1][1] = 1;

        // fill in Pascal's triangle
        for (int i = 2; i <= n; i++) {
            pascal[i] = new int[i+2];
            for (int k = 1; k < pascal[i].length - 1; k++)
                pascal[i][k] = pascal[i-1][k-1] + pascal[i-1][k];
        }

        // print binomial coefficients
        int denominator = 1;
        for (int i = 1; i <= n; i++) {
            for (int k = 1; k < pascal[i].length - 1; k++) {
                System.out.print(pascal[i][k] + "/" + denominator + " ");
            }
            System.out.println();
            denominator += denominator;
        }
    }

}

 BinomialCoefficients.java (princeton.edu)


本文标签: 导论计算机科学答案方法Java