《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 ...
随机推荐
- 笨办法学Python(十一)
习题 11: 提问 我已经出过很多打印相关的练习,让你习惯写简单的东西,但简单的东西都有点无聊,现在该跟上脚步了.我们现在要做的是把数据读到你的程序里边去.这可能对你有点难度,你可能一下子不明白,不过 ...
- 使用Atom编写Makedown
Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器. Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 作为一个Gi ...
- Poj(1521),哈夫曼编码
题目链接:http://poj.org/problem?id=1521 这里,网上有很多博客都有写,很多人没有建树,直接就是求一下这个哈夫曼编码的长度,的确很巧妙,我也用的这个方法,但是,几乎所有博客 ...
- Android开发之动态创建多个按钮
//获取屏幕大小,以合理设定 按钮 大小及位置 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDispl ...
- js实现弹窗一个ip在24小时只弹出一次的代码
function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...
- DESCryptoServiceProvider 类加密解密
DESCryptoServiceProvider 点击查看介绍 加密解密辅助类:点击查看 私钥加密 定义:定义一个包装对象来访问加密服务提供程序 (CSP) 版本的数据加密标准 (DES) 算法. ...
- Ubuntu 16.04安装docker(2018年最新)
参考https://blog.csdn.net/bingzhongdehuoyan/article/details/79411479 http://www.cnblogs.com/lighten/p/ ...
- 记一次EBS正式环境补丁安装的过程
因菏泽能源上线需求,需要在8009上修复集团8000环境上已经修复的所有补丁程序,修复前做应用及数据库层备份,完成修复后解决并发管理器无法启动的问题.此为概述. 应用层备份 应用层的备份采用直接压缩备 ...
- 在centos7云服务器上搭建Apache服务器并访问到你的网站
使用X-shell ssh安全连接到云服务器 https://mail.qq.com/cgi-bin/mail_spam?action=check_link&url=https://www.n ...
- Go HTTP模块处理流程简析
Go语言提供完善的net/http包,用户使用起来非常方便简单,只需几行代码就可以搭建一个简易的Web服务,可以对Web路由.静态文件.cookie等数据进行操作. 一个使用http包建立的Web服务 ...