猿问

Golang中的hackerrank阶梯挑战问题

我想在 GO 的 hackerrank 中解决这个挑战。当我运行它时,我得到了挑战想要的相同结果,但他们不接受我的回答。这是挑战链接: https ://www.hackerrank.com/challenges/staircase/problem?isFullScreen=true

这是我的代码:

func staircase(n int32) {

    var i int32

    for i = 0; i < n; i++ {

        fmt.Println(strings.Repeat(" ", int(n-i)), strings.Repeat("#", int(i)))


    }


}


MM们
浏览 126回答 1
1回答

尚方宝剑之说

首先,第一行必须有一个#符号,最后一行必须有n&nbsp;#符号。因此,将循环更改为从1到n&nbsp;inclusive。接下来,fmt.Println()在参数之间打印一个空格,这将“扭曲”输出。连接 2 个字符串,或者使用fmt.Print()which 不在字符串参数之间添加空格,或者使用fmt.Printf("%s%s\n", text1, text2).例如:func staircase(n int32) {&nbsp; &nbsp; for i := int32(1); i <= n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(strings.Repeat(" ", int(n-i)) + strings.Repeat("#", int(i)))&nbsp; &nbsp; }}使用 对其进行测试,输出将是(在Go Playgroundstaircase(4)上尝试):&nbsp;&nbsp;&nbsp;# &nbsp;&nbsp;## &nbsp;### ####
随时随地看视频慕课网APP

相关分类

Go
我要回答