新来一个同事做 PHP 两年 不会写九九乘法表。。。。

2018-11-18 15:18:48 +08:00
 chaodada

如题。。。

22323 次点击
所在节点    PHP
176 条回复
183387594
2018-11-19 11:44:52 +08:00
起初他们写各种 XX 树,我没有说话,因为我不会写。现在你们竟然要写乘法口诀表,我...
liuxu
2018-11-19 11:48:19 +08:00
培训班出来的都是 2 年
yoshiyuki
2018-11-19 13:28:11 +08:00
你们这个岗位薪资多少? 面试官做了几年技术?
zjsxwc
2018-11-19 13:29:43 +08:00
起初他们写各种 XX 树,我没有说话,因为我不会写。现在你们竟然要写乘法口诀表,我...
Amance
2018-11-19 13:34:18 +08:00
管你什么事?你是负责人还是啥?你还是嫉妒?还是开不起他?你一个月的工资因为他少拿了?你见不惯别人拿这么高的工资?你付出的比他多很多?一天天没事做事,不要断别人财路,公司不是你开的你就省点心。
lideshun123
2018-11-19 13:39:41 +08:00
蛋逼吧
cincout
2018-11-19 14:16:49 +08:00
@AscenZ 这个是不是更优雅,哈哈哈哈
for i in 1...9 {
var str = ""
for j in 1...i {
str += "\(j)x\(i)=\(i*j) "
}
print(str)
}
mmp123
2018-11-19 14:28:58 +08:00
python 一行就足够了

print ('\n'.join([' '.join(['%s*%s=%-2s' % (j,i,i*j) for j in range(1,i+1)]) for i in range(1,10)]))
markgor
2018-11-19 14:34:19 +08:00
<?php
$IsCLI = preg_match("/cli/i", php_sapi_name()) ? true : false;
$S_SPACE = $IsCLI ? ' ' : '&nbsp;&nbsp;';
$S_EOL = $IsCLI ? PHP_EOL : '</br>';

for($col=1;$col<=9;$col++){
for($row=1;$row<=$col;$row++){
echo strpad($col) . '*' . strpad($row) . '=' . strpad($col*$row) . $S_SPACE;
}
echo $S_EOL;
}
function strpad($str){
GLOBAL $S_SPACE;
return str_pad($str,3,$S_SPACE,STR_PAD_BOTH);
}
?>

1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 =12 4 * 4 =16
5 * 1 = 5 5 * 2 =10 5 * 3 =15 5 * 4 =20 5 * 5 =25
6 * 1 = 6 6 * 2 =12 6 * 3 =18 6 * 4 =24 6 * 5 =30 6 * 6 =36
7 * 1 = 7 7 * 2 =14 7 * 3 =21 7 * 4 =28 7 * 5 =35 7 * 6 =42 7 * 7 =49
8 * 1 = 8 8 * 2 =16 8 * 3 =24 8 * 4 =32 8 * 5 =40 8 * 6 =48 8 * 7 =56 8 * 8 =64
9 * 1 = 9 9 * 2 =18 9 * 3 =27 9 * 4 =36 9 * 5 =45 9 * 6 =54 9 * 7 =63 9 * 8 =72 9 * 9 =81
vicvinc
2018-11-19 14:42:03 +08:00
```javascript
const dp = new Array(10)
.fill(0)
.map((x, i) =>
new Array(10)
.fill(0)
.map((y, j) => (i > 0 && j > 0 ? `${i}*${j}=${i * j}` : 0))
.filter(x => x !== 0)
)
.map(x => x.join(" "))
.filter(x => x !== "")
.join("\n");

console.log(dp);
```
ashin
2018-11-19 14:43:31 +08:00
我随手写出来居然长这个样, 这算九九表吗

```
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
```
vicvinc
2018-11-19 14:43:40 +08:00
up 一下:
`javascript
const dp = new Array(10)
.fill(0)
.map((x, i) =>
new Array(10)
.fill(0)
.map((y, j) => (i > 0 && j > 0 && i >= j ? `${i}*${j}=${i * j}` : 0))
.filter(x => x !== 0)
)
.map(x => x.join(" "))
.filter(x => x !== "")
.join("\n");

console.log(dp);

`
ashin
2018-11-19 14:45:21 +08:00
package main

import (
"fmt"
)

func main() {
for i := 1; i <= 9; i++ {
for j := 1; j <= i + 0; j++ {
fmt.Printf("%d * %d = %d\t", i, j, i*j)
}
fmt.Println()
}
}
ruixingchen
2018-11-19 14:48:42 +08:00
两层循环,用第一层循环的值作为第二层循环的临界条件,就可以了吧,代码就不上了
ashin
2018-11-19 14:50:10 +08:00
我改了三次才写对 哈哈 要被楼主鄙视

package main

import (
"fmt"
)

func main() {
for i := 1; i <= 9; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d * %d = %d\t", j, i, i*j)
}
fmt.Println()
}
}


1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
90safe
2018-11-19 14:51:44 +08:00
```
for ($i=1; $i < 10; $i++) {
for ($x=1; $x < 10; $x++) {
$arr[$i * $x] = "$i * $x = " . $i * $x . PHP_EOL;
}
}
```
hauzi
2018-11-19 15:03:10 +08:00
<?php
for ($i = 1; $i <= 9; $i++) {
for ($j = $i; $j <= 9; $j++) {
print($i);print('*');print($j);print('=');print($i*$j);print(' ');

}
echo PHP_EOL;
}
?>
AscenZ
2018-11-19 15:11:15 +08:00
@cincout #147
兄弟 学习了
NEETLEE
2018-11-19 16:58:56 +08:00
@fyibmsd 这样子怕是有偏差哦
fyibmsd
2018-11-19 18:29:40 +08:00
@NEETLEE
- printf('%d*%d=%d ', $j, $i, $j * $j);
+ printf('%d*%d=%d ', $j, $i, $j * $i);

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/508959

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX