php - 任何人都知道如何在 php 中打印此模式
问题描述
$n = 5;
$n1 = 4;
for ($i = 0; $i <= $n; $i++) {
for ($j = 0; $j <= $n1; $j++) {
if (($i == 0 || $i == 5) && ($j > 0 and $j < 3)) {
if (($i == 0 && $j == 1) || ($i == $n && $j != 2)) {
printf(" ");
}
printf("$i");
} elseif (($j == 0 || $j == 3) && ($i != 0 and $i != 5)) {
if ($j == 0 && $i != 1 && $i != 4) {
printf("");
} elseif (($j == 3 && $i != 1 && $i != 4)) {
printf("  ");
} elseif ($j == 0) {
printf(" ");
}
printf("$i");
} else {
printf(" ");
}
}
echo "<be>";
`
解决方案
只是为了好玩,我用 JS 编写了它,但转换为 PHP 可能非常简单(String.repeat => str_repeat
、、、[].push => array_push
等[].slice => array_slice
)
const arr = [];
const output = (x=1, y=1) => {
for (let k = 0; k < 5; k++) {
arr.push([' '.repeat(5-k), k, ' '.repeat(k), k, ' '.repeat(5-k)].join(''))
}
for (let k = 5; k <= 10; k++) {
arr.push([' '.repeat(k-5), 10-k, ' '.repeat(10-k), 10-k, ' '.repeat(k-5)].join(''))
}
arr.forEach((row, index) => {
arr[index] = row + row.substring(1).repeat(x - 1);
});
const orig = [...arr.slice(1)]
for (let i = 1; i < y; i++) {
arr.push(...orig);
}
console.log(arr.join('\n'))
}
output(2,3)
推荐阅读
- pandas - 将行附加到 DataFrame 时出现问题。到 Pandas Dataframe 的 ZMQ 消息
- ios - URLSession dataTask: POST 请求说无法连接到服务器,可以使用 POSTMAN 访问
- r - 使用标签功能时的错误消息
- swift - 可变多线程访问 - 损坏
- ios - 我将如何继续在屏幕右侧的随机位置生成 SKSpriteNode 以将其从左侧的随机位置转换?
- hadoop - 如何创建 hive 仓库目录?
- php - 在 php 中将 mssql 转换为 sqlsrv。存储过程成功时不返回正确结果
- html - 本地主机和域上图像的 Asp.Net web core Url 产生不同的结果
- android - 如何在gridview android中计算总值
- android - 如何使用约束布局为新意图设置动画