《Cracking the Coding Interview》——第8章:面向对象设计——题目4
2014-04-23 18:17
题目:设计一个停车位的类。
解法:停车位,就要有停车、取车的功能了。另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用。
代码:
// 8.4 Design a class to simulate the parking lot.
#include <iostream>
#include <string>
#include <thread>
#include <vector>
using namespace std; class ParkingLot {
public:
ParkingLot(int _capacity = ):capacity(_capacity) {
slots.resize(capacity);
fees.resize(capacity);
fill(slots.begin(), slots.end(), false);
fill(fees.begin(), fees.end(), );
}; void start() {
work = new thread(workThread, this);
}; void parkIn() {
int i; for (i = ; i < (int)slots.size(); ++i) {
if (!slots[i]) {
cout << "Car is parked at slot " << i << "." << endl;
slots[i] = true;
fees[i] = ;
return;
}
} cout << "Sorry, no more slot is available." << endl;
}; void getOut(int id) {
if (id < || id > (int)slots.size() - ) {
cout << "Invalid slot number." << endl;
return;
} else if (slots[id] == false) {
cout << "The slot is empty." << endl;
return;
} cout << "Car in slot " << id << " is delivered. Total fee is " << fees[id] << " bucks." << endl;
slots[id] = false;
fees[id] = ;
}; friend void workThread(ParkingLot *); ~ParkingLot() {
slots.clear();
fees.clear();
work->detach();
delete work;
};
private:
thread *work;
int capacity;
vector<bool> slots;
vector<int> fees;
}; void workThread(ParkingLot *p)
{
while (true) {
// sleep for one second.
_sleep();
for (int i = ; i < p->capacity; ++i) {
if (p->slots[i]) {
++p->fees[i];
}
}
}
} int main()
{
ParkingLot *p;
string cmd;
int id; p = new ParkingLot();
p->start();
while (cin >> cmd) {
if (cmd == "park") {
p->parkIn();
} else if (cmd == "get") {
cin >> id;
p->getOut(id);
} else if (cmd == "end") {
break;
}
}
delete p; return ;
}
《Cracking the Coding Interview》——第8章:面向对象设计——题目4的更多相关文章
- 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》——第8章:面向对象设计——题目10
2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...
随机推荐
- My First Blog in Cnblogs
终于打算从csdn搬到博客园了 虽然在csdn只写过三篇文章,不过打算写第四篇的时候发现原先的三篇都消失了.联系客服最终还是找回了,不过对于csdn神奇的管理方式还是不放心,也没在csdn上再写过文章 ...
- c++ priority_queue
1.默认为大顶堆 #include <iostream> #include <queue> using namespace std; int main() { priority ...
- MySQL入门很简单: 5 索引
1. 索引的含义和特点 索引:创建在表上,是对数据库表中一列或多列的值进行排序的一种结构. 存储类型: B性树(BTREE)索引和哈希(HASH)索引: InnoDB和MyISAM支持BTREE索引, ...
- Git入门体验
Git这个东西我也是最近才知道的,然后知道后却发现一个事实:自己真的是太LOW啦!竟然连Git都不知道!!!??? Git 在实际协同工作时会为我们提供巨大帮助, 下面简单介绍一下Git的用法: 一. ...
- CodeForces 506D Mr. Kitayuta's Colorful Graph
brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...
- P1540 机器翻译
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- Intel MKL 多线程设置
对于多核程序,多线程对于程序的性能至关重要. 下面,我们将对Intel MKL 有关多线程方面的设置做一些介绍: 我们提到MKL 支持多线程,它包括的两个概念:1>MKL 是线程安全的: MKL ...
- P2447 [SDOI2010]外星千足虫
怎么说呢? 因为是在mod 2 意义下的吗(一般是遇到二就可能是位运行算或二分图) 就可以利用异或计算. 因为奇数和偶数在二进制上就用判断最后一位就可以了 然后因为异或符合交换律和结合律 直接消元就可 ...
- Search in Rotated Sorted Array——LeetCode
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- jquery 添加和删除节点
// 增加一个三和一节点 function addPanel() { // var newPanel = $('.my-panel').clone(true) var newPanel = $(&qu ...