CPTTRN1 - Character Patterns (Act 1)--php解法

CPTTRN1 - Character Patterns (Act 1)
#basics
Using two characters: . (dot) and * (asterisk) print a chessboard-like pattern. The first character printed should be * (asterisk).

Input

You are given t < 100 - the number of test cases and for each of the test cases two positive integers: l - the number of lines and c - the number of columns in the pattern (l, c < 100).

Output

For each of the test cases output the requested pattern (please have a look at the example). Use one line break in between successive patterns.

Example

Input:
3
3 1
4 4
2 5

Output:
*
.
*

*.*.
.*.*
*.*.
.*.*

*.*.*

.*.*.


我的解法一:

<?php

$hi = fopen('php://stdin', "r");
$ho = fopen('php://stdout', "w");

while($some=trim(fgets($hi,100)))
{
	list($a, $b) = explode(' ', $some);
	if($a&&$b)
	{
		for($i=0;$i<$a;$i++)
		{
			for($j=0;$j<$b;$j++)
			{
				if(($i%2==0 && $j%2==0)||($i%2==1 && $j%2==1) )
					fwrite($ho,  "*");
				else
					fwrite($ho,  ".");
		
			}
			fwrite($ho,  "\n");
		}
	}else
		continue;
	fwrite($ho,  "\n");
}

fclose($ho);
fclose($hi);


我的解法二:

<?php

$hi = fopen('php://stdin', 'r');
$ho = fopen('php://stdout', 'w');
fscanf($hi, '%d', $num);

for($i=0;$i<$num;$i++)
{
	$line = trim(fgets($hi,100));
	list($left, $right) = explode(' ', $line);

	for($j=0;$j<$left; $j++)
	{
		for($k=0;$k<$right; $k++)
		{
			if(($k%2==0 && $j%2==0)||($k%2==1 && $j%2==1))
				fwrite($ho,  "*");
			else
				fwrite($ho,  ".");
		}
		fwrite($ho,  "\n");
	}
	fwrite($ho,  "\n");
}
fclose($ho);
fclose($hi);
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值