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 MainApp {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
if(a == b && b == c) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
}
}
1.1.4
a. 去掉"then"
b. a > b 外围加括号
c. 正确
d. else 之前加冒号
1.1.5
public class MainApp {
public static void main(String[] args) {
Double a = Double.parseDouble(args[0]);
Double b = Double.parseDouble(args[1]);
if(a >= 0 && a <= 1 && b >=0 && b <= 1) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
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
int N = ;
String s = "";
for(int i = N; i > ; i /= ) {
s = i % + s;
}
1.1.10
数组没有初始化
1.1.11
public class TestApp1 {
public static void main(String[] args) {
int N = 10;
boolean ma[][] = new boolean[N][N];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
double r = Math.random();
if(r < 0.5)
ma[i][j] = true;
else
ma[i][j] = false;
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(ma[i][j] == true)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
1.1.12
0 1 2 3 4 4 3 2 1 0
1.1.13
public class TestApp1 {
public static void main(String[] args) {
int N = 10;
int M = 15;
boolean ma[][] = new boolean[N][M];
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
double r = Math.random();
if(r < 0.5)
ma[i][j] = true;
else
ma[i][j] = false;
}
}
System.out.println("origin matrix:");
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(ma[i][j] == true)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println("transposition matrix:");
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
if(ma[j][i] == true)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
1.1.14
public class TestApp1 {
public static void main(String[] args) {
System.out.println(lg(15));
System.out.println(lg(16));
System.out.println(lg(17));
}
private static int lg(int N) {
int result = 1;
while(result <= N) {
result *= 2;
}
return result / 2;
}
}
1.1.15
public class TestApp1 {
public static void main(String[] args) {
int A[] = {1, 2, 3, 3, 2, 5, 5, 4};
int N = 8;
int M[] = histogram(A, N);
for(int m : M) {
System.out.println(m);
}
}
private static int[] histogram(int[] A, int N) {
int[] M = new int[N];
for(int i = 0; i < N; i++) {
M[A[i]]++;
}
return M;
}
}
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
public class TestApp1 {
private static long[] list = new long[1000];
public static void main(String[] args) {
F(1000);
for(int i = 0; i < list.length; i++) {
if(i > 0 && list[i] == 0) {
break;
}
StdOut.println(i + " " + list[i]);
}
}
public static void F(int N) {
list[0] = 0;
list[1] = 1;
for(int i = 2; i < N; i++) {
list[i] = list[i - 1] + list[i - 2];
if(list[i] < list[i - 1]) {
list[i] = 0;
System.out.println("MAX : " + list[i - 1]);
break;
}
}
}
}
最大值为 7540113804746346429
1.1.20
public class TestApp1 {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++) {
System.out.println("log_fact(" + i + "): " + log_fact(i));
}
}
private static double log_fact(int n) {
if(n == 1)
return 0;
else
return log_fact(n - 1) + Math.log(n);
}
}
1.1.21
import java.util.ArrayList;
import java.util.List; public class TestApp1 { public static void main(String[] args) { List<Info> list = new ArrayList<Info>();
String line; while(StdIn.hasNextLine()) {
line = StdIn.readLine();
if(line.isEmpty()) {
break;
}
String[] content = line.split(" ");
Info info = new Info(content[0], Integer.parseInt(content[1]), Integer.parseInt(content[2]));
list.add(info);
} for(Info info : list) {
StdOut.println(info);
}
}
} class Info {
private String name;
private int x;
private int y; public Info(String name, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
} public String toString(){
return "1: " + name + "2: " + x + "3: " + y + "4: " + x * 1.0 / y;
}
}
1.1.22
import java.util.ArrayList;
import java.util.List; public class TestApp1 { public static void main(String[] args) {
rank(3, new int[]{1, 3, 5, 6, 8, 11, 25, 78, 345, 7653});
} private static int rank(int key, int[] a) {
return rank(key, a, 0, a.length - 1, 0);
} private static int rank(int key, int[] a, int lo, int hi, int depth) {
if(lo > hi)
return -1;
int mid = (lo + hi) / 2;
int loop = depth;
while(loop != 0) {
System.out.print(" ");
loop--;
}
System.out.println("lo:" + lo + " hi:" + hi);
if(key < a[mid])
return rank(key, a, lo, mid - 1, ++depth);
else if(key > a[mid])
return rank(key, a, mid + 1, hi, ++depth);
else
return mid;
}
}
1.1.23
import java.util.ArrayList;
import java.util.Arrays; public class BinarySearch { private BinarySearch(){} public static int rank(int key, int[] a) {
int lo = ;
int hi = a.length - ;
while(lo <= hi) {
int mid = (lo + hi) / ;
if(key < a[mid])
hi = mid - ;
if(key > a[mid])
lo = mid + ;
else
return mid;
}
return -;
} public static void main(String[] args) {
In in = new In(args[]);
int[] whitelist = in.readAllInts();
Arrays.sort(whitelist);
String flag = args[]; if("+".equals(flag)) {
while(!StdIn.isEmpty()) {
int key = StdIn.readInt();
if(rank(key, whitelist) == -) {
StdOut.println(key);
}
}
}else if("-".equals(flag)) {
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if(rank(key, whitelist) != -) {
StdOut.println(key);
}
}
} else {
StdOut.println("Error Flag");
} }
}
1.1.24
public class GCD {
public static void main(String[] args) {
StdOut.println(gcd(105, 24));
}
public static int gcd(int a, int b) {
StdOut.println("a: " + a + " b: " + b);
if(b == 0)
return a;
else
return gcd(b, a % b);
}
}
1.1.25
Algorithms 4th - 1.1 Basic Programming Model - EXERCISES的更多相关文章
- 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(); ...
- 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( ...
随机推荐
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
- LSH算法原理
原始链接--http://www.jiahenglu.net/NSFC/LSH.html LSH(Location Sensitive Hash),即位置敏感哈希函数.与一般哈希函数不同的是位置敏感性 ...
- 编程算法 - 最长上升子序列问题 代码(C)
最长上升子序列问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有一个长为n的数列a. 请求出这个序列中最长上升子序列的长度. 最长上升子序 ...
- HTML6注册表单输入日志标题
一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...
- js的两种数据类型
js中的数据类型总体来说分为两种,他们分别是: 1 值类型(基本类型):数值型(Number),字符类型(String),布尔值型(Boolean),null 和 underfined 2 引用类型( ...
- bash基础知识
站在用户登录的角度来说,SHELL的类型:登录式shell: 正常通常某终端登录 su - USERNAME su -l USERNAME 非登录式shell: su USERNAME 图形终端下打开 ...
- zoj1074 To the Max
题目很简单,求一个连续的最大子矩阵的值. zoj上的数据非常弱. 首先爆搜是O(N^4),10^8的复杂度略高,那么我们可以处理一下其中一维的前缀和,降一阶,然后按照连续最大子序列来处理,因为可能为负 ...
- BZOJ 3503: [Cqoi2014]和谐矩阵( 高斯消元 )
偶数个相邻, 以n*m个点为变量, 建立异或方程组然后高斯消元... O((n*m)^3)复杂度看起来好像有点大...但是压一下位的话就是O((n*m)^3 / 64), 常数小, 实际也跑得很快. ...
- Android onSaveInstanceState()
我们知道,由于手机的内存问题,很容易造成切换activity之后上一个activity被回收的情况,虽然我们按下back按键的时候,还是能够回到上一个activity,但是此时我们并不是执行的onRe ...
- php 数组去除空值
/** * 方法库-数组去除空值 * @param string $num 数值 * @return string */ public function array_remove_empty(& ...