美文网首页
《算法》第四版答案(四)习题1.1

《算法》第四版答案(四)习题1.1

作者: HilaryLi | 来源:发表于2017-10-21 21:18 被阅读0次

本章节为习题1.1.1-1.1.25的答案

  • 1.1.1
    答案
a. 7  
b. 200.0000002 (科学记数法,e后面的数字表示10的多少次方) 
c. true
  • 1.1.2
    答案
a.1.618(注意不是1哦)   表达式类型为浮点型
b. 10.0               表达式类型为浮点型
c.true                表达式类型为布尔类型                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
d.33                  表达式为String类型(3+“3”时int和String型的运算,结果会自动转换为String类型)   

ps:基本数据类型转换遵循规则:

容量小的类型自动转换成容量大的数据类型,数据类型按照容量大小排序为:

byte,short,char<int<long<float<double
但String类型不能自动转换为int类型 
  • 1.1.3
    答案
答案一:
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class TestUqual {
    
    public static void main(String[] args) {
        
        int a,b,c;
        a=b=c=0;
        StdOut.println("Please enter three numbers:");
        a = StdIn.readInt();//作者自己写的库 从命令行输入
        b = StdIn.readInt();
        c = StdIn.readInt();
        
        //调用函数进行判断并输出结果
        if (equals(a,b,c)==1) StdOut.println("equal");
        else StdOut.println("not equal");   
        }

    //判断三个数是否相等
    public static int equals(int a, int b, int c) {
        // TODO Auto-generated method stub
        if(a==b&&b==c) return 1;
        else return 0;      
    }

}
########################################
答案二:
public class ex_1_1_3 {
    
    public static void main(String[] args) {
        
        int number1 = Integer.parseInt(args[0]);
        int number2 = Integer.parseInt(args[1]);
        int number3 = Integer.parseInt(args[2]);
        
        if(number1 == number2){
            if(number2 == number3)
                System.out.println("equal");
        }
        else System.out.println("not equal");
        
    }

}
  • 1.1.4
    答案
a. if (a > b)  c = 0;   
b. if (a > b) { c = 0; }
  • 1.1.5
    答案
public class TestUqual {

     public static  void main(String[] args)  
       { 
           double x; double y; 
           x=StdIn.readDouble(); 
           y=StdIn.readDouble(); 
           StdOut.print(compare(x)&& compare(y)); 
       } 
     public static boolean compare(double x) 
        { 
             if(x>0&&x<1)  returen  ture; 
            else                 return   false; 
       } 
  } 
  • 1.1.6
    答案
0  1  1 2 3 5 8 13 21 34 55 89 144 233 377 610
  • 1.1.7
    答案
a.3.00009  b.499500  c. 10000
  • 1.1.8
    答案
a. b    b.  bc   c. e

  • 1.1.9
    答案
原答案:
import java.util.Scanner;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdOut;

public class exerce1 {
    
    public static void main(String[] args) {
                
        Stack<Integer> sta = new Stack<Integer>();//注意不能为int
        Scanner s = new Scanner(System.in);
        String st = "";//转换为String类型的值
        int N = s.nextInt();
        while(N != 0){
            int m = N%2;
            sta.push(m);
            N = N/2;        
        }
        
        while(!sta.isEmpty())
            StdOut.print(st+sta.pop());
    }

第二次修改的答案(另一种方法)

public static String decimalToBinary(int n) {
        String resultString = "";
        for (int i = 31; i >= 0; i--)
            resultString = resultString + (n >>> i & 1);//(与运算符:两个操作数中位都为1,结果才为1,否则结果为0)
        return resultString;
    }

解析:“>>>”运算符所作的是无符号的位移处理,它不会将所处理的值的最高位视为正负符号,所以作位移处理时,会直接在空出的高位填入0。
    当我们要作位移的原始值并非代表数值时(例如:表示颜色图素的值,最高位并非正负号),可能就会需要使用此种无符号的位移。
    无符号右移的意思,忽略符号位,空位都以0补齐 
    value >>> num     --   num 指定要移位值value 移动的位数
    移位运算符大于&运算符
    如数字为-17     二进制为:11111111 11111111 11111111 11101111  -17>>>2为(无符号右移2位)后为 00111111 11111111 11111111 11111011

运算符优先级查询地址

