Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

javascript - 如何以单个模式将星星和数字组合在一起组成金字塔(How to do a combined pyramid with stars and numbers together in a single pattern)

Need a pyramid with numbers and stars and there are no examples to review.(需要一个由数字和星星组成的金字塔,没有任何例子可供审查。)

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

Can I get some assistance with this?(我可以得到一些帮助吗?)

This is what I have, but I can't figure out how to get the the numbers in there..(这就是我所拥有的,但是我不知道如何获得那里的数字。)

 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) 

  ask by misterhtmlcss translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's first try to write a function for just one line of the pyramid, any line.(首先,让我们尝试为金字塔的仅一行,任意行编写一个函数。)

We'll ignore the whitespace for now too.(我们现在也将忽略空格。)

Every line of the pyramid seems to follow a few simple rules:(金字塔的每一行似乎都遵循一些简单的规则:)

  • All lines start with a * .(所有行均以*开头。)
  • The 'inner sequence' is just alternating 1 and 0 a certain number of times.(“内部序列”只是交替10一定次数。)
  • There are 2*n - 1 digits in the inner sequence of each line, except the first.(除第一行外,每行的内部序列中都有2*n - 1位数字。)
  • Even lines (0, 2, 4, ...) begin the inner sequence with a 1 .(偶数行(0,2,4,...)以1开头内部序列。)
  • Odd lines (1, 3, 5, ...) begin the inner sequence with a 0 .(奇数行(1、3、5,...)以0开始于内部序列。)
  • All lines end with an additional * , except the first.(除第一行外,所有行均以附加*结束。)

In the rules above, everything can be determined from n .(在上述规则中,一切都可以从n确定。)

So our function only needs one argument:(所以我们的函数只需要一个参数:)
function GetLine(n) {
   // ...
}

We can also determine whether a line is even or odd, and how many characters the inner sequence has:(我们还可以确定一行是偶数还是奇数,以及内部序列有多少个字符:)

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

Building the sequence between the two stars can be done with a simple for loop.(可以使用简单的for循环在两颗星之间建立序列。)

Putting that all together, we can build the following function.(综上所述,我们可以构建以下功能。)

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;
}

When we call GetLine() with some consecutive numbers we can see that the pattern is mostly there:(当我们使用一些连续的数字调用GetLine() ,我们可以看到该模式主要存在:)

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

*
*0*
*101*

Now all that needs to be done is insert the whitespace in two steps:(现在,需要做的就是分两步插入空白:)

  1. A space between each character.(每个字符之间的空格。)
  2. The leading space to align the pyramid.(对齐金字塔的引导空间。)
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);
    }
}

Finally,(最后,)

printPyramid(5);


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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...