《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 ...
随机推荐
- myeclipse 10 创建webservice
java 快捷创建webservice 收集一下,方便一下查阅 详情去看一下这个老哥,里面写得非常详细: http://hyan.iteye.com/ -- http://www.cnblogs.co ...
- EF写INNER JOIN 链接
面对多表的查询,一般都是多表连接后下面再写条件,但是有一种写法可以提升一下EF生成的语句的效率 首先先去查询每一个表,把每一个表对应的条件附加上去,注意:过滤数据最多的条件放在首先位置 var lt ...
- 在vue中使用插槽 slot
插槽(slot)这个概念非常重要 插槽的使用场景1:在子组件里面显示父组件的dom <div id='root'> <child content = '<p>Dell&l ...
- vuejs使用组件的细节点
is属性 <div id='root'> <table> <tbody> <row></row> <row></row&g ...
- 2017.9.16 Web 应用开发环境搭建与开发工具安装
1.JDK的下载与安装 1.1 在网址:http://javase/downloads/index.jsp网站下载最新的JDK版本 1.2 安装jdk,双击下载好的.exe文件运行,一般默认安装在c盘 ...
- AI-Info-Micron-Insight:V2X 自主性:帮助减少事故、排放和交通拥堵
ylbtech-AI-Info-Micron-Insight:V2X 自主性:帮助减少事故.排放和交通拥堵 1.返回顶部 1. V2X 自主性:帮助减少事故.排放和交通拥堵 一辆汽车冲到你的车道上.晚 ...
- idea中不重启服务器更改代码(使用jrebel)
http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b107ce 第一步 第二步:下载jrebel 第三步(这里有些有有些没有) 下载完后 ...
- 理解 JavaScript 作用域(转)
简介 JavaScript 有个特性称为作用域.尽管对于很多开发新手来说,作用域的概念不容易理解,我会尽可能地从最简单的角度向你解释它们.理解作用域能让你编写更优雅.错误更少的代码,并能帮助你实现强大 ...
- iOS MapKit地图
地图框架:#import <MapKit/MapKit.h> 基本属性和方法: 属性: 地图类视图:MKMapView 地图类型:MKMapType mapType 地图旋转:rotate ...
- 牛客小白月赛2 H 武 【Dijkstra】
链接:https://www.nowcoder.com/acm/contest/86/H来源:牛客网 题目描述 其次,Sεlιнα(Selina) 要进行体力比武竞赛. 在 Sεlιнα 所在的城市, ...