title: Golang 不同格式輸出九九乘法表
date: 2022-05-18 14:33:00
toc: false
index_img: https://b3logfile.com/file/2022/05/image-bc047597.png
category:
- Go
tags: - Go
- go
- Golang
- golang
- 格式
- 輸出
package main
import (
"fmt"
"strings"
)
func style0() {
m := make(map[int][]string, 9)
for i := 1; i < 10; i++ {
for j := i; j < 10; j++ {
str := fmt.Sprintf("%d * %d = %2d\t", i, j, i*j)
s, ok := m[i]
if !ok {
m[i] = []string{str}
} else {
m[i] = append(s, str)
}
}
}
for i := 0; i < 9; i++ {
for j := 1; j < 10-i; j++ {
fmt.Print(m[j][i])
}
fmt.Println()
}
}
func style1() {
m := make(map[int][]string, 9)
for i := 1; i < 10; i++ {
for j := i; j < 10; j++ {
str := fmt.Sprintf("%d * %d = %2d\t", i, j, i*j)
s, ok := m[i]
if !ok {
m[i] = []string{str}
} else {
m[i] = append(s, str)
}
}
}
for i := 0; i < 9; i++ {
var placeholder []string
for k := 0; k < i; k++ {
placeholder = append(placeholder, fmt.Sprint(" \t"))
}
fmt.Print(strings.Join(placeholder, ""))
for j := 9 - i; j > 0; j-- {
fmt.Print(m[j][i])
}
fmt.Println()
}
}
func style2() {
for i := 1; i < 10; i++ {
for j := i; j < 10; j++ {
str := fmt.Sprintf("%d * %d = %2d\t", i, j, i*j)
fmt.Print(str)
}
fmt.Println()
}
}
func style3() {
for i := 1; i < 10; i++ {
var placeholder []string
for k := 1; k < i; k++ {
placeholder = append(placeholder, fmt.Sprint(" \t"))
}
fmt.Print(strings.Join(placeholder, ""))
for j := 9; j >= i; j-- {
str := fmt.Sprintf("%d * %d = %2d\t", i, j, i*j)
fmt.Print(str)
}
fmt.Println()
}
}
// 九九乘法表,4種輸出格式
func main() {
style0()
style1()
style2()
style3()
}
運行結果:
1 * 1 = 1 2 * 2 = 4 3 * 3 = 9 4 * 4 = 16 5 * 5 = 25 6 * 6 = 36 7 * 7 = 49 8 * 8 = 64 9 * 9 = 81
1 * 2 = 2 2 * 3 = 6 3 * 4 = 12 4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