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( ...
随机推荐
- UML的基本关联
First, a dependency is a semantic relationship between two model elements in which a change to on ...
- 第十一届GPCT杯大学生程序设计大赛完美闭幕
刚刚过去的周六(6月7号)是今年高考的第一天,同一时候也是GPCT杯大学生程序设计大赛颁奖的日子,以下我们用图文再回想一下本次大赛颁奖的过程. 评审过程的一些花絮<感谢各位评审这些天的付出!&g ...
- IDA strings view 中文字符的显示
具体原理不清楚,在网上找了找,记录下. 第一步:将ida.cfg中cpp866 version的AsciiStringChars注释掉,把full version的AsciiStringChars取消 ...
- UVA 806 Spatial Structures
题意: 如果某一大区域所有色块颜色是相同的,那么这一个大区域就算作一块,如果不同,则将其划分成四个小区域,然后重复上述步骤递归进行直到所有区域的颜色相同为止.然后根据上面划分的区域建树,小区域作为大区 ...
- Android 简单的代码混淆
Android的代码混淆是开发者需要了解的相关知识,它能够防止android应用程序的反编译.因为android程序多数是java语言开发的,而java代码很容易被反编译,所以为了使android应用 ...
- OC-类
1.关于头文件 #include <stdio.h> #import <Foundation/Foundation.h> 区别:#import指令导入更快更有效率.#i ...
- C# 5 break continue 球员成绩 彩票 选班长
二.新课: 1.break与continue. 这两个关键字一般放在循环的花括号里面使用. break--结束整个循环. continue--结束本次循环,进入下次循环. break的案例: ...
- python基础之 sys.argv[]用法
sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始. arg[1]表示第一个命令行参数 arg[1][2:] 表示取第一个命令行参数,但是去掉前两 ...
- [string]字符串中几个比较难的算法和容易搞混的题目
一.两个难点算法 1.Manacher算法,线性时间求最长回文子串 2.KMP算法,字符串匹配问题,c语言中的strStr 二.几个题目 1.最长回文子串 方法:暴力,动态规划,中心扩展,manach ...
- Flink Program Guide (9) -- StateBackend : Fault Tolerance(Basic API Concepts -- For Java)
State Backends 本文翻译自文档Streaming Guide / Fault Tolerance / StateBackend ----------------------------- ...