《Cracking the Coding Interview》——第5章:位操作——题目8
2014-03-19 06:33
题目:用一个byte数组来模拟WxH的屏幕,每个二进制位表示一个像素。请设计一个画水平线的函数。
解法:一个点一个点地画就可以了。如果要优化的话,其实可以把中间整字节的部分一口气画了,只用1/8的力气。
代码:
// 5.8 Given a byte array, set a consecutive bit segment to '1'.
#include <cstdio>
#include <vector>
using namespace std; typedef unsigned char byte; inline void setBit(vector<byte> &v, int i, int j, int w)
{
v[i * w / + j / ] |= ( << (j - (j >> << )));
} inline int getBit(vector<byte> &v, int i, int j, int w)
{
return !!(v[i * w / + j / ] & ( << (j - (j >> << ))));
} int main()
{
int n, w, h;
int x1, x2, y;
vector<byte> v;
int i, j; while (scanf("%d%d", &n, &w) == ) {
h = n * / w;
v.resize(n);
for (i = ; i < n; ++i) {
v[i] = ;
}
scanf("%d%d%d", &x1, &x2, &y); // boundary check is omitted.
for (i = x1; i <= x2; ++i) {
setBit(v, y, i, w);
} for (i = ; i < h; ++i) {
for (j = ; j < w; ++j) {
putchar('' + getBit(v, i, j, w));
}
putchar('\n');
}
} return ;
}
《Cracking the Coding Interview》——第5章:位操作——题目8的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目1
2014-03-19 05:45 题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去. 解法:位操作,请看代码. 代码: // 5.1 Insert one number into th ...
- 《Cracking the Coding Interview》——第5章:位操作——题目6
2014-03-19 06:24 题目:将一个整数的奇偶二进制位交换,(0, 1) (2, 3) ... 解法:使用掩码来进行快速交换,定义掩码为'0101...'和‘1010...’. 代码: // ...
随机推荐
- leetcode: 树
1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...
- leetcode: 哈希——two-sum,3sum,4sum
1). two-sum Given an array of integers, find two numbers such that they add up to a specific target ...
- 利用Excel导入数据到SAP C4C
假设要导入一个Account数据到C4C系统. 工作中心Data Workbench,工作中心视图Import,点download metadata: 会下载一个压缩包到本地. 进入文件夹Templa ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- css样式设置高度不定文本垂直居中
使用css实现文本垂直居中,对于支持display: table的浏览器来说,是比较容易实现的,只需要对外层div设置为table,内层div设置为table-cell,并设置文本垂直居中即可.但对于 ...
- 认识http客户端
最简单的http客户端就是我们的浏览器,浏览器地址输入baidu.com,就会返回响应内容,打开network,都是http请求,第一个就是www.baidu.com的请求,旁边第一个General就 ...
- windows/Linux主机重启NetBackup服务和后台进程
关闭并启动所有NetBackup服务和后台进程 要关闭并启动所有NetBackup服务和后台驻留程序,请从命令行输入以下命令: 在Windows上: 要关闭所有NetBackup服务: install ...
- How To Secure Nginx with Let's Encrypt on Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14 ...
- SWFUpload 参数详解
属性 类型 默认值 描述 upload_url String 处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当前代码所在的文档地址 preserv ...
- CPP-基础:类
1,成员访问属性 一,对于类的实现来说: private:类内部(包括类域范围内)可访问. protect:类内部(包括类域范围内)或 派生类类内部(包括类域范围内)可访问. public: 类内部和 ...