duandongji2231 2015-07-10 05:49
浏览 43
已采纳

更快的输入扫描

I am attempting to solve the SPOJ question that can be found here

Following is my solution:

package main

import "fmt"
import "bufio"
import "os"

func main() {
    var n, k int
    var num int
    var divisible int

    in := bufio.NewReader(os.Stdin)

    fmt.Fscan(in, &n)
    fmt.Fscan(in, &k)

    for n > 0 {
        fmt.Fscan(in, &num)

        if num%k == 0 {
            divisible++
        }

        n--
    }

    fmt.Println(divisible)
}

The code works fine. The issue here is that I get a timeout when I execute it in SPOJ.

I was first using only fmt.Scan but I then came across this thread that suggested that I use bufio instead for faster input scanning.

But I still get a timeout issue. I am only looping to get all the inputs and within this loop itself I determine whether the input is divisible or not. So, I believe that its not the loop but the input scanning that's taking time. How can I improve this to read the input faster? Or is the issue somewhere else?

  • 写回答

2条回答 默认 最新

  • duanque3125 2015-07-10 07:04
    关注

    You can use bufio.Scanner to read lines from the input.

    And since we're always reading numbers, we can create a highly optimized converter to get the number. We should avoid using Scanner.Text() which creates a string as we can obtain the number just from the raw bytes returned by Scanner.Bytes(). Scanner.Text() returns the same token as Scanner.Bytes() but it first converts to string which is obviously slower and generates "garbage" and work for the gc.

    So here is a converter function which obtains an int from the raw bytes:

    func toInt(buf []byte) (n int) {
        for _, v := range buf {
            n = n*10 + int(v-'0')
        }
        return
    }
    

    This toInt() works because the []byte contains the UTF-8 encoded byte sequence of the string representation of the decimal format of the number, which contains only digits in the range of '0'..'9' whose UTF-8 encoded bytes are mapped one-to-one (one byte is used for one digit). The mapping from digit to byte is simply a shift: '0' -> 48, '1' -> 49 etc.

    Using this your complete application:

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
    )
    
    func main() {
        var n, k, c int
        scanner := bufio.NewScanner(os.Stdin)
    
        scanner.Scan()
        fmt.Sscanf(scanner.Text(), "%d %d", &n, &k)
    
        for ;n > 0; n-- {
            scanner.Scan()
            if toInt(scanner.Bytes())%k == 0 {
                c++
            }
        }
    
        fmt.Println(c)
    }
    
    func toInt(buf []byte) (n int) {
        for _, v := range buf {
            n = n*10 + int(v-'0')
        }
        return
    }
    

    This solution is about 4 times faster than calling strconv.Atoi() for example.

    Notes:

    In the above solution I assumed input is valid, that is it always contains valid numbers and contains at least n lines after the first (which gives us n and k).

    If the input is closed after n+1 lines, we can use a simplified for (and we don't even need to decrement and rely on n):

    for scanner.Scan() {
        if toInt(scanner.Bytes())%k == 0 {
            c++
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起