《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 ...
随机推荐
- IOS NSThread(线程同步)
@interface HMViewController () /** 剩余票数 */ @property (nonatomic, assign) int leftTicketsCount; @prop ...
- Jenkins添加项目说明,增加项目描述
背景:往往正常Jenkins上呈现的内容,太过简短,不易直观看了解项目是干嘛的,如下面的内容: 解决方案,使用插件,Extra Columns Plugin 安装成功后配置,需要结合自定义视图使用,新 ...
- C. Tanya and Toys_模拟
C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- vuejs生命周期函数
生命周期函数就是vue实例在某一个时间点会自动执行的函数 当我们创建一个实例的时候,也就是我们调用 new Vue() 这句话的时候,vue会帮助我们去创建一个实例,创建过程其实并不像我们想的那么简单 ...
- RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x
PS:如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 FedoraProject 推出的 EPEL(Extra Packages for Ent ...
- EF 集合版 增删查改
- Git笔记(pull/push)
一.从远程服务器上获取分支 git pull <远程主机名> <远程分支>:<本地分支> 例如 git pull origin master:loacal_bran ...
- mask r-cnn
mask R-cnn, kaiming he的新作.可以同时完成object detection和segmentation,还可以做pose estimation,简直就是功能多多啊.在coco上测试 ...
- MVAPICH
from:https://developer.nvidia.com/mvapich MVAPICH2 is an open source implementation of Message Passi ...
- git和svn的混用
服务器上的项目是使用svn进行管理的. 本来本地的项目也是通过svn进行管理的,但是后来使用svn的分支功能进行项目的测试/新功能等等时,总是会出现各种各样的问题,遂转投git. 因为git的分支机制 ...