Chp17: Moderate
17.1 swap a number in place.(without temporary variables)
a = a ^ b;
b = a ^ b;
a = a ^ b;
17.3 Write a function which computes the number of trailing zeros in n factorial.
To count the number of zeros, we only need to count the pairs of multiples of 5 and 2. There will always be more multiples of 2 than 5 though, so, simply counting the number of multiples of 5 is sufficient.
public int count(int num){
int count = 0;
if(num < 0) return -1;
for(int i = 5; num /i > 0; i *= 5)
count += num / i;
return count;
}
17.4 Write a method which finds the maximum of two numbers. You should not use if-else or any other comparison operator.
public int flip(int bit){
return 1 ^ bit;
}
public int sign(int a){
return flip((a >> 31) & 0x1);
}
public int getMax(int a, int b){
int c = a - b;
int sa = sign(a); //if a >= 0 : 1 other : 0
int sb = sign(b); //if b >= 1 : 1 other : 0
int sc = sign(c); //depends on whether a - b overflows, like a = INT_MAX, b < 0
int use_sign_a = sa ^ sb;
int use_sign_c = flip(sa ^ sb);
int k = use_sign_a * sa + use_sign_c * sc;
int q = flip(k);
return a * k + b * q;
}
17.9 Design a method to find the frequency of occurrences of any given word in a book.
The first question that you should ask is if you will be doing this operation once or repeatedly.
Solution: Single Query
go through the book, word by word, count the number of times that words appears. O(n)
Solution: Repetitive Queries
do pre-processing on the book! create a hash table which maps from a word to its frequency.
Hashtable<String, Integer> setupDic(String[] book){
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
for(String word : book){
word = word.toLowerCase();
if(word.trim() != ""){
if(!table.containsKey(word)) table.put(word, 0);
table.put(word, table.get(word) + 1);
}
}
return table;
}
int getFrequency(Hashtable<String, Integer> table, String word){
if(table == null || word == null) return -1;
word = word.toLowerCase();
if(table.containsKey(word)) return table.get(word);
return 0;
}
17.11 Implement a method rand7() given rand5(). Given a method that generates a random number between 0 and 4, write a method that generates a random number between 0 and 6.
Nondeterministic Number of Calls
public int rand7(){
while(true){
int num = 5 * rand5() + rand5();
if(num < 21) return num % 7;
}
}
Chp17: Moderate的更多相关文章
- Moderate 加入空格使得可辨别单词数量最多 @CareerCup
递归题目,注意结合了memo的方法和trie的应用 package Moderate; import java.util.Hashtable; import CtCILibrary.AssortedM ...
- found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, or `npm audit` for details
npm 安装包之后,如果出现类似下面的信息 found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, ...
- 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)
题面 B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...
- Atcoder B - Moderate Differences
http://agc017.contest.atcoder.jp/tasks/agc017_b B - Moderate Differences Time limit : 2sec / Memory ...
- CCI_chapter 19 Moderate
19 1 Write a function to swap a number in place without temporary variables void swap(int &a, i ...
- [图形学] Chp17 OpenGL光照和表面绘制函数
这章学了基本光照模型,物体的显示受到以下效果影响:全局环境光,点光源(环境光漫反射分量,点光源漫反射分量,点光源镜面反射分量),材质系数(漫反射系数,镜面反射系数),自身发光,雾气效果等.其中点光源有 ...
- Atcoder | AT2665 【Moderate Differences】
又是一道思路特别清奇的题qwq...(瞪了一上午才发现O(1)的结论...差点还想用O(n)解决) 问题可以转化为是否能够由\(f_{1}=a\)通过\(\pm x \in[c,d]\)得到\(f_{ ...
- Atcoder #017 agc017 B.Moderate Differences 思维
LINK 题意:给出最左和最右两个数,要求往中间填n-2个数,使得相邻数间差的绝对值$∈[L,R]$ 思路:其实也是个水题,比赛中大脑宕机似的居然想要模拟构造一个数列,其实我们只要考虑作为结果的数,其 ...
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
随机推荐
- 8款超酷的HTML5 3D图片动画源码
1.HTML5移动端图片左右切换动画 今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外观上,这款 ...
- struts2指定集合元素的泛型
public class LoginAction implements Action{ private List users; public void setUsers(List users){ th ...
- ZigBee profile
每个ZigBee设备都与一个特定模板相关联,可能是公共模板或私有模板.这些模板定义了设备的应用环境.设备类型以及用于设备间通信的簇.采用公共模板,可以确保不同供应商的设备在相同应用领域的互操作 ...
- 为什么ARM的frq中断的处理速度比较快
FRQ向量位于异常向量表的最末端,不需要跳转就可以直接执行后面跟随的异常处理程序:FRQ模式中私有寄存器数量最多,在进行异常处理时不需要对这些寄存器进行压栈保存.
- VmodCAM图像采集 VGA显示
先上图 总体框图 效果图 效果不是很好,因为暂时用的是zedboard自带的VGA,其只能RGB只有3*3*3的彩色度 VmodCAM原理图 VmodCAM的zedboard管脚约束见:http:// ...
- 基于BT协议的文件分发系统
基于BT协议的文件分发系统构成: 1.一个Web服务器:保存着种子文件 2.一个种子文件:保存共享文件的一些信息(文件名,文件大小 ,Tracker服务器地址,torrent为后缀) ...
- jquery.validate.js
jquery.validate.js使用记录 首先到JQ官网下载搜索validate插件,下载相关源码,附上validate官网网址 https://jqueryvalidation.org/ 找到v ...
- 如何将Log4Net 日志保存到mongodb数据库之实践
log4net的大名早有耳闻,一直没真正用过,这次开发APP项目准备在服务端使用log4net. 日志的数据量较大,频繁的写数据库容易影响系统整体性能,所以独立将日志写到mongodb数据库是不错的选 ...
- Mongodb地理空间索引
1.索引: 建立索引既耗时也费力,还需要消耗很多资源.使用{"bakckground":true}选项可以使这个过程在后台完成,同时正常处理请求.如果不包括background 这 ...
- 解析php开发中的中文编码问题
其实php开发中的中文编码并没有想像的那么复杂,虽然定位和解决问题没有定规,各种运行环境也各不尽然,但后面的原理是一样的. 了解字符集的知识是解决字符问题的基础. PHP程序设计中中文编码问题曾经困扰 ...