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的更多相关文章

  1. Moderate 加入空格使得可辨别单词数量最多 @CareerCup

    递归题目,注意结合了memo的方法和trie的应用 package Moderate; import java.util.Hashtable; import CtCILibrary.AssortedM ...

  2. 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, ...

  3. 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)

    题面 B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...

  4. Atcoder B - Moderate Differences

    http://agc017.contest.atcoder.jp/tasks/agc017_b B - Moderate Differences Time limit : 2sec / Memory ...

  5. CCI_chapter 19 Moderate

    19 1  Write a function to swap a number in place without temporary variables void swap(int &a, i ...

  6. [图形学] Chp17 OpenGL光照和表面绘制函数

    这章学了基本光照模型,物体的显示受到以下效果影响:全局环境光,点光源(环境光漫反射分量,点光源漫反射分量,点光源镜面反射分量),材质系数(漫反射系数,镜面反射系数),自身发光,雾气效果等.其中点光源有 ...

  7. Atcoder | AT2665 【Moderate Differences】

    又是一道思路特别清奇的题qwq...(瞪了一上午才发现O(1)的结论...差点还想用O(n)解决) 问题可以转化为是否能够由\(f_{1}=a\)通过\(\pm x \in[c,d]\)得到\(f_{ ...

  8. Atcoder #017 agc017 B.Moderate Differences 思维

    LINK 题意:给出最左和最右两个数,要求往中间填n-2个数,使得相邻数间差的绝对值$∈[L,R]$ 思路:其实也是个水题,比赛中大脑宕机似的居然想要模拟构造一个数列,其实我们只要考虑作为结果的数,其 ...

  9. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

随机推荐

  1. 推荐7款新鲜出炉的HTML5/CSS3应用

    1.HTML5/CSS3发光文字可自定义文字色彩效果很赞 要分享的这款HTML5/CSS3文字效果很帅,鼠标滑过文字时,文字会出现发光的特效,并且我们可以自定义文字和颜色. 在线演示 源码下载 2.H ...

  2. zz linux 下查看局域网内所有存活主机和MAC进址

    用namp对局域网扫描一遍,然后查看arp缓存表就可以知道局域内ip-mac的对应了namp比较强大也可以直接扫描mac地址和端口 进行ping扫描,打印出对扫描做出响应的主机: nmap -sP 1 ...

  3. 【Qt】Qt之进程间通信(IPC)【转】

    简述 进程间通信,就是在不同进程之间传播或交换信息.那么不同进程之间存在着什么双方都可以访问的介质呢?进程的用户空间是互相独立的,一般而言是不能互相访问的,唯一的例外是共享内存区.但是,系统空间却是“ ...

  4. 所有外包项目威客网站列表----来自程序员接私活网qxj.me

    猪八戒    http://www.zhubajie.com/  有佣金,建议别去坑死了 csto      http://www.csto.com/ 开源中国众包   https://zb.osch ...

  5. 无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。

    在VS2010 MVC4项目中,安装NuGet程序包Microsoft.AspNet.SignalR时出现以下错误: 原因是安装的版本是Microsoft.AspNet.SignalR 2.0.2,要 ...

  6. vmware虚拟机上网:NAT搭建局域网

    若是你不知道的情况下,可以编辑虚拟机网络配置,然后恢复默认,vmware会自动给你分配好ip,默认使用的是vmware8,下面的是使用默认的配置 看图 注意:子网的ip一定要在如上图所示的范围 适配器 ...

  7. C# 缓存学习总结

    昨天整理了一下缓存的基本用法,和缓存依赖类 CacheDependency类的使用,今天整理一下缓存的数据库依赖类SqlCacheDependency 1.数据库依赖类SqlCacheDependen ...

  8. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports

    http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...

  9. IAR修改工程名称Tab键设置模板建立

    IAR 修改工程名称 很多时候用IAR开发都是基于已有工程模板开发的,但是工程模板的名称经常让人头疼:以下是修改办法: 从一个实例工程复制后缀名为"dep,ewd,ewp,eww" ...

  10. 未能加载文件或程序集“Report.Basic”或它的某一个依赖项。试图加载格式不正确的程序

    出现问题如下: 解决办法: 这是由于没有开启32位程序兼容模式 具体操作如下:找到对应的程序池--------高级设置-------修改“启用32位应用程序”状态修改为true