《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--requests库 安装及简单使用
官方文档:http://www.python-requests.org/en/master/ 1 安装requests库 2 get请求不带参数的 带参数的 3 post请求 更多使用请看官方文档 ...
- 快算24点,POJ(3983)
题目链接:http://poj.org/problem?id=3983 中文题,就不解释题意了. 类似之前的一篇博客,这里也不上解释了,直接上代码吧. #include <iostream> ...
- 1012: A MST Problem
1012: A MST Problem 时间限制: 1 Sec 内存限制: 32 MB提交: 63 解决: 33[提交][状态][讨论版][命题人:外部导入] 题目描述 It is just a ...
- Spring boot 集成 Swagger
添加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- (jdbc和cmd)sqlite数据迁入mysql(导入导出)
从sqlite进行导出数据 进行cmd命令 第一步:sqlite3->.open [文件路径](打开连接)->.tables(这个是查看表是否有没有)->.cd [切换的盘符](这里 ...
- 一篇RxJava友好的文章(二)
上一篇文章介绍了rxjava的基本用法,和一些常用的操作符,以及rxjava的链式操作带来的好处.由于rxjava非常的强大,让我如此的痴迷,我打算写五篇文章,专门讲解rxjava 常见的操作符和用法 ...
- java自定义泛型 面试题:接收任意数组进行反转 泛型通配符
不用泛型只能操作某种类型进行反转 代码如下: package com.swift.fanxing; import org.junit.Test; public class RenyiReverse { ...
- jquery-ui-custom autocomplete
//jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8& ...
- python读写dbf数据库
dbf数据库作为一种简单的数据库,曾经广泛使用.现在在金融领域还是有很多的应用之处,工作中遇到此类的问题,在此记录一下. 1. 读取dbf ''' 读取DBF文件 ''' def readDbfFil ...
- 转:SpringCloud服务注册中心比较:Consul vs Zookeeper vs Etcd vs Eureka
原文链接地址:http://luyiisme.github.io/2017/04/22/spring-cloud-service-discovery-products/ 这里就平时经常用到的服务发现的 ...