  • 1.1.10
    答案
它没有用new为a[ ]分配内存
这段代码会产生一个variable a might not have been initialized的编译错误。
  • 1.1.11
    答案
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class ex1_1_11 {
    
    public static void main(String[] args) {
        
        boolean a[][] = new boolean[10][10];
        a = RandomInitial(a);//随机初始化
        TestPrint(a);//打印数组
    }

    private static void TestPrint(boolean[][] a) {
        // TODO Auto-generated method stub
        for(int i = 0;i<a.length;i++){
            StdOut.print(" "+i);//打印行号
        }
        StdOut.print(" ");
        for(int i = 0;i<10;i++){
            StdOut.print(i);//打印列号
            for(int j = 0;j<10;j++){
                if(a[i][j]) StdOut.print("*"+" ");
                else        StdOut.print(" "+" ");
            }
            
            StdOut.println(" ");
        }
        
        
    }

    //随机产生布尔数组
    private static boolean[][] RandomInitial(boolean[][] a) {
        // TODO Auto-generated method stub
        
        for(int i = 0;i<a.length;i++){
            for(int j = 0;j<a.length;j++){
                
                if(StdRandom.bernoulli(0.1)) a[i][j] = true;
                else                         a[i][j] = false;               
            }
            
            
        }
        
        return a;
    }
}
  • 1.1.12
    答案
0 1 2 3 4 5 6 7 8 9 
  • 1.1.13
    答案
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;


public class ex1_1_13 {
    
    public static void main(String[] args) {
        final int M = 4;
        final int N = 4;
        
        int a[][] = new int [M][N];
        int b[][] = new int [N][M];//不能用同一个数组
        
        a = RandomInitial(a,N);//初始化二维数组
        b = MigrateArrays(a,b);//转置二维数组
        MigratePrint(b);//输出转置后的二维数组
                
    }

    private static void MigratePrint(int[][] b) {
        // TODO Auto-generated method stub
        StdOut.print("转置后的二维数组为:");
        StdOut.println();
        for(int i = 0; i<b.length;i++){
            for(int j = 0;j<b[0].length;j++){
                StdOut.print(b[i][j]+" ");
            }
            StdOut.println();
        }
        
    }

    private static int[][] MigrateArrays(int[][] a, int[][] b) {
        // TODO Auto-generated method stub
        
        for(int i = 0; i<a.length;i++){
            for(int j = 0;j<a[0].length;j++){
                b[j][i] = a[i][j];
            }
        }
        
        return b;
    }

    private static int[][] RandomInitial(int[][] a, int n) {
        // TODO Auto-generated method stub
        
        StdOut.println("初始化二维数组:"); 

        for(int i = 0; i<a.length;i++){
            for(int j = 0;j<a[0].length;j++){
                a[i][j] = StdRandom.uniform(n);
                StdOut.print(a[i][j]+" ");
            }
            
        StdOut.println();
        
    }
        return a;

}
}
  • 1.1.14
    答案
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class ex1_1_14 {
    
    public static int lg(int N){
        int j = 0;
        while(N!=1){
            N = N/2;
            j++;
        }
        return j;
    }
    
    public static void main(String[] args) {
        int m = StdIn.readInt();
        StdOut.print(lg(m));    
    }
}

  • 1.1.15
    答案
public static int[] histogram(int a[],int M){
        
        int b[] = new int[M];
        int n = 0;
        int m = 0;
        for(int i = 0;i<M;i++){
            for(int j =0;j<a.length;j++){
                if(i == a[j]) n++;
                b[i] = n;
            }
            n = 0;
        }
        
        for(int i = 0;i<M;i++){
            m = m+b[i];
        }
        
        return b;
        
    }
  • 1.1.16
    答案
311361142246
(不懂诶,程序输出的是上面的这个值,而我算的是32)
  • 1.1.17
    答案
这段代码中的基础情况永远不会被访问。
调用exR2(3) 会产生调用exR2(0)、exR2(-3) 和exR2(-6),
循环往复直到发生StackOverflowError。
  • 1.1.18
    答案
50   33   a*b
 2^25   3^11 
  • 1.1.19
    答案
public class Fibonacci {
 
     // Fibonacci数列计算,时间空间复杂度优化版
      private static int M = 100;
      private static long[] fib = new long[M];
      public static long fibonacciOptimization(int N) {
         if(0 == N)
              fib[0] = 0;
          else if(1 == N)
             fib[1] = 1;
         else
             fib[N] = fib[N - 1] + fib[N -2];
         return fib[N];
     }
 
     public static void main(String[] args) {
        for(int N = 0; N < 100; ++N) {
             fib[N] = fibonacciOptimization(N);
             StdOut.println(N + "\t" + fib[N]);
         }
     }
 
}
  • 1.1.20
    答案
    前提知识:
    111.png
     512的以2为底的对数是:

double log = Math.log(512, 2);

public class ex1_1_20 {
    
    public static double factorialln(double N){
        double a;
        if (N>1)
            return Math.log(N)+factorialln(N-1);
        else
            return 0;
        
    }
    
    public static void main(String[] args) {
        
        System.out.println(factorialln(35));
        
        
    }
}
  • 1.1.21
    答案
java 中存储不同类型数据可以用类~

还没想好~

明日再补~

相关文章

网友评论

      本文标题:《算法》第四版答案(四)习题1.1

      本文链接:https://www.haomeiwen.com/subject/gncwuxtx.html