如何在一个单一的模式中用星星和数字组合金字塔



需要一个带有数字和星星的金字塔,并且没有示例可以查看。

*
* 0 *
* 1 0 1 *
* 0 1 0 1 0 *
* 1 0 1 0 1 0 1 *

我可以得到一些帮助吗?

这就是我所拥有的,但我无法弄清楚如何获得那里的数字。

function displayPyramid(n) {
for (let i = 0; i < n; i++) {
let str = ''
let count = 0
// Spacing before each row
for (let j = 1; j < n - i; j++) {
str = str + ' '
}
// Row worth of data
const max = 2 * i + 1
for (let k = 1; k <= max; k++) {
str = str + '*'
}
console.log(str);
}
}
displayPyramid(5)

以下是获得预期结果的思考过程:

  • 所有具有偶数索引的行都称为"偶数行"(0、2、4、6 等(

  • 所有具有不均匀索引的行称为"不均匀行"(1、3、5、7 等(

  • 所有星星都是任何行中的第一列或最后一列

  • 对于所有偶数行,偶数索引显示为0s,不偶数索引显示为1s

  • 对于所有不偶数
  • 行,不偶数索引显示0s,偶数索引显示1s

我添加了一个名为getSymbol的函数,该函数执行最后三次检查并返回相应的符号。我还重命名了迭代器变量以提高可读性。

function getSymbol(column, max, row) {
if (column === 0 || column === max - 1) { // if it is the first or last index within the row
return '*'
}
if (row % 2 === 0) { // if it is an uneven row
if (column % 2 === 0) { // if it is an even column
return '0';
}
return '1' // if it is an uneven column within that row
}
// if it's not an even row, it's an uneven row
if (column % 2 === 0) { // if it is an even column within that row
return '1';
}
return '0' // if it is an uneven column within that row
}
function displayPyramid(n) {
for (let row = 0; row < n; row++) {
let str = ''
let count = 0
// Spacing before each row
for (let spaces = 1; spaces < n - row; spaces++) {
str = str + ' '
}
// Row worth of data
const max = 2 * row + 1
for (let column = 0; column < max; column++) {
str = str + getSymbol(column, max, row)
}
console.log(str);
}
}
displayPyramid(5)

function displayPyramid(n) {
for (let i = 0; i < n; i++) {
let str = "";
let count = 0;
// Spacing before each row
let padCount = 2 * n - 2 * i - 1;
for (let j = 1; j < padCount; j++) {
str = str + " ";
}
// Row worth of data
const max = 2 * i + 1;
for (let k = 1; k <= max; k++) {
if (k === 1 || k === max) {
str = str + "* ";
} else {
str += (k % 2).toString() + " ";
}
}
console.log(str);
}
}
displayPyramid(5);

让我们首先尝试为金字塔的一行(任何行(编写一个函数。我们现在也会忽略空格。

金字塔的每一条线似乎都遵循一些简单的规则:

  • 所有行都以*开头。
  • "内序"只是交替10一定次数。
  • 每行的内部序列中都有2*n - 1位数字,但第一行除外。
  • 偶数行 (0, 2, 4, ...( 以1开头的内部序列。
  • 奇数行 (1, 3, 5, ...( 以0开头的内部序列。
  • 所有行都以附加*结尾,除了第一行。

在上面的规则中,一切都可以从n确定。所以我们的函数只需要一个参数:

function GetLine(n) {
// ...
}

我们还可以确定一行是偶数还是奇数,以及内部序列有多少个字符:

EvenLine = (n % 2 == 0);
Count = 2*n - 1;

建立两颗恒星之间的序列可以通过一个简单的for循环来完成。

综上所述,我们可以构建以下函数。

function GetLine(n) {
// Create a string for the line that starts with a star
var Line = "*";
// Determine whether the line is even or odd
var EvenLine = (n % 2 == 0);
// Calculate the total number of ones and zeros in the line
var Count = (2 * n - 1);
// We need a variable to store whether the next character should be a one or zero
var One = EvenLine ? true : false; // Even lines start with 1, Odd starts with 0
// Repeat 'Count' times, alternating between ones and zeros
for (var i=0; i<Count; i++)
{
Line += One ? "1" : "0";
One = !One; // Toggle the bool value to alternate on the next iteration
}
// Only add a tail star if we're not on the first line
if (n > 0) Line += "*";
return Line;
}

当我们用一些连续的数字调用GetLine()时,我们可以看到模式主要在那里:

console.log(GetLine(0));
console.log(GetLine(1));
console.log(GetLine(2));
*
*0*
*101*

现在需要做的就是分两步插入空格:

  1. 每个字符之间的空格。
  2. 用于对齐金字塔的前导空间。
function printPyramid(s) {
for (var n=0; n<s; n++) {
// Get the line for n
var line = GetLine(n);
// This one-liner can be used to insert whitespace between each character
// split('') will explode the characters into an array
// join(' ') will turn the array back into a string with ' ' inbetween each element
line = line.split('').join(' ');
// Then we just add the necessary whitespace for alignment
// We need 2 * (s - n - 1) spaces in front of each line.
line = "  ".repeat(s - n - 1) + line;
// Print the line
console.log(line);
}
}

最后

printPyramid(5);

*
* 0 *
* 1 0 1 *
* 0 1 0 1 0 *
* 1 0 1 0 1 0 1 *

const rows = 5;
const columns = 2 * rows - 1;
// create an array filled with " "
const array = Array(rows).fill().map( () => Array(columns).fill(" "));
// if r and c are the row and column indices of the array, then
// in each row place two askerisks where c = rows -1 ± r   (if r is 0, there's only one asterisk)
// and alternating 1's (if c is odd) and 0's (if c is even) for all elements between columns rows - 1 - r and rows - 1 + r
const pyramid = array.map(function(row, r) {
	return row.map(function(v, c) {
		return c === rows - 1 - r || c === rows - 1 + r ? "*" : c > rows - 1 - r && c < rows - 1 + r ? c % 2 === 1 ? "1" : "0" : v;
	});
});
// const pyramid = array.map( (row, r) => row.map( (v, c) => c === rows - 1 - r || c === rows -1 + r ? "*" : c > rows - 1 - r && c < rows - 1 + r ? c % 2 === 1 ? "1" : "0" : v))

function displayPyramid(n)
{
for (let i = 0; i < n; i++)
{
let str = ''
let count = 0
// Spacing before each row
for (let j = 1; j < n - i; j++)
{
str = str + ' '
}
// Row worth of data
const max = 2 * i + 1
str = str + '*'
const innerChars = ['0', '1'];
for (let k = 1; k < max; k++)
{
if (k === max - 1)
str += '*'
else
str += innerChars[(i % 2 + k) % 2];
}
console.log(str);
}
}
displayPyramid(5)

要提出的主要意见:

  • 第一行我们没有内线字符
  • 第二行以内部字符 0 开头
  • 第三行从内部字符 1 开始,然后交替
  • 第四行以内部字符 0 开头,然后交替

我们注意到一种模式,偶数行(如果从零开始,则为奇数(以 0 开头,反之亦然。交替模式应该暗示我们循环的数组。

我不确定您是否需要空格,或者是否要更改树中的字符,因此我将它们添加为参数。

如果你想在数组中有 2 个以上的元素,并且第一个元素在中心,你可以在每一行上移动数组,而不是反转它。

function displayPyramid(n, charArray, withSpace) {
let charArrayOrdered = charArray
for (let i = 0; i < n; i++) {
let str = ''
let count = 0
// Spacing before each row
let withSpaceChar = ''
if(withSpace) {
withSpaceChar=' '
}
for (let j = 1; j < n - i; j++) {
str = str + ' ' + withSpaceChar
}
// Row worth of data
const max = 2 * i
charArrayOrdered = charArrayOrdered.reverse()
let charState = 0
for (let k = 0; k <= max; k++) {
let char = '*'
if(k!=0 && k !=max) {
char=charArrayOrdered[charState]
charState++
} 
if(charState==charArrayOrdered.length){
charState=0
}
if(k!=max && withSpace) {
char = char+' '
}
str = str + char
}
console.log(str);
}
}
displayPyramid(5,[0,1], true)

你可以做这样的事情,但如果在中间是01将取决于你提供的数字。

function build(n) {
let grid = ''
let total = n * 2;
for (let i = 0; i < n - 1; i++) {
let row = [];
let start = total / 2 - i - 1
let end = total / 2 + i
if (!i) grid += ' '.repeat(start + 1) + '*' + 'n'
for (let j = 0; j < total; j++) {
if (j < start) row.push(' ');
else if (j > end) row.push(' ');
else {
if (j == start) row.push('*');
if (j == end) row.push('*');
else row.push(j % 2 == 0 ? 1 : 0)
}
}
grid += row.join('') + 'n'
}
return grid;
}
console.log(build(3))
console.log(build(6))
console.log(build(9))

最新更新