《Cracking the Coding Interview》——第8章:面向对象设计——题目1
2014-04-23 17:32
题目:请设计一个数据结构来模拟一副牌,你要如何用这副牌玩21点呢?
解法:说实话,扑克牌的花样在于各种花色、顺子、连对、三带一、炸弹等等,如果能设计一个数据结构,让判断这些特征的代码变得很好写,那就能满足题意要求了。我只是勉强实现了几个基本功能,包括抽牌、洗牌、切牌,用的是单向链表。至于要拿这个打斗地主、黑桃五之类的还是算了吧。
代码:
// 8.1 Implement a class to simulate a deck of cards
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std; struct Poker {
int index;
Poker(int _index = ): index(_index) {}; friend ostream& operator << (ostream &, const Poker &);
}; ostream& operator << (ostream &cout, const Poker &p)
{
cout << '[';
if (p.index < || p.index > ) {
cout << "ERROR";
} else if (p.index == ) {
cout << "BLACK JOKER";
} else if (p.index == ) {
cout << "RED JOKER";
} else {
switch(p.index / ) {
case :
cout << "SPADE ";
break;
case :
cout << "HEART ";
break;
case :
cout << "CLUB ";
break;
case :
cout << "DIAMOND ";
break;
}
switch(p.index % ) {
case :
cout << 'A';
break;
case :
case :
case :
case :
case :
case :
case :
case :
cout << char('' + p.index % );
break;
case :
cout << "";
break;
case :
cout << 'J';
break;
case :
cout << 'Q';
break;
case :
cout << 'K';
break;
}
}
cout << ']'; return cout;
} struct PokerListNode {
Poker p;
PokerListNode *next;
PokerListNode(int _index): p(_index), next(nullptr) {};
}; class DeckOfPoker {
public:
DeckOfPoker() {
int i; head = tail = nullptr;
for (i = ; i < ; ++i) {
if (head == nullptr) {
head = tail = new PokerListNode(i);
} else {
tail->next = new PokerListNode(i);
tail = tail->next;
}
dict.insert(i);
}
} friend ostream& operator << (ostream &, const DeckOfPoker &); Poker peekCard() {
if (head == nullptr) {
return Poker(-);
} else {
return head->p;
}
} Poker getCard() {
if (head == nullptr) {
return Poker(-);
} else {
Poker p = head->p;
PokerListNode *ptr = head;
head = head->next;
delete ptr;
if (head == nullptr) {
tail = nullptr;
}
dict.erase(p.index); return p;
}
} void insertCard(int index) {
if (index < || index > ) {
return;
}
if (dict.find(index) != dict.end()) {
return;
} PokerListNode *ptr = new PokerListNode(index);
if (head == nullptr) {
head = tail = ptr;
} else {
ptr->next = head;
head = ptr;
}
dict.insert(index);
} bool empty() {
return head == nullptr;
} void cutCards() {
if (head == tail) {
return;
} PokerListNode *p1, *p2;
p1 = p2 = head;
while (p2->next != nullptr && p2->next->next != nullptr) {
p1 = p1->next;
p2 = p2->next->next;
}
PokerListNode *head2 = p1->next;
p1->next = nullptr; PokerListNode *new_head, *new_tail; new_head = new_tail = nullptr;
p1 = head;
p2 = head2;
while (p1 != nullptr && p2 != nullptr) {
if (new_tail == nullptr) {
new_head = new_tail = p1;
} else {
new_tail->next = p1;
new_tail = new_tail->next;
}
p1 = p1->next;
new_tail->next = nullptr; new_tail->next = p2;
new_tail = new_tail->next;
p2 = p2->next;
new_tail->next = nullptr;
}
while (p1 != nullptr) {
new_tail->next = p1;
new_tail = new_tail->next;
p1 = p1->next;
new_tail->next = nullptr;
}
while (p2 != nullptr) {
new_tail->next = p2;
new_tail = new_tail->next;
p2 = p2->next;
new_tail->next = nullptr;
} head = new_head;
tail = new_tail;
} void shuffleCards() {
if (head == tail) {
// no card or one card only
return;
} PokerListNode *p1, *p2; p1 = p2 = head;
while (p2->next != nullptr && p2->next->next != nullptr) {
p1 = p1->next;
p2 = p2->next->next;
}
tail->next = head;
head = p1->next;
p1->next = nullptr;
tail = p1;
} ~DeckOfPoker() {
PokerListNode *ptr; while (head != nullptr) {
ptr = head;
head = head->next;
delete ptr;
}
tail = head;
dict.clear();
}
private:
PokerListNode *head;
PokerListNode *tail;
unordered_set<int> dict;
}; ostream& operator << (ostream& cout, const DeckOfPoker &deck)
{
cout << '{' << endl;
if (deck.head == nullptr) {
cout << "EMPTY" << endl;
} else {
PokerListNode *ptr = deck.head; while (ptr != nullptr) {
cout << ptr->p << endl;
ptr = ptr->next;
}
}
cout << '}' << endl; return cout;
} int main()
{
DeckOfPoker *deck = new DeckOfPoker();
string s;
int index; while (cin >> s) {
if (s == "insert") {
cin >> index;
deck->insertCard(index);
} else if (s == "peek") {
cout << deck->peekCard() << endl;
} else if (s == "get") {
cout << deck->getCard() << endl;
} else if (s == "shuffle") {
deck->shuffleCards();
} else if (s == "cut"){
deck->cutCards();
} else if (s == "print"){
cout << *deck << endl;
}
}
delete deck; return ;
}
《Cracking the Coding Interview》——第8章:面向对象设计——题目1的更多相关文章
- 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 ...
随机推荐
- IOS 自定义代理delegate方法
创建一个自定义代理 @class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJT ...
- LCA最近公共祖先(POJ1330)
题目链接:http://poj.org/problem?id=1330 解题报告: 先将一个子节点,深搜每一个根节点,并标记. 然后深索另一个子节点,当发现访问过了,就找到了最近的公共祖先. #inc ...
- Buffer的使用
虽然知道了怎么实例化Buffer,但这还远远不够,因为Buffer类使随nodejs一起发布的核心库,Buffer不仅能处理tcp连接中发送接收的数据,也能处理图像或者是压缩文件,甚至说文件系统里面的 ...
- Netbackup:nbu常见错误及故障解决
Veritas Netbackup 提供了强大的故障响应功能, 能够有效及时的处理 各种备份故障.主要有备份状态码(status) .错误信息.报告信息及调试日志.下面我们主要针对备份状态码讲解下各种 ...
- log4net为什么会打印两次?
用“log4net 使用”做关键字在bing上搜索,点开排序第一的链接:http://33liuhongwei33.blog.163.com/blog/static/39923778201156101 ...
- Spring常用配置 Scope
Bean的Scope Scope描述的是Spring容器如何新建Bean的实例的.Spring的Scope有以下几种,通过@Scope注解来实现. 1.Singleton:一个Spring容器中 ...
- Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple ...
- nginx架构分析之 模块化
Nginx涉及到的模块分为核心模块.标准HTTP模块.可选HTTP模块.邮件服务模块以及第三方模块等五大类. 核心模块 核心模块是指Nginx服务器正常运行时必不可少的模块,它们提供了Nginx最基本 ...
- Git版本控制的原理
Git工作区域 工作目录(Working Directory) 暂存区(Stage/Index) 资源库(Repository或Git Directory) 远程的git仓库(Remote Direc ...
- Yarn下分片和分块源代码分析
public class FileSplit extends InputSplit implements Writable { private Path file; private long star ...