欢迎交流

  1.1.26

public class TestApp {
public static void main(String[] args) { int a = StdIn.readInt();
int b = StdIn.readInt();
int c = StdIn.readInt();
int t; if( a > b) {
t = a;
a = b;
b = t;
} if( a > c) {
t = a;
a = c;
c = t;
} if( b > c) {
t = b;
b = c;
c = t;
} StdOut.println(a + "->" + b + "->" + c);
}
}

   1.1.27

public class Binomial {
/**
* 递归方式的二项分布
* @param N 总次数
* @param k 出现次数
* @param p 每次出现概率
* @return
*/
public static double binomial1(int N, int k, double p) {
if(N == 0 && k == 0)
return 1.0;
if(N < 0 || k < 0)
return 0.0;
return (1.0 - p) * binomial1(N - 1, k, p) + p * binomial1(N - 1, k - 1, p);
} public static double binomial2(int N, int k, double p) {
double[][] b = new double[N + 1][N + 1];
// base
for(int i = 0; i <= N; i++) {
b[i][0] = Math.pow(1.0 - p, i);
}
b[0][0] = 1.0;
// recursive
for(int i = 1; i <= N; i++) {
for(int j = 1; j <=k; j++) {
b[i][j] = p * b[i - 1][j - 1] + (1.0 - p) * b[i - 1][j];
}
}
return b[N][k];
} public static void main(String[] args) {
int N = 100;
int k = 50;
double p = 0.76;
StdOut.println(binomial1(N, k, p));
StdOut.println(binomial2(N, k, p));
}
}

  1.1.28

  想不出与BinarySearch有关的去重方法。

  1.1.29

// returns the number of elements that are smaller than the key
public static int rank(int key, int[] a) {
int lo = 0;
int hi = a.length - 1;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(key < a[mid])
hi = mid - 1;
else if(key > a[mid])
lo = mid + 1;
else {
while(a[mid - 1] == key) {
mid -= 1;
}
return mid;
}
}
return -1;
} // returns the number of elements equal to the key
public static int count(int key, int[] a) {
int lo = 0;
int hi = a.length - 1;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(key < a[mid])
hi = mid - 1;
else if(key > a[mid])
lo = mid + 1;
else {
lo = mid;
hi = mid;
while(a[lo - 1] == key) {
lo -= 1;
}
while(a[hi + 1] == key) {
hi += 1;
}
return hi - lo + 1;
}
}
return 0;
}

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

  1. Algorithms 4th - 1.1 Basic Programming Model - EXERCISES

    欢迎交流 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 public class MainA ...

  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. sql linq lambda 对比

    . 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student Linq: from s in Students ...

  2. 初学DIV+CSS要记住的

    初学DIV+CSS?有六个问题需要您关注一下!作为DIV+CSS初学者,如果在动手写代码之前对网页整体结构由一个清晰认识的话,写起来会事半功倍!但是,写的过程中总是有这样那样的问题,使得我们不得不停下 ...

  3. Visual C++ 64bit应用程序项目设置

    Visual Studio 2005 This topic describes how to set up C++ applications to target 64-bit platforms us ...

  4. DBubtil的使用

    1.什么是O-R Mapping(对象-关系映射) 常用O-R Mapping映射工具 Hibernate(全自动框架) Ibatis(半自动框架/SQL) Commons DbUti ls(只是对J ...

  5. cURL模拟POST方法提交XML数据并解析

    php编程中会用到xml格式传送数据,这里演示下php以post形式发送xml,服务器接收,并解析xml的过程! post_xml.php源码: <?php header("Conte ...

  6. PHPMailer发送邮件方法

    /** * * 测试邮件发送s * @param 服务器 $Host * @param 端口 $Port * @param 昵称 $Fromname * @param 身份验证用户名 $Usernam ...

  7. 初学swift笔记字典、数组(四)

    import Foundation //字典 元素顺序是无序的 //1.字典元素是键值对 (key:value) //key 一定是可哈希的 string\int\bool var dic1=[&qu ...

  8. 不调用库函数实现 strCpy

    实现函数时,首先要弄清楚特殊情况,边界条件要搞清. char* strCpy(char* src,char* des){ //判断指针是否为空 if(NULL==src||NULL == des) r ...

  9. 翻书的效果:FMX.TSwipeTransitionEffect Animation

    This example shows how to use a TSwipeTransitionEffect transition and a TPathAnimation to simulate t ...

  10. docker 私有仓库上传镜像,其他docker服务器从私有镜像下载

    <pre name="code" class="cpp">docker:/data# docker ps CONTAINER ID IMAGE CO ...