php之for循环 用php输出1121123211234321123211211 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php$max = isset($_GET['t']) ? $_GET['t'] : 4;$row = 1;$row_break = true;$str='';while (true) { $b = true; $i = 1; while (true) { $str.=$i; if ($i >= $row) $b = false; $b ? $i++ : $i--; if (!$i){ echo $str. '<br />'; break; } } if ($row >= $max) $row_break = false; $row_break ? $row++ : $row--; $str=''; if (!$row) break;} <?php$max = isset($_GET['t'])&& is_numeric($_GET['t']) ? $_GET['t'] : 4;$current_row=$max-1;while (true) { $row=$max-abs($current_row--); $col=$row-1; $str=''; while (true) { $str.=$row-abs($col--); if ($col== -$row){ echo $str. '<br />'; break; } } if ($current_row == -$max) break;}再简化一下。 这不是php问题,是编程基础问题,LZ是来送分的吧 嗯 之所以奇怪是 人家测试个for输出,有这么奇葩的代码...$max = isset($_GET['t'])&& is_numeric($_GET['t']) ? $_GET['t'] : 4; php代码for ($index = 1; $index < 5; $index++) { for ($indexl = 1; $indexl <= $index; $indexl++) { echo $indexl; } for ($indexl = $index-1; $indexl >0 ; $indexl--) { echo $indexl; } echo '<br>';}for ($index = 5; $index > 0; $index--) { for ($indexl = 1; $indexl <= $index; $indexl++) { echo $indexl; } for ($indexl = $index-1; $indexl >0 ; $indexl--) { echo $indexl; } echo '<br>';}html结果页面11211232112343211234543211234321123211211 汗 第2个for写差了for ($index = 1; $index < 5; $index++) { for ($indexl = 1; $indexl <= $index; $indexl++) { echo $indexl; } for ($indexl = $index-1; $indexl >0 ; $indexl--) { echo $indexl; } echo '<br>';}for ($index = 3; $index > 0; $index--) { for ($indexl = 1; $indexl <= $index; $indexl++) { echo $indexl; } for ($indexl = $index-1; $indexl >0 ; $indexl--) { echo $indexl; } echo '<br>';}1121123211234321123211211 for ($index = 5; $index > 0; $index--) {红色数字 改成 3 汗 我也来个最简单的$sum = 5;for($i=1; $i<=$sum; $i++) { for($k=1; $k<=$sum; $k++) { echo $k; if($k == $i) break; } for($k=$i-1; $k>=1; $k--) { if($i == $k) break; echo $k; } echo '<br/>';}/**输出结果:1121123211234321123454321*/ $len = 9;for($i=1,$n=2*$len;$i<$n;++$i){ $arr[] = $i == 1 || $i == $n-1 ? '1' : join('',range(1, $len-abs($i-$len))) . join('',range($len-1-abs($i-$len), 1));}echo join("\n", $arr);/**1121123211234321123454321123456543211234567654321123456787654321123456789876543211234567876543211234567654321123456543211234543211234321123211211/**/ 总算勾引出来一点精彩的代码了,range函数的用法,秒哇。 <?php$n = $_REQUEST['n'] - 1;if ($n < 1) $n = 1;if ($n > 8) $n = 8;?><?phpfor ($i = -$n; $i <= $n; $i++) { for ($j = abs($i) - $n; $j <= $n - abs($i); $j++) { printf('%d', $n - abs($i) - abs($j) + 1); } echo '<br>';}?>委琐流 小弟艰苦的写了半天...准备学习楼上的!<?php$b=4;for($i=1;$i<=$b;$i++){ $a=$i; for($g=1;$g<=$a;$g++) { echo $g; } for ($h=$i-1;$h>0;$h--) { echo $h; } echo "<br>";}for($i=$b-1;$i>=1;$i--){ $a=$i; for($g=1;$g<=$a;$g++) { echo $g; } for ($h=$i-1;$h>0;$h--) { echo $h; } echo"<br>";}?> echo '1'echo '121'echo '12321'echo '1234321'echo '123454321'echo '12345654321'echo '1234567654321'echo '123456787654321'echo '12345678987654321'echo '123456787654321'echo '1234567654321'echo '12345654321'echo '123454321'echo '1234321'echo '12321'echo '121'echo '1'我这个地球人都知道 不就比简单额...咱也会.$i = 1;$j = 2;$k = 3;$f = 4;echo $i.'</br>';echo $i.$j.$i .'</br>';echo $i.$j.$k.$j.$i.'</br>';echo $i.$j.$k.$f.$k.$j.$i.'</br>';echo $i.$j.$k.$j.$i.'</br>';echo $i.$j.$i .'</br>';echo $i.'</br>'; $len = 4;$arr = array();for($i=1;$i<=$len;$i++){ $arr[$i] = join('',range(1,$i)); for($j = $i-1;$j>=1;$j--){ $arr[$i] .= $j; }}$array = $arr;unset($arr[$len]);rsort($arr);$array = array_merge($array,$arr);print_r($array); <?php $num=9; for($i=1;$i<$num*2;$i++){ $temm=$num-abs($num-$i); for($j=1;$j<2*$temm;$j++) printf($temm-abs($temm-$j)); printf("<br />"); }?>1121123211234321123454321123456543211234567654321123456787654321123456789876543211234567876543211234567654321123456543211234543211234321123211211 正则匹配换行问题 AJAX中文乱码PHP完美解决(IE和Firefox兼容) PHP对MYSQL日期字段的查询 file_get_contents 超时问题 成都的php程序员薪水几何?很久没来了,散点分! PHP在使用post传递数据时,action="login.php?id=2",点击提交,$_POST["id"]无法显示,即没有被设置! PHP验证中文表达式 急!!!!!!! PHP在IIS上面不成功,老是弹出验证框 PHP 4.3.1下如何安装支持GIF的GD库... 有关php和iis以及apache的问题,我给80分 fck获取第一章图片当缩略图 PHP跨域上传附件问题
<?php
$max = isset($_GET['t']) ? $_GET['t'] : 4;
$row = 1;
$row_break = true;
$str='';
while (true) {
$b = true;
$i = 1;
while (true) {
$str.=$i;
if ($i >= $row)
$b = false;
$b ? $i++ : $i--;
if (!$i){
echo $str. '<br />';
break;
}
}
if ($row >= $max)
$row_break = false;
$row_break ? $row++ : $row--;
$str='';
if (!$row)
break;
}
$max = isset($_GET['t'])&& is_numeric($_GET['t']) ? $_GET['t'] : 4;
$current_row=$max-1;
while (true) {
$row=$max-abs($current_row--);
$col=$row-1;
$str='';
while (true) {
$str.=$row-abs($col--);
if ($col== -$row){
echo $str. '<br />';
break;
}
}
if ($current_row == -$max)
break;
}
再简化一下。
for ($indexl = 1; $indexl <= $index; $indexl++) {
echo $indexl;
}
for ($indexl = $index-1; $indexl >0 ; $indexl--) {
echo $indexl;
}
echo '<br>';
}
for ($index = 5; $index > 0; $index--) {
for ($indexl = 1; $indexl <= $index; $indexl++) {
echo $indexl;
}
for ($indexl = $index-1; $indexl >0 ; $indexl--) {
echo $indexl;
}
echo '<br>';
}
html结果页面1
121
12321
1234321
123454321
1234321
12321
121
1
for ($indexl = 1; $indexl <= $index; $indexl++) {
echo $indexl;
}
for ($indexl = $index-1; $indexl >0 ; $indexl--) {
echo $indexl;
}
echo '<br>';
}
for ($index = 3; $index > 0; $index--) {
for ($indexl = 1; $indexl <= $index; $indexl++) {
echo $indexl;
}
for ($indexl = $index-1; $indexl >0 ; $indexl--) {
echo $indexl;
}
echo '<br>';
}1
121
12321
1234321
12321
121
1
红色数字 改成 3 汗
for($i=1; $i<=$sum; $i++) {
for($k=1; $k<=$sum; $k++) {
echo $k;
if($k == $i) break;
}
for($k=$i-1; $k>=1; $k--) {
if($i == $k) break;
echo $k;
}
echo '<br/>';
}
/**
输出结果:
1
121
12321
1234321
123454321
*/
$len = 9;
for($i=1,$n=2*$len;$i<$n;++$i){
$arr[] = $i == 1 || $i == $n-1 ? '1' : join('',range(1, $len-abs($i-$len))) . join('',range($len-1-abs($i-$len), 1));
}
echo join("\n", $arr);
/**
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
/**/
$n = $_REQUEST['n'] - 1;
if ($n < 1) $n = 1;
if ($n > 8) $n = 8;
?>
<?php
for ($i = -$n; $i <= $n; $i++) {
for ($j = abs($i) - $n; $j <= $n - abs($i); $j++) {
printf('%d', $n - abs($i) - abs($j) + 1);
}
echo '<br>';
}
?>
委琐流
$b=4;
for($i=1;$i<=$b;$i++)
{
$a=$i;
for($g=1;$g<=$a;$g++)
{
echo $g;
}
for ($h=$i-1;$h>0;$h--)
{
echo $h;
}
echo "<br>";
}
for($i=$b-1;$i>=1;$i--)
{
$a=$i;
for($g=1;$g<=$a;$g++)
{
echo $g;
}
for ($h=$i-1;$h>0;$h--)
{
echo $h;
}
echo"<br>";
}
?>
echo '121'
echo '12321'
echo '1234321'
echo '123454321'
echo '12345654321'
echo '1234567654321'
echo '123456787654321'
echo '12345678987654321'
echo '123456787654321'
echo '1234567654321'
echo '12345654321'
echo '123454321'
echo '1234321'
echo '12321'
echo '121'
echo '1'我这个地球人都知道
$j = 2;
$k = 3;
$f = 4;echo $i.'</br>';
echo $i.$j.$i .'</br>';
echo $i.$j.$k.$j.$i.'</br>';
echo $i.$j.$k.$f.$k.$j.$i.'</br>';
echo $i.$j.$k.$j.$i.'</br>';
echo $i.$j.$i .'</br>';
echo $i.'</br>';
$len = 4;
$arr = array();
for($i=1;$i<=$len;$i++){
$arr[$i] = join('',range(1,$i));
for($j = $i-1;$j>=1;$j--){
$arr[$i] .= $j;
}
}
$array = $arr;
unset($arr[$len]);
rsort($arr);
$array = array_merge($array,$arr);
print_r($array);
$num=9;
for($i=1;$i<$num*2;$i++){
$temm=$num-abs($num-$i);
for($j=1;$j<2*$temm;$j++) printf($temm-abs($temm-$j));
printf("<br />");
}
?>1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1