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

本章节为习题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 中存储不同类型数据可以用类~

还没想好~

明日再补~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容