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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第8章:面向对象设计——题目10

    2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...

  8. 《Cracking the Coding Interview》——第8章:面向对象设计——题目9

    2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...

  9. 《Cracking the Coding Interview》——第8章:面向对象设计——题目8

    2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...

  10. 《Cracking the Coding Interview》——第8章:面向对象设计——题目7

    2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...

随机推荐

  1. 一款带有CSS的单选框以及选中事件

    html <div class="radio radio-success"> <input type=" name="radioSingle1 ...

  2. oracle 创建SDO_Geometry表

    Oracle Spatial由一坨的对象数据类型,类型方法,操作子,函数与过程组合而成.一个地理对象作为一个SDO_GEOMETRY对象保存在表的一个字段里.空间索引则由普通的DDL和DML语句来建立 ...

  3. POJ-2352 && hdu-1541 Stars---树状数组的运用

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意 : 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y ...

  4. centos6.5下编译安装FFmpeg

    以下安装步骤基本来自官网,做个笔记以方便自己以后查看 http://trac.ffmpeg.org/wiki/CompilationGuide 1.安装依赖包 <span style=" ...

  5. CentOS6.5手动升级gcc4.8.2

    一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 本文记录了在Cent ...

  6. 复式记账中"借"与"贷"的理解

    财务常识中,复式记账法应用极广,公司采用的是它的借贷记账法.因此,深刻的理解"借"与"贷"的含义极其重要.  一切从历史说起. 起源: Credit  英文含义 ...

  7. 阿里数据库连接池druid

    官方wiki: https://github.com/alibaba/druid/wiki 实用方法介绍的想当详细,包含监控.扩展.大力推荐!

  8. 前端jQuery之文档操作

    1.文档操作内部插入 A.append(B) 吧B添加到A的后面 A.appendTo(B) 吧A添加到B的后面 A.prepend(B) 吧B添加到A的前面 A.prependTo(B) 吧A添加到 ...

  9. socket传送二进制流的一些总结

    第一次实质性的接触socket通信方面的工作,所以遇到的问题还真不少,写篇博客记录一下,提升下记忆. 需求是通过私有协议进行二进制数据的传输,必须保证数据包不能被丢失,所以选择tcp的socket进行 ...

  10. 【PE】手动给PE文件添加一段代码MessageBoxA

    源程序是这个样子: 思路: 1.通过LordPE工具拿到所需数据 2.OllyDebug通过BP MessageBoxA拿到MessageBoxA地址 3.UE十六进制编辑器定位代码节基址 4.在代码 ...