英文:
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 (
"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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论