在输入过程中,我错过了什么?

huangapple go评论78阅读模式
英文:

What Did I Miss in Input Process?

问题

我正在解决一个在Hackerearth上的问题。除了一个测试用例之外,我通过了所有的测试,但是显示出了"Time limit exceeded"。我在代码中真正错过了什么?

package main

import (
    "fmt"
    "strings"
)

func rotateRight(numbers []int, size int, k int) []int {
    new_numbers := make([]int, size)
    for index, value := range numbers {
        new_numbers[(index + k) % size] = value
    }

    return new_numbers
}

func main() {
    var test_case, size, k int

    fmt.Scanf("%v", &test_case)
    fmt.Scanln()
    for i := 0; i < test_case; i++ {
        fmt.Scanf("%v %v", &size, &k)
        fmt.Scanln()

        numbers := make([]int, size)
        for i := 0; i<size; i++ {
            fmt.Scanf("%v", &numbers[i])
        }

        result := rotateRight(numbers, size, k)

        fmt.Println(strings.Trim(fmt.Sprint(result), "[]"))
    }
}

我将代码翻译为中文,如下所示:

package main

import (
    "fmt"
    "strings"
)

func rotateRight(numbers []int, size int, k int) []int {
    new_numbers := make([]int, size)
    for index, value := range numbers {
        new_numbers[(index + k) % size] = value
    }

    return new_numbers
}

func main() {
    var test_case, size, k int

    fmt.Scanf("%v", &test_case)
    fmt.Scanln()
    for i := 0; i < test_case; i++ {
        fmt.Scanf("%v %v", &size, &k)
        fmt.Scanln()

        numbers := make([]int, size)
        for i := 0; i<size; i++ {
            fmt.Scanf("%v", &numbers[i])
        }

        result := rotateRight(numbers, size, k)

        fmt.Println(strings.Trim(fmt.Sprint(result), "[]"))
    }
}

请注意,我只是将代码翻译成了中文,没有对代码进行任何修改。

英文:

I am solving a problem in Hackerearth. Passed all the test cases except 1, showing "Time limit exceeded". What did I really miss in my code?

package main

import(
    &quot;fmt&quot;
    &quot;strings&quot;
)

func rotateRight(numbers []int, size int, k int) []int {
    new_numbers := make([]int, size)
    for index, value := range numbers {
        new_numbers[(index + k) % size] = value
    }

    return new_numbers
}

func main() {
    var test_case, size, k int

    fmt.Scanf(&quot;%v&quot;, &amp;test_case)
	fmt.Scanln()
    for i := 0; i &lt; test_case; i++ {
        fmt.Scanf(&quot;%v %v&quot;, &amp;size, &amp;k)
		fmt.Scanln()
        
        numbers := make([]int, size)
        for i := 0; i&lt;size; i++ {
            fmt.Scanf(&quot;%v&quot;, &amp;numbers[i])
        }

        result := rotateRight(numbers, size, k)

        fmt.Println(strings.Trim(fmt.Sprint(result), &quot;[]&quot;))
    }
}

答案1

得分: 1

也许原因是你读取数据的方式,fmt包的速度确实很慢,尝试使用以下代码进行更改:

package main

import (
	"bufio"
	"os"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	sc.Text()//这里是你的数据
}

这个改变将提高时间效率。

英文:

maybe the reason is the way that you read the data, fmt is really slow, try change it with

package main

import (
	&quot;bufio&quot;
	&quot;os&quot;
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	sc.Text()//here you have your data
}

this change will improve the time wasted

huangapple
  • 本文由 发表于 2022年6月7日 15:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/72527584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定