drmcm84800 2019-09-01 08:12
浏览 25

我怎样才能使金字塔形的数字反过来

I encounter a problem when try to make a pyramid of number in reverse of golang

I already can make a pyramid of number with this code :

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()

    }

Input :

5

Output :

     9
    87
   654
  3210
 98765

How do I make a reverse one like this

input:

5

Output

    9
   78
  456
 0123
56789

  • 写回答

1条回答 默认 最新

  • dozug64282 2019-09-01 08:49
    关注

    Just change the way you calculate the current number (fmt.Print(bil-d))

    func main() {
        var (
            input, bil int
        )
    
        fmt.Scanln(&input)
        bil = 9
        for b := 1; b <= input; b++ {
            for c := input; c >= b; c-- {
                fmt.Print(" ")
            }
    
            for d := b - 1; d >= 0; d-- {
                v := bil - d
                if v < 0 {
                    v = v%10 + 10
                }
                fmt.Print(v)
            }
            bil -= b
            if bil < 0 {
                bil = bil%10 + 10
            }
            fmt.Println()
    
        }
    }
    

    Note that I also change the handling of boundary conditions.

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题