欢迎交流

  1.1.1

  a. 7

  b. 200.0000002

  c. true

  1.1.2

  a. 1.618

  b. 10.0

  c. true

  d. 33

  1.1.3

  1. public class MainApp {
  2. public static void main(String[] args) {
  3.  
  4. int a = Integer.parseInt(args[0]);
  5. int b = Integer.parseInt(args[1]);
  6. int c = Integer.parseInt(args[2]);
  7.  
  8. if(a == b && b == c) {
  9. System.out.println("equal");
  10. } else {
  11. System.out.println("not equal");
  12. }
  13. }
  14. }

   1.1.4

  a. 去掉"then"

  b. a > b 外围加括号

  c. 正确

  d. else 之前加冒号

  1.1.5

  1. public class MainApp {
  2. public static void main(String[] args) {
  3.  
  4. Double a = Double.parseDouble(args[0]);
  5. Double b = Double.parseDouble(args[1]);
  6.  
  7. if(a >= 0 && a <= 1 && b >=0 && b <= 1) {
  8. System.out.println("true");
  9. } else {
  10. System.out.println("false");
  11. }
  12. }
  13. }

  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

  1. int N = ;
  2. String s = "";
  3. for(int i = N; i > ; i /= ) {
  4. s = i % + s;
  5. }

  1.1.10

  数组没有初始化

  1.1.11

  1. public class TestApp1 {
  2. public static void main(String[] args) {
  3.  
  4. int N = 10;
  5. boolean ma[][] = new boolean[N][N];
  6.  
  7. for(int i = 0; i < N; i++) {
  8. for(int j = 0; j < N; j++) {
  9. double r = Math.random();
  10. if(r < 0.5)
  11. ma[i][j] = true;
  12. else
  13. ma[i][j] = false;
  14. }
  15. }
  16.  
  17. for(int i = 0; i < N; i++) {
  18. for(int j = 0; j < N; j++) {
  19. if(ma[i][j] == true)
  20. System.out.print("*");
  21. else
  22. System.out.print(" ");
  23. }
  24. System.out.println();
  25. }
  26. }
  27. }

  1.1.12

  0 1 2 3 4 4 3 2 1 0

  1.1.13 

  1. public class TestApp1 {
  2. public static void main(String[] args) {
  3.  
  4. int N = 10;
  5. int M = 15;
  6. boolean ma[][] = new boolean[N][M];
  7.  
  8. for(int i = 0; i < N; i++) {
  9. for(int j = 0; j < M; j++) {
  10. double r = Math.random();
  11. if(r < 0.5)
  12. ma[i][j] = true;
  13. else
  14. ma[i][j] = false;
  15. }
  16. }
  17.  
  18. System.out.println("origin matrix:");
  19. for(int i = 0; i < N; i++) {
  20. for(int j = 0; j < M; j++) {
  21. if(ma[i][j] == true)
  22. System.out.print("*");
  23. else
  24. System.out.print(" ");
  25. }
  26. System.out.println();
  27. }
  28.  
  29. System.out.println("transposition matrix:");
  30. for(int i = 0; i < M; i++) {
  31. for(int j = 0; j < N; j++) {
  32. if(ma[j][i] == true)
  33. System.out.print("*");
  34. else
  35. System.out.print(" ");
  36. }
  37. System.out.println();
  38. }
  39. }
  40. }

  1.1.14

  1. public class TestApp1 {
  2. public static void main(String[] args) {
  3.  
  4. System.out.println(lg(15));
  5. System.out.println(lg(16));
  6. System.out.println(lg(17));
  7. }
  8.  
  9. private static int lg(int N) {
  10.  
  11. int result = 1;
  12. while(result <= N) {
  13. result *= 2;
  14. }
  15.  
  16. return result / 2;
  17. }
  18. }

  1.1.15

  1. public class TestApp1 {
  2. public static void main(String[] args) {
  3. int A[] = {1, 2, 3, 3, 2, 5, 5, 4};
  4. int N = 8;
  5. int M[] = histogram(A, N);
  6. for(int m : M) {
  7. System.out.println(m);
  8. }
  9. }
  10.  
  11. private static int[] histogram(int[] A, int N) {
  12. int[] M = new int[N];
  13. for(int i = 0; i < N; i++) {
  14. M[A[i]]++;
  15. }
  16. return M;
  17. }
  18. }

  1.1.16

  311461142246

  1.1.17

  由于一直会递归调用,所以不会停止,直到栈溢出。

  1.1.18

  修改前:

  mystery(2, 25) = 50

  mystery(3, 11) = 33

  修改后:

  mystery(2, 25) = 33554432

  mystery(3, 11) = 177147

  1.1.19

  1. public class TestApp1 {
  2.  
  3. private static long[] list = new long[1000];
  4.  
  5. public static void main(String[] args) {
  6. F(1000);
  7. for(int i = 0; i < list.length; i++) {
  8. if(i > 0 && list[i] == 0) {
  9. break;
  10. }
  11. StdOut.println(i + " " + list[i]);
  12. }
  13. }
  14.  
  15. public static void F(int N) {
  16. list[0] = 0;
  17. list[1] = 1;
  18. for(int i = 2; i < N; i++) {
  19. list[i] = list[i - 1] + list[i - 2];
  20. if(list[i] < list[i - 1]) {
  21. list[i] = 0;
  22. System.out.println("MAX : " + list[i - 1]);
  23. break;
  24. }
  25. }
  26. }
  27. }

  最大值为 7540113804746346429

  1.1.20

  1. public class TestApp1 {
  2.  
  3. public static void main(String[] args) {
  4. for(int i = 1; i <= 10; i++) {
  5. System.out.println("log_fact(" + i + "): " + log_fact(i));
  6. }
  7. }
  8.  
  9. private static double log_fact(int n) {
  10. if(n == 1)
  11. return 0;
  12. else
  13. return log_fact(n - 1) + Math.log(n);
  14. }
  15. }

  1.1.21

  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class TestApp1 {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. List<Info> list = new ArrayList<Info>();
  9. String line;
  10.  
  11. while(StdIn.hasNextLine()) {
  12. line = StdIn.readLine();
  13. if(line.isEmpty()) {
  14. break;
  15. }
  16. String[] content = line.split(" ");
  17. Info info = new Info(content[0], Integer.parseInt(content[1]), Integer.parseInt(content[2]));
  18. list.add(info);
  19. }
  20.  
  21. for(Info info : list) {
  22. StdOut.println(info);
  23. }
  24. }
  25. }
  26.  
  27. class Info {
  28. private String name;
  29. private int x;
  30. private int y;
  31.  
  32. public Info(String name, int x, int y) {
  33. this.name = name;
  34. this.x = x;
  35. this.y = y;
  36. }
  37.  
  38. public String toString(){
  39. return "1: " + name + "2: " + x + "3: " + y + "4: " + x * 1.0 / y;
  40. }
  41. }

  1.1.22

  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class TestApp1 {
  5.  
  6. public static void main(String[] args) {
  7. rank(3, new int[]{1, 3, 5, 6, 8, 11, 25, 78, 345, 7653});
  8. }
  9.  
  10. private static int rank(int key, int[] a) {
  11. return rank(key, a, 0, a.length - 1, 0);
  12. }
  13.  
  14. private static int rank(int key, int[] a, int lo, int hi, int depth) {
  15. if(lo > hi)
  16. return -1;
  17. int mid = (lo + hi) / 2;
  18. int loop = depth;
  19. while(loop != 0) {
  20. System.out.print(" ");
  21. loop--;
  22. }
  23. System.out.println("lo:" + lo + " hi:" + hi);
  24. if(key < a[mid])
  25. return rank(key, a, lo, mid - 1, ++depth);
  26. else if(key > a[mid])
  27. return rank(key, a, mid + 1, hi, ++depth);
  28. else
  29. return mid;
  30. }
  31. }

  1.1.23

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4. public class BinarySearch {
  5.  
  6. private BinarySearch(){}
  7.  
  8. public static int rank(int key, int[] a) {
  9. int lo = ;
  10. int hi = a.length - ;
  11. while(lo <= hi) {
  12. int mid = (lo + hi) / ;
  13. if(key < a[mid])
  14. hi = mid - ;
  15. if(key > a[mid])
  16. lo = mid + ;
  17. else
  18. return mid;
  19. }
  20. return -;
  21. }
  22.  
  23. public static void main(String[] args) {
  24. In in = new In(args[]);
  25. int[] whitelist = in.readAllInts();
  26. Arrays.sort(whitelist);
  27. String flag = args[];
  28.  
  29. if("+".equals(flag)) {
  30. while(!StdIn.isEmpty()) {
  31. int key = StdIn.readInt();
  32. if(rank(key, whitelist) == -) {
  33. StdOut.println(key);
  34. }
  35. }
  36. }else if("-".equals(flag)) {
  37. while (!StdIn.isEmpty()) {
  38. int key = StdIn.readInt();
  39. if(rank(key, whitelist) != -) {
  40. StdOut.println(key);
  41. }
  42. }
  43. } else {
  44. StdOut.println("Error Flag");
  45. }
  46.  
  47. }
  48. }

  1.1.24

  1. public class GCD {
  2.  
  3. public static void main(String[] args) {
  4. StdOut.println(gcd(105, 24));
  5. }
  6.  
  7. public static int gcd(int a, int b) {
  8. StdOut.println("a: " + a + " b: " + b);
  9. if(b == 0)
  10. return a;
  11. else
  12. return gcd(b, a % b);
  13. }
  14. }

   1.1.25

