javascript - 如何生成具有给定开始值和结束值的数字模式?
问题描述
如何使用 php 生成这样的数字模式?
一个。开始 = 1,结束 = 3 123 231 312 湾。开始 = 2 ,结束 = 7 234567 345672 456723 567234 672345 723456
更新:我试过这段代码:
function generate (int $start, int $end)
{
$arr = [];
for($start; $start <= $end; $start ++) {
$arr[] = $start;
}
for($i = $arr[0]; $i <= count($arr); $i++) {
for($l = $i - 1; $l < $end; $l ++) {
echo $arr[$l];
}
echo " -> $i<br/>";
}
}
并得到这个输出:
12345
2345
345
45
5
但是如何显示其余的数字?
解决方案
你可以试试这个算法:
const generate = (start, end) => {
const length = end-start+1
let array = Array.from({length}, () => Array.from({length}, (x,i)=>i+start)) // creating 2D array and filling it with a loop from start value to end value
for (let i = 0; i < array.length; i++) {
poped = array[i].splice(i); // slice and put the element from index i to the last index
array[i].unshift(...poped) // adding poped value to the begining of the array
}
return array
}
console.log(generate(1,3))
console.log(generate(2,7))
推荐阅读
- python - 如何获得最后 2 个轴的 argmax?
- java - 如何将实体收集为“地图
>”? - css - 如何在 v-col 内装上绝对的 v-overlay?
- visual-studio - 为什么这个 std::map 不能在 Visual C++ 的监视窗口中以可用的方式显示?
- django - DetailView Django 不工作错误页面未找到
- c# - 有没有其他正确的方法可以在 C# 表单中的两个不同选项卡上进行相同的控制?
- spring-boot - 带有原型 bean 的 Spring AOP
- reactjs - 如何在 React 的输入元素中突出显示主题标签?
- angular - Angular and Http Calls How to make an http call to keep trying until a success response is received?
- javascript - 在反应中访问'scrollLeft'属性