码迷,mamicode.com
首页 > 其他好文 > 详细

1515: Play whit bear kid 模拟题——(逻辑关系要注意)

时间:2015-02-28 21:41:43      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:acm   模拟题   

题目描述

Happy Spring Festival everyone! Many relatives will visit your house, of course they have bear kids. Now you are playing chess with a bear kid.
It‘s really terrible. You have a 3*3 chessboard, bear kid puts 0s and you put 1s while the empty positions are ‘.‘ . The rule is that one whose
chess pieces are in a line horizontally, vertically or diagonally will win.

输入

There are several cases.
For each case, there is a 3*3 chessboard and the chess pieces have been put.

输出

You should judge whether someonw has won or not.
If the bear kid win, output "BEAR KID".
If you win, output "YOU".
Output "NO BODY" in other cases.

We guarantee you and bear kid won‘t win at the same time, and the number of 0s and 1s may be not the same.

样例输入

000
...
...

样例输出

BEAR KID

题目的意思大致为给你已经摆好的一幅3*3棋盘,判断是谁赢。

直接判断横着的,竖着的和斜着的,这里注意,斜着的线有两条,不要漏掉。

#include<stdio.h>
#include<string.h>
int main(){
	char a[5][5];
	int i,j,k1=0,k2=0,k3=0,num1=0,num2=0;
	while(scanf("%s%s%s",a[0],a[1],a[2])!=EOF){
		k1=k2=k3=0;
		num1=num2=0;
	if((a[0][0]==a[1][1] && a[1][1]==a[2][2])||(a[0][2]==a[1][1] && a[1][1]==a[2][0])){
		if(a[1][1]=='0') {puts("BEAR KID"); k1++;}
		else if(a[1][1]=='1') {puts("YOU");  k1++;}
	}
	if(k1==0){
		for(i=0;i<3;i++){
			num1=num2=0;
			for(j=0;j<3;j++){
				if(a[i][j]=='0')  num1++;
				else if(a[i][j]=='1') num2++;
			}
			if(num1==3)  {puts("BEAR KID"); k2++; break;}
			else if(num2==3)  {puts("YOU"); k2++; break;}
		}
	}
	if(k1==0 && k2==0){
		for(j=0;j<3;j++){
			num1=num2=0;
			for(i=0;i<3;i++){
				if(a[i][j]=='0')  num1++;
				else if(a[i][j]=='1') num2++;
			}
			if(num1==3)  {puts("BEAR KID"); k3++; break;}
			else if(num2==3)  {puts("YOU"); k3++; break;}
		}
	}
	if(k1==0 && k2==0 && k3==0){
		puts("NO BODY");
	}
	//printf("%d%d%d\n",k1,k2,k3);
	}
}


 

我一开始老是错掉,也不知道为什么,后来才发现是逻辑错误,一开始写if的地方都写成了else if,那么肯定错,因为执行了其中的一个else if后下面的就不会再进行了,所以这里就全改成if,然后就直接AC,不过也算是给我长了经验,哈哈。

1515: Play whit bear kid 模拟题——(逻辑关系要注意)

标签:acm   模拟题   

原文地址:http://blog.csdn.net/acmer_hades/article/details/43989521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!