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( ...
随机推荐
- POJ 1734 求最小环路径 拓展Floyd
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11888019 题意: n个点 m条无向边 下面m条有权无向边 问图中最小环的路径 ...
- EK中fromCharCode和parseInt的配合使用
基于web的漏洞攻击的第一步一般是:在landing page中通过<script>标签下的JavaScript脚本引入一些恶意链接.这些脚本往往会採用各种各样的混淆.加密手法来躲避AV和 ...
- HTML5开发 BUG解决
1.点透Q:元素A上定位另外一个元素B,点击元素B,如果元素A有事件或链接,会触发元素A上的事件或链接,即点透A:在元素B的touchend中增加ev.preventDefault();阻止默认事件即 ...
- android开发SD卡工具类(一)
SD卡工具类整理: package com.gzcivil.utils; import java.io.File; import java.io.FileInputStream; import jav ...
- Oracle使用imp导入dmp数据提示:只有DBA才能导入有其他DBA导入的文件
使用imp导入时提示:只有DBA才能导入有其他DBA导入的文件 查看权限,发现admin和default栏没有打钩,打上勾就可以了: 打上勾,保存后,继续导入数据,如下: 成功!
- C# 7 函数 青歌赛打分 天气预报
函数: 数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数 程序里的函数:能完成一个相对独立功的代码块. 数学里的函数:高度抽象. 函数四要素:函数名,输入,输出,加工 ...
- mysql学习(十一)嵌套查询 排序 分组
select * from products where id in(select id from cats where name like '%java%');//查找类型中名字中包含java的的商 ...
- eclipse中使用Lombok
1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar 2.运行Lombok.jar: java -jar D:\00 ...
- View的事件分发机制
一.点击事件的传递规则 传递事件的主要方法: public boolean dispatchTouchEvent(MotionEvent ev) 如果事件能够传递到当前View,则该方法一定会被调用. ...
- android studio 环境配置
遇到哪些坑: Gradle:configure project 卡死在此处 Haxm is not installed hax is not working and emulator runs in ...