您的位置:首页 > 教育 > 培训 > 1. Go 九九乘法表

1. Go 九九乘法表

2024/10/5 15:25:01 来源:https://blog.csdn.net/qq_15603633/article/details/140293601  浏览:    关键词:1. Go 九九乘法表

方式一

package mainimport "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 productproduct := row * col// Print the productfmt.Printf("%d x %d = %d\t", row, col, product)}// Print a newline after each rowfmt.Println()}
}

方式二

package mainimport "fmt"func printTable(row, col int) {if row > 9 || col > row {return // Base case: exit when row or col exceeds limits}product := row * colfmt.Printf("%d x %d = %d\t", row, col, product)// Print next column in the same rowprintTable(row, col+1)// If at the end of the row, move to the next row with col 1if col == row {fmt.Println()printTable(row+1, 1)}
}func main() {printTable(1, 1)
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com