样式:
代码:
package main
import (
"fmt"
_"math"
)
func main() {
layers := 5 //金字塔层数
fmt.Println("半个金字塔:")
//金字塔层数
for i := 1; i <= layers; i++ {
//每列显示的*数量
for x := 1; x <= i; x++ {
fmt.Printf("*")
}
fmt.Println()
}
fmt.Printf("\n\n半个金字塔:\n")
//金字塔层数
for i := 1; i <= layers; i++ {
tmp := i * 2 - 1
//每列显示的*数量
for x := 1; x <= tmp; x++ {
fmt.Printf("*")
}
fmt.Println()
}
fmt.Printf("\n\n实心金字塔:\n")
//金字塔层数
for i := 1; i <= layers; i++ {
//空格规律:总层数 - 当前层数
space := layers - i
//打印空格
for j := 1; j <= space; j++ {
fmt.Printf(" ")
}
tmp := i * 2 - 1
//每列显示的*数量
for x := 1; x <= tmp; x++ {
fmt.Printf("*")
}
fmt.Println()
}
fmt.Printf("\n\n空心金字塔:\n")
//金字塔层数
for i := 1; i <= layers; i++ {
//空格规律:总层数 - 当前层数
space := layers - i
//打印空格
for j := 1; j <= space; j++ {
fmt.Printf(" ")
}
tmp := i * 2 - 1
//每列显示的*数量
for x := 1; x <= tmp; x++ {
if x == 1 || x == tmp || i == layers {
fmt.Printf("*")
} else {
fmt.Printf(" ")
}
}
fmt.Println()
}
fmt.Printf("\n\n实心菱形:\n")
for i := 1; i <= layers; i++ {
//打印空格
for k := 1; k <= layers-i; k++ {
fmt.Printf(" ")
}
for x := 1; x <= 2*i-1; x++ {
fmt.Printf("*")
}
fmt.Println()
}
for i := layers-1; i > 0; i-- {
for k := 1; k <= layers-i; k++ {
fmt.Printf(" ")
}
for x := i*2-1; x > 0; x-- {
fmt.Printf("*")
}
fmt.Println()
}
fmt.Printf("\n\n空心菱形:\n")
for i := 1; i <= layers; i++ {
for k := 1; k <= layers-i; k++ {
fmt.Printf(" ")
}
for x := 1; x <= 2*i-1; x++ {
if x == 1 || x == 2*i-1 {
fmt.Printf("*")
} else {
fmt.Printf(" ")
}
}
fmt.Println()
}
for i := layers-1; i > 0; i-- {
for k := 1; k <= layers-i; k++ {
fmt.Printf(" ")
}
for x := 2*i-1; x > 0; x-- {
if x == 1 || x == 2*i-1 {
fmt.Printf("*")
} else {
fmt.Printf(" ")
}
}
fmt.Println()
}
}
下一篇:最后一页