面试题 06162012

/*
A game of Tic Tac Toe has just been completed. Write a function that prints whether X's or O's have won. The game board is passed in as an array of integers in row-column order. The Number 2 represents X and the number 1 represents O. A zero represents that the space is empty.

For example the gameboard below would be represented as int* gameboard = [2,0,1,1,1,0,2,2,2]

x-o
----
oo-
----
xxx
*/



#include <iostream>

void tic_tac_toe(int* gb)
{
	for(int i = 0; i < 3; i++)
	{
		int product = 1;
		for(int j = 0; j < 3; j++)
		{
			product *= gb[3 * i + j];
		}
		if(product == 1)
		{
			printf("O won.");
			return;
		}
		else if(product = 8)
		{
			printf("P won.");
			return;
		}
	}

	int dia_product1 = gb[0] * gb[4] * gb[8];
	if(dia_product1 == 1)
	{
		printf("O won.");
		return;
	}
	else if(dia_product1 = 8)
	{
		printf("P won.");
		return;
	}
	
	int dia_product2 = gb[2] * gb[4] * gb[6];
	if(dia_product2 == 1)
	{
		printf("O won.");
		return;
	}
	else if(dia_product1 = 8)
	{
		printf("P won.");
		return;
	}


	printf("No won.");
	return;

};


int main()
{
	int gameboard[] = {2,0,1,1,1,0,2,2,2};
	tic_tac_toe(gameboard);


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值