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. 笨办法学Python(二十八)

    习题 28: 布尔表达式练习 上一节你学到的逻辑组合的正式名称是“布尔逻辑表达式(boolean logic expression)”.在编程中,布尔逻辑可以说是无处不在.它们是计算机运算的基础和重要 ...

  2. 2017.10.28 QB模拟赛 —— 下午

    题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...

  3. 洛谷 P1266 速度限制

    题目描述 在这个繁忙的社会中,我们往往不再去选择最短的道路,而是选择最快的路线.开车时每条道路的限速成为最关键的问题.不幸的是,有一些限速的标志丢失了,因此你无法得知应该开多快.一种可以辩解的解决方案 ...

  4. 大数据量高并发的数据库优化详解(MSSQL)

    转载自:http://www.jb51.net/article/71041.htm 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能. ...

  5. flex布局-常用布局

    在使用flex布局,老是需要去查资料,很多常用的,知道大概,可还是需要去过一遍,这里记录一下几个常用的flex布局 一个div,内容垂直居中 html <div className='topHe ...

  6. 2018.8.15 AOP面向切面编程简单理解

    在Filter过滤器中 拦截器 表面上看 -拦截器帮我们封装了很多功能 拦截器优秀的设计,可拔插设计 aop思想 在struts2中 归纳总结

  7. removing vmware debugger from visual studio

    removing vmware debugger from visual studio by Ross on 十月 14, 2010 at 5:30 下午 under Visual Studio |  ...

  8. JavaSE 面试题总结

    一. JavaSE 4 1. 面向对象的特征有哪些方面 4 2. String是最基本的数据类型吗? 4 3. super()与this()的区别? 4 4. JAVA的事件委托机制和垃圾回收机制 4 ...

  9. Git工作流指南:功能分支工作流(转)

    一旦你玩转了集中式工作流,在开发过程中可以很简单地加上功能分支,用来鼓励开发者之间协作和简化交流. 功能分支工作流背后的核心思路是所有的功能开发应该在一个专门的分支,而不是在master分支上.这个隔 ...

  10. 写给iOS小白的MVVM教程(一): 从MVC到MVVM之一个典型的MVC应用场景

    前言 本着实践为主的原则,此系列文章不做过多的概念性的阐述和讨论;更多的代码和篇幅用来展示MVC和MVVC下的基础代码结构与具体实现,来展示各自优劣.这篇文章,更多的在于发掘MVC与MVVC的共性,以 ...