
1 manhere 2013-12-13 01:08:42 +08:00 php递归? |
3 kavinyao 2013-12-13 01:29:20 +08:00 via iPhone 为什么不用循环? |
5 cyr1l 2013-12-13 01:48:44 +08:00 我去, 看这 PHP 好像 Javascript..... |
6 cyr1l 2013-12-13 02:22:01 +08:00 <?php function sulution($num){ for($a=1;$a<=$num;$a++){ $b=$c=$a; for($d=$a;$d>0;$d--){ if($d>=2){ $b=$b*($d-1); $c=$c."*".($d-1); }else{ echo $c."=".$b."<br/>"; } } } } sulution(10); ?> 尝试写了一下, 好像ok. 我PHP学的不好, 完全当成JS写的. |
7 txlty 2013-12-13 02:22:38 +08:00 <?php for($i=1;$i<=10;$i++){ for($j=$i;$j>=1;$j--){ $arr[$i][]=$j; } } for($i=1;$i<=10;$i++){ echo implode($arr[$i],'*').'='.array_product($arr[$i])."\r\n"; } ?> |
8 cyr1l 2013-12-13 02:23:21 +08:00 MD,单词还拼错了. 丢人了又... |
9 txlty 2013-12-13 02:24:39 +08:00 换成<br> <?php for($i=1;$i<=10;$i++){for($j=$i;$j>=1;$j--){$arr[$i][]=$j;}} for($i=1;$i<=10;$i++){echo implode($arr[$i],'*').'='.array_product($arr[$i]).'<br>';} ?> |
10 cyr1l 2013-12-13 02:25:27 +08:00 我去LSS好NB.不明觉厉. |
11 cyr1l 2013-12-13 02:26:56 +08:00 错了, LSSSS... 你老跟我抢楼... |
12 txlty 2013-12-13 03:01:50 +08:00 <?php $arr=array(); for($i=1;$i<=10;$i++){array_unshift($arr,$i);echo implode($arr,'*').'='.array_product($arr)."<br>";} ?> 总算缩成一个循环了。小站长水平,见笑。 |
13 cyr1l 2013-12-13 03:10:59 +08:00 我去, 楼上你来劲了. 来个Ruby版的. def xx(i) (1..i).map{|n| p (1..n).to_a.join("*")+"="+(1..n).inject(:*).to_s} end xx(10) 应该还能更短的. 我就这水平了. |
14 ccidcce32167 2013-12-13 10:55:11 +08:00 <?php function jiecheng($i){ for($a=1;$i!=1;$i--)$a*=$i; return $a; } ?> <?php echo jiecheng(10); ?> 这个可否满足您? |
15 ccidcce32167 2013-12-13 10:56:20 +08:00 可以写成一行的 <?php function jiecheng($i){for($a=1;$i!=1;$i--){$a*=$i;}return $a;} ?> |
17 mantianyu 2013-12-13 13:44:42 +08:00 @ccidcce32167 人家要前面那串 "10*9*8*7*6*5*4*3*2*1=" 也输出出来... |
18 10iii 2013-12-13 13:50:57 +08:00 我是来测试gist的。 https://gist.github.com/10iii/7940276 |
19 10iii 2013-12-13 13:52:12 +08:00 |
20 ccidcce32167 2013-12-13 14:19:15 +08:00 @mantianyu = =#好吧 两行 <?php function jiecheng($i){for($a=1;$i>=1;$i--){echo $i.($i!=1?"*":"");$a*=$i;}echo "=".$a."<br />";} ?> <?php for($t=1;$t<=10;$t++){jiecheng($t);} ?> |
21 zhujinliang 2013-12-13 14:35:34 +08:00 没人想到用神器eval么。。。 不过看起来比楼上某些同学的更长。。。 while($i++ < 10) { $s=$i.$p.$s; $p='*'; echo "$s=".eval("return $s;").'<br/>'; } 顺便测试了php对未定义变量的容忍程度,居然未初始化的int与string都可以通过并按预想地执行。 |
22 zhujinliang 2013-12-13 15:22:04 +08:00 次奥,玩上瘾了。。。 目前是不是我的最短~~(°°) <?php for(;$s=++$i.$p.$s,$i<11;$p='*')eval("echo '$s='.($s).'<br>';"); 除去<?php部分,长度是64字节 |
23 thbourlove 2013-12-13 15:24:41 +08:00 |
24 thbourlove 2013-12-13 15:29:06 +08:00 这么多人喜欢把php当perl用吗? |
25 Ever 2013-12-13 15:44:26 +08:00 来个fp点的吧 <?php function multiplication_table($max=10, $current=1, $cache_expr="", $cache_result=1, $row_sep="\n", $result=array()){ $expr = $current ===1 ? $current: "{$current} * {$cache_expr}"; $cache_result *= $current++; $result[] = "$expr = {$cache_result}"; return $current<=$max? multiplication_table($max, $current, $expr, $cache_result, $row_sep, $result):join($row_sep, $result); } echo multiplication_table(10); echo "\n"; |
26 msg7086 2013-12-13 15:47:29 +08:00 |
27 thbourlove 2013-12-13 15:52:26 +08:00 @msg7086 快慢其实根本测不出。。不用gmp到20+就果断溢出了。。 |
28 msg7086 2013-12-13 15:56:11 +08:00 |
29 10iii 2013-12-13 17:04:17 +08:00 这样? <script src="https://gist.github.com/10iii/7940276.js"></script> |
30 10iii 2013-12-13 17:17:24 +08:00 |
31 10iii 2013-12-13 17:18:18 +08:00 <script src="https://gist.github.com/%31%30iii/7940276.js"></script> |
32 Fedor 2013-12-13 17:22:06 +08:00 |
33 Fedor 2013-12-13 17:22:53 +08:00 |
34 Geeker 2013-12-13 18:04:41 +08:00 刚学PHP,看了楼上的各种代码,整个人都不好了ORZ |
35 infong 2013-12-13 21:10:05 +08:00 看完楼上们的回复,感觉整个都不知道php是什么了。 |
36 mikej 2013-12-13 21:58:32 +08:00 这帖子很v2ex |
37 mantianyu 2013-12-13 23:51:06 +08:00 |
38 mantianyu 2013-12-13 23:51:34 +08:00 |
39 mantianyu 2013-12-13 23:54:17 +08:00 这下可以了吧 <script src="https://gist.github.com/cifer-lee/7946292"></script> |
40 mantianyu 2013-12-13 23:56:32 +08:00 再来一次 <script src="https://gist.github.com/thbourlove/7940940.js"></script> |
41 mantianyu 2013-12-13 23:56:55 +08:00 我了个去 <script src="https://gist.github.com/thbourlove/7940940"></script> |
42 mantianyu 2013-12-13 23:58:12 +08:00 <script src="https://gist.github.com/cifer-lee/7946292"></script> |
43 mantianyu 2013-12-14 00:01:27 +08:00 |
44 mantianyu 2013-12-14 00:01:56 +08:00 <?php // 递归版 $s = 1; function jiecheng($num) { global $s; $s *= $num; if($num == 1) return "$num = $s \n"; else return "$num * " . jiecheng($num - 1); } // 循环版 function jiecheng2($num) { for($m=1,$s=1,$i=1;$i<$num;++$i) {$m=($i+1)." * $m";$s*=($i+1);}echo "$m = $s\n"; } |
45 octref 2013-12-14 00:33:38 +08:00 function factorial($x) { for($i=1, $j=1; $i <= $x; $j*=$i, $i++); return $j; } echo factorial(10) |
46 picasso250 2013-12-14 13:17:23 +08:00 |
47 picasso250 2013-12-14 13:17:49 +08:00 |
48 yangff 2013-12-15 13:27:32 +08:00 @picasso250 噗。。 |
49 thbourlove 2013-12-15 16:01:43 +08:00 已笑尿。。。 |
50 faceair 2013-12-15 20:44:10 +08:00 @mantianyu 我来试贴一下,转换不出来不要笑。。 https://gist.github.com/cifer-lee/7946292 |
52 mantianyu 2013-12-15 20:47:39 +08:00 |
53 mantianyu 2013-12-15 20:49:52 +08:00 <script src="https://gist.github.com/cifer-lee/7946292.js"></script> |
54 thwawar 2013-12-15 22:28:10 +08:00 |
55 thwawar 2013-12-15 22:28:47 +08:00 直接贴那个 gist 在地址栏的地址就好了。。。 |