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();
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的更多相关文章
- 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 ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc
Posted by Shiv Kumar on 23rd February, 2011 The Asynchronous Programming Model (or APM) has been aro ...
- PatentTips - Heterogeneous Parallel Primitives Programming Model
BACKGROUND 1. Field of the Invention The present invention relates generally to a programming model ...
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ
3.1.4 无序链表中的顺序查找 符号表中使用的数据结构的一个简单选择是链表,每个结点存储一个键值对,如以下代码所示.get()的实现即为遍历链表,用equals()方法比较需被查找的键和每个节点中的 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)
2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ
· 学后心得体会与部分习题实现 心得体会: 曾经只是了解了优先队列的基本性质,并会调用C++ STL库中的priority_queue以及 java.util.PriorityQueue<E&g ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
- Udacity并行计算课程笔记-The GPU Programming Model
一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...
随机推荐
- Android 动画之ScaleAnimation应用具体解释
android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 TranslateAnimation 位移动画效果 RotateAnimat ...
- Linux下卸载ORACLE的多种方法(转)
第一种# cd /u01/app/oracle/product/11.2.0/client_1/deinstall/ # ./deinstall# rm -rf /u01/app/oracle# rm ...
- UVA1600 Patrol Robot
题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...
- tomcat 发布webService
<!-- tomcat发布webservice时所需jar --> <dependency> <groupId>com.sun.xml.ws</groupId ...
- 06JS高级创建对象使用原型共享对象方法
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 学习Oracle一个星期以来的总结
公司开发部门主要分2部分:.net开发和Oracle PL\SQL开发.刚入职的我被分到Oracle PL\SQL组了.Oracle是比SQL Server更大的数据库应用,我在学校只接触过SQL S ...
- HDU 1829 - A Bug's Life
Problem Description Background Professor Hopper is researching the sexual behavior of a rare species ...
- jquery easyui filebox 上传附件 + asp.net后台
form必须加这个属性enctype="multipart/form-data",否则后台获取不到文件 <script> function uploadFiles() ...
- Android_使用getIdentifier()获取资源Id
Android 获取资源ID的另外一种方法,常规获取ID是在特定的文件夹下面的资源,如果在比较特殊的文件夹下面,就需要其他方法获取ID 了: 使用getIdentifier()方法可以方便的获各应用包 ...
- SQL Server 查看指定表上的索引
解决方案: sys.indexs; ---------------------------------------------------------------------------------- ...