从标准输入读取n个整数/浮点数/字符串。

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

Read n integers / float / string from standard input

问题

算法竞赛中的问题通常会提供多行输入,其中第一行指定输入的数量。例如:

3
78
42
99

第一行告诉我们后面会有3个整数。

目前,我有以下代码来读取它们:

package main

import "fmt"

func main() {
    var num []int
    var input int
    var count int

    fmt.Scanf("%d", &count)

    for {
       if count == 0 {
           break
       }
       
       fmt.Scanf("%d", &input)
       num = append(num, input)

       count--
    }
}

有没有更好的方法来实现这个功能?上述方法感觉有些笨拙。

英文:

Algorithm competition have questions that provide the input in multiple lines, with the first line specifying the count of the inputs. Example -

3
78
42
99

The first line tells that there will be 3 integers followed by the three integers.

Currently, I have the following code to read them -

package main

import "fmt"

func main() {
    var num []int
    var input int
    var count int

    fmt.Scanf("%d", &count)

    for {
       if (count == 0) {
           break
       }
       
       fmt.Scanf("%d", &input)
       num = append(num, input)

       count--
    }
}

Is there a better way to carry this out? The above approach feels clumsy for some reason.

答案1

得分: 2

这段代码将所有内容都放入了循环头部,并将input放在了尽可能局部的作用域中。你应该检查Scanf返回的错误:

package main

import "fmt"

func main() {
    var num []int
    var count int
    var err error

    for _, err = fmt.Scanf("%d\n", &count); err == nil && count > 0; count-- {
        var input int
        _, err = fmt.Scanf("%d\n", &input)
        num = append(num, input)
    }

    if err != nil {
        panic(err)
    }
}

有很多种编写等效代码的方式,我认为这种方式最好。可以提出将错误检查放在append之前的循环中的论点,但是由于遇到错误可能会使列表无效,我认为这种方式看起来更漂亮。

英文:

This code pushes everything into the loop header, as well as puts input into the most local scope possible. You should be checking the error returned by Scanf too:

package main

import "fmt"

func main() {
    var num []int
    var count int
    var err error

   for _, err = fmt.Scanf("%d\n", &count); err == nil && count > 0; count-- {
       var input int
       _, err = fmt.Scanf("%d\n", &input)
       num = append(num, input)
   }

   if err != nil {
       panic(err)
   }

}

There are about a million ways to write equivalent code, this seemed the best to me. An argument could be made for putting the error check in the loop before the append, but since encountering an error presumably invalidates the list, I thought it looked prettier this way.

答案2

得分: 0

package main

import (
	"bufio"
	"os"
	"fmt"
)

func main() {

	reader := bufio.NewReader(os.Stdin)

	a := read(reader, 100000)

	fmt.Println(a)
}

func read(reader *bufio.Reader, n int) ([]uint32) {

	a := make([]uint32, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}

	return a
}
package main

import (
	"bufio"
	"os"
	"fmt"
)

func main() {

	reader := bufio.NewReader(os.Stdin)

	a := read(reader, 100000)

	fmt.Println(a)
}

func read(reader *bufio.Reader, n int) ([]uint32) {

	a := make([]uint32, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}

	return a
}

这是一个Go语言的代码示例,它包含了一个main函数和一个read函数。main函数从标准输入读取数据,并调用read函数进行处理,最后将结果打印出来。read函数接收一个bufio.Reader和一个整数n作为参数,它会读取n个无符号整数,并将它们存储在一个切片中,最后返回该切片。

英文:
package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)

	a:= read(reader,100000)

	fmt.Println(a)
}

func read (reader *bufio.Reader, n int)([]uint32) {

	a := make([]uint32, n)
	for i:=0; i&lt;n; i++ {
		fmt.Fscan(reader, &amp;a[i])
	}

	return a
}

huangapple
  • 本文由 发表于 2015年1月5日 15:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/27775337.html
匿名

发表评论

匿名网友

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

确定