方式一
package main
import "fmt"
func main() {
// Iterate over the rows (1 to 9)
for row := 1; row <= 9; row++ {
// Iterate over the columns (1 to row)
for col := 1; col <= row; col++ {
// Calculate the product
product := row * col
// Print the product
fmt.Printf("%d x %d = %d\t", row, col, product)
}
// Print a newline after each row
fmt.Println()
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
方式二
package main
import "fmt"
func printTable(row, col int) {
if row > 9 || col > row {
return // Base case: exit when row or col exceeds limits
}
product := row * col
fmt.Printf("%d x %d = %d\t", row, col, product)
// Print next column in the same row
printTable(row, col+1)
// If at the end of the row, move to the next row with col 1
if col == row {
fmt.Println()
printTable(row+1, 1)
}
}
func main() {
printTable(1, 1)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.