题解 | #圣诞树#
圣诞树
https://www.nowcoder.com/practice/9a03096ed8ab449e9b10b0466de29eb2
思路:找规律,分别处理1、2、3 层
- 代码
package main
import (
"fmt"
)
func main() {
var n int
fmt.Scan(&n)
for i := 1; i <= 3*n+n; i++ {
if i > 3*n {
for j := 1; j <= 3*n-1; j++ {
fmt.Printf("%c", ' ')
}
fmt.Println("*")
} else {
for j := 1; j <= 3*n-i; j++ {
fmt.Printf("%c", ' ')
}
if i%3 == 0 {
for j := 1; j <= i/3; j++ {
fmt.Printf("%s", "* * * ")
}
}
if i%3 == 1 {
for j := 1; j <= i/3+1; j++ {
fmt.Printf("%s", "* ")
}
}
if i%3 == 2 {
for j := 1; j <= i/3+1; j++ {
fmt.Printf("%s", "* * ")
}
}
fmt.Println()
}
}
}