我怎样才能制作一个数字金字塔循环,但相反

当我尝试用 golang 逆向制作数字金字塔时遇到问题


我已经可以用这段代码制作一个数字金字塔:


var (

        input, bil int

    )


    fmt.Scanln(&input)

    bil = 9

    for b := 1; b <= input; b++ {

        for c := input; c >= b; c-- { //spasi

            fmt.Print(" ")

        }

        for d := 1; d <= b; d++ { //bintang


            fmt.Print(bil)


            if bil == -1 {

                bil = 9

            }

            bil = bil - 1

        }

        fmt.Println()


    }

输入 :


5

输出 :


     9

    87

   654

  3210

 98765

我怎样做一个像这样的反向的


输入:


5

输出


    9

   78

  456

 0123

56789


德玛西亚99
浏览 68回答 1
1回答

吃鸡游戏

只需更改计算当前数字的方式即可 ( fmt.Print(bil-d))func main() {&nbsp; &nbsp; var (&nbsp; &nbsp; &nbsp; &nbsp; input, bil int&nbsp; &nbsp; )&nbsp; &nbsp; fmt.Scanln(&input)&nbsp; &nbsp; bil = 9&nbsp; &nbsp; for b := 1; b <= input; b++ {&nbsp; &nbsp; &nbsp; &nbsp; for c := input; c >= b; c-- {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(" ")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for d := b - 1; d >= 0; d-- {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v := bil - d&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v < 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = v%10 + 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(v)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; bil -= b&nbsp; &nbsp; &nbsp; &nbsp; if bil < 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bil = bil%10 + 10&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; }}请注意,我还更改了边界条件的处理。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go