设两数为a、b(b<a),用gcd(a,b)表示a,b的最大公约数,r=a mod b 为a除以b以后的余数,k为a除以b的商,即a÷b=k.......r。辗转相除法即是要证明gcd(a,b)=gcd(b,r)。
第一步:令c=gcd(a,b),则设a=mc,b=nc
第二步:根据前提可知r =a-kb=mc-knc=(m-kn)c
第三步:根据第二步结果可知c也是r的因数
第四步:可以断定m-kn与n互质【否则,可设m-kn=xd,n=yd,(d>1),则m=kn+xd=kyd+xd=(ky+x)d,则a=mc=(ky+x)dc,b=nc=ycd,故a与b最大公约数成为cd,而非c,与前面结论矛盾】
从而可知gcd(b,r)=c,继而gcd(a,b)=gcd(b,r)。
证毕。

Algorithms 4th - 1.1 Basic Programming Model - EXERCISES的更多相关文章

  1. Algorithms 4th - 1.1 Basic Programming Model - CREATIVE PROBLEMS

    欢迎交流 1.1.26 public class TestApp { public static void main(String[] args) { int a = StdIn.readInt(); ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc

    Posted by Shiv Kumar on 23rd February, 2011 The Asynchronous Programming Model (or APM) has been aro ...

  4. PatentTips - Heterogeneous Parallel Primitives Programming Model

    BACKGROUND 1. Field of the Invention The present invention relates generally to a programming model ...

  5. 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ

    3.1.4 无序链表中的顺序查找 符号表中使用的数据结构的一个简单选择是链表,每个结点存储一个键值对,如以下代码所示.get()的实现即为遍历链表,用equals()方法比较需被查找的键和每个节点中的 ...

  6. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)

    2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...

  7. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ

    · 学后心得体会与部分习题实现 心得体会: 曾经只是了解了优先队列的基本性质,并会调用C++ STL库中的priority_queue以及 java.util.PriorityQueue<E&g ...

  8. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

  9. Udacity并行计算课程笔记-The GPU Programming Model

    一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...

随机推荐

  1. Apache启用GZIP压缩网页传输方法

    一.gzip介绍 Gzip是一种流行的文件压缩算法,如今的应用十分广泛,尤其是在Linux平台.当应用Gzip压缩到一个纯文本文件时,效果是很明显的,大约能够降低70%以上的文件大小.这取决于文件里的 ...

  2. JavaScript中模块“写法”

    在JavaScript模块到底是什么 event = function() { // do more return { bind: function() {}, unbind: function() ...

  3. C#中byte[] 与指针

    本文假定读者熟悉byte[].指针. C#是类型安全的,默认是不允许使用指针,但是针对C\C++或者其他语言的程序员(delphi)转为使用C#的的工作人员,不适用指针觉得很别扭.下面介绍一下基础的指 ...

  4. IBM WebSphere MQ的C#工具类以及源码(net)

    简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...

  5. 通过WebService跨平台上传大文件到服务器

    好长时间没写博客了,对最近工作中遇到的大文件上传和下载的代码贴出来和大家分享一下. 大文件的上传的和下载是C++的平台通过调用WebService实现文件上传和下载到服务器 /// <summa ...

  6. 详解JOIN

    根据连接中使用的操作符不同,连接条件可分为:等连接,不等连接   连接本身分为: 内连接(INNER JOIN) (1)INNER JOIN 方式(INNER可以省略) 取两表的交集. (2)并表查询 ...

  7. JavaScript中的this引用

    在JavaScript的学习当中,this关键字的出现频率可不低,所以想想有必要对this关键字做一个总结.在总结过程中,参考的资料来源于书本及网上. 一.定义 1.this是函数内部的一个特殊对象( ...

  8. Ucenter整合Thinkphp 双向同步登录退出

    1.整合初步工作: 1,安装Ucenter,完成后添加应用,填写要对接的网站地址 2,api , uc_client目录放置对接项目的根目录 3,通信对接,新建Ucenter组,confi文件填写在u ...

  9. JSP(一)

    开宗明义:JSP本质上就是一个Servlet scriplet JSP 变量和函数的声明 局部变量 <% int a = 3;> 全局变量和函数 <%! int a = 3;> ...

  10. 【转】unity3d的常用快捷键

    Unity3D默认的快捷键. shift +方向键             向“向方向键前进” Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl   N New ...