算法(第四版) chapter 1.1部分习题答案

来源:互联网 发布:怎样提高淘宝店铺流量 编辑:程序博客网 时间:2024/05/16 19:42

1.1.13 编写一段代码,打印出一个二维布尔数组的内容,其中,使用*表示真,空格表示假。打印出行号和列号

public static void printBoolArray(boolean a[][], int row, int col){    for(int i=1; i<=row+1; i++){        for(int j=1; j<=col+1; j++){            if(i==1){                if(j==col+1)                    break;                System.out.print("  "+j);                continue;            }            else{                if(j==1)                    System.out.print(i-1+" ");                else{                    if(a[i-2][j-2]==true)   System.out.print("*  ");                    else System.out.print("   ");                }            }        }        System.out.println();    }}

1.1.13 编写一段代码,打印出一个M行N列的二维数组的转置

public static int[][] switchMartix(int[][] a, int row, int col){    int[][] b=new int[col][row];    for(int i=0; i<col; i++){        for(int j=0; j<row; j++){            b[j][i]=a[i][j];        }    }    return b;}

1.1.20 编写一个递归的静态方法计算ln(N!)的值

public static double cLog(int n){    if(n==1 || n==0) return 0;    else return Math.log(n)+cLog(n-1);}
0 0
原创粉丝点击