# Happy gathering school recruit one side programming problem - print isosceles triangle

• print(line) can print isosceles triangles. It's a very simple topic, but at that time, i don't know whether it's because of tension or something. The number of asterisks is 2 * (line) + 1, which is analyzed and realized, but i'm a little confused about the number of spaces. There is one space in the front and one space in the back (a little (▔). First count the number of spaces on one side, line - 1 - i, i starts from 0

print(3) prints isosceles triangles composed of asterisks

```/*
print(3);
*
***
*****
*/
function print(line) {
let res1 = [];
for (let row = 0; row < line; row ++) {
if (!res1[row]) res1[row] = "";
for (let j = row; j < line - 1; j ++) {
res1[row] += " ";
}
for (let k = 0; k < row * 2 + 1; k ++) {
res1[row] += "*";
}
for (let j = row; j < line - 1; j ++) {
res1[row] += " ";
}
}
return res1.join("\n");
}
print(3);
```
• What is the optimization method? Because the time complexity of this method is O(n^2) and the space complexity is O(n), I can't think of it for the time being. However, since I encounter a problem and adhere to the starting point of solving a class of problems, I can classify it as printing stars (inverted triangle, positive triangle, isosceles triangle, right angle triangle, diamond and hollow diamond)

# Print stars

## square

• Print Square: add an asterisk to each line
```	function printSquare(line) {
let res = [];
for (let i = 0; i < line; i ++) {
if (!res[i]) res[i] = "";
for (let j = 0; j < line; j ++) {
res[i] += "*";
}
}
return res.join("\n");
}
console.log(printSquare(3));
```

## Equilateral triangle

```/*
Number of stars: row + 1, row = 0,1,2,3
Number of spaces: line - row - 1, row = 0,1,2,3
But each star is followed by a space
*
* *
* * *
* * * *
*/
function print(line) {
let res1 = [];
for (let row = 0; row < line; row ++) {
if (!res1[row]) res1[row] = "";
for (let j = row; j < line - 1; j ++) {
res1[row] += " ";
}
for (let k = 0; k < row + 1; k ++) {
res1[row] += "* ";
}
}
return res1.join("\n");
}
```

## diamond

• Two isosceles or equilateral triangles, one of which is an inverted triangle
• Analyze the relationship between the number of stars in each row of isosceles triangle and the space
```  		*             * * * * *
* * *             * * *
* * * * *             *
```
• The implementation code is as follows
```	// diamond
function print(line) {

let res1 = [];
// Number of stars per row: 2 * row + 1, row = 0,1,2
// Number of spaces per line: (line - 1) - row, row = 0,1,2
for (let row = 0; row < line; row ++) {
if (!res1[row]) res1[row] = "";
for (let j = row; j < line - 1; j ++) {
res1[row] += " ";
}
for (let k = 0; k < 2 * row + 1; k ++) {
res1[row] += "*";
}
}

// Number of stars per row: 2 * (line - row - 1) + 1, row = 0,1,2
// Number of spaces per line: row, row = 0,1,2
let res2 = [];
for (let row = 1; row < line; row ++) {
if (!res2[row - 1]) res2[row - 1] = "";
for (let j = 0; j < row; j ++) {
res2[row - 1] += " ";
}
for (let k = 0; k < 2 * (line - row - 1) + 1; k ++) {
res2[row - 1] += "*";
}
}

// console.log(res1,res2);
return res1.join("\n") + '\n' + res2.join("\n");
}
```
• Realization of equilateral triangle and inverse equilateral triangle
```	function print(line) {
/*
Equilateral triangle
Number of stars: row + 1, row = 0,1,2,3
Number of spaces: line - row - 1, row = 0,1,2,3
But each star is followed by a space
*/
let res1 = [];
for (let row = 0; row < line; row ++) {
if (!res1[row]) res1[row] = "";
for (let j = row; j < line - 1; j ++) {
res1[row] += " ";
}
for (let k = 0; k < row + 1; k ++) {
res1[row] += "* ";
}
}

/*
Inverted equilateral triangle
Number of stars: line - row, row = 0,1,2,3
Number of spaces: row, row = 0,1,2,3
Each star is followed by a space
*/
let res2 = [];
for (let row = 1; row < line; row ++) {
if (!res2[row - 1]) res2[row - 1] = "";
for (let j = 0; j < row; j ++) {
res2[row - 1] += " ";
}
for (let k = row; k < line; k ++) {
res2[row - 1] += "* ";
}
}

return res1.join("\n") + "\n" + res2.join("\n");
}

```

# summary

• In addition, after this interview, I found that I have a bad habit of doing programming problems. When I encounter problems, don't rush to write the code directly, or I'm afraid I can't write it. Instead, I analyze the problems first, clarify my own implementation ideas, and then use the code to implement them
• In fact, this topic is to count the relationship between each planet character and space character, so as to form a geometric figure (refer to the diamond implementation above)

Posted on Sat, 18 Sep 2021 12:15:27 -0400 by stormszero