《Cracking the Coding Interview》——第17章:普通题——题目2
2014-04-28 22:05
题目:写个程序判断三连棋哪一方赢了。
解法:三个相同的棋子连成一条横线,竖线或者对角线就判断为赢了。
代码:
// 17.2 Write an algorithm to check if someone has won the tic-tac-toe game.
// Here is the game:
// xo-
// ox-
// --x
// 'x' won the game.
// Rule: anyone who gets three consecutive pieces in a row, a column or a diagonal line first wins.
#include <cstdio>
using namespace std; int check(int a[][])
{
if (a == nullptr) {
return -;
} int i;
for (i = ; i < ; ++i) {
if (a[i][] == a[i][] && a[i][] == a[i][] && a[i][] != ) {
return a[i][];
}
if (a[][i] == a[][i] && a[][i] == a[][i] && a[][i] != ) {
return a[i][];
}
}
if (a[][] == a[][] && a[][] == a[][] && a[][] != ) {
return a[][];
}
if (a[][] == a[][] && a[][] == a[][] && a[][] != ) {
return a[][];
} return ;
} int main()
{
int a[][];
int i, j; for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
scanf("%d", &a[i][j]);
}
}
printf("%d\n", check(a)); return ;
}
《Cracking the Coding Interview》——第17章:普通题——题目2的更多相关文章
- 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》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
随机推荐
- 笨办法学Python(十六)
习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...
- JSON:使用json_encode函数解析结果为Null
1.首先,数据库中的json数据是这样的 2.仓鼠使用json_encode()函数进行解析json数据时,显示了一个NULL: 3.这时候,我们需要使用,表示在解析json之前,该json是有语法错 ...
- SINAMICS S120/S120 EPOS 基本定位修改方向
步骤 修改 P1821 (在线修改需要P10=3) 修改完成,copy ram to rom / load to PG
- selenium 打开浏览器报错java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/String;)
java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/Strin ...
- BZOJ 2824: [AHOI2012]铁盘整理
BZOJ 2824: [AHOI2012]铁盘整理 标签(空格分隔): OI-BZOJ OI-搜索 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...
- CentOS安装配置MongoDB
1.下载安装包: wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.3.tgz 2.解压: tar -zxvf mongodb-l ...
- jquery控制display属性为none或block
代码如下: //隐藏 $("#id").css('display','none'); //显示 $("#id").css('display','block'); ...
- 用keytool制作证书并在tomcat配置https服务(三)
用keytool制作证书并在tomcat配置https服务(一) 用keytool制作证书并在tomcat配置https服务(二) 用keytool制作证书并在tomcat配置https服务(四) 模 ...
- Login用户登录(Python实现)
username_fault = "isuperSun" #程序存储用户名和密码 password_fault = " counts = 0 while counts&l ...
- Python简单线程间通信
本节主要举一个简单的线程间通信的例子,利用线程安全的数据结构queue.Queue保存线程间通信的内容, import queue from threading import Thread from ...