Answer the question
In order to leave comments, you need to log in
How to complete a task in Yandex.Contest?
When solving a training task, I encountered a Presentation error.
The testing system cannot check the output data, because their format does not match the one described in the task conditions.
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
input, err := os.Open("input.txt")
if err != nil {
fmt.Println(err)
return
}
defer input.Close()
file, err := os.Create("output.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
output, err := os.OpenFile("output.txt", os.O_APPEND|os.O_WRONLY, 0777)
if err != nil {
fmt.Println(err)
return
}
defer output.Close()
data := make([]byte, 64)
for {
n, err := input.Read(data)
if err == io.EOF {
break
}
arr := strings.Split(string(data[:n]), " ")
a, err := strconv.Atoi(arr[0])
if err != nil {
fmt.Println(err)
return
}
b, err := strconv.Atoi(arr[1])
if err != nil {
fmt.Println(err)
return
}
if _, err = output.WriteString(strconv.Itoa(a + b)); err != nil {
fmt.Println(err)
return
}
_, err = os.Open("output.txt")
if err != nil {
fmt.Println(err)
return
}
}
}
Answer the question
In order to leave comments, you need to log in
Your code will break when it sees an input file that ends with a line break.
When it breaks, it will write an error to STDOUT, which will most likely be taken by the verification system as a result output. The result, of course, will not be in that format.
I racked my brains over this problem, in the end it turned out that I needed to remove newline char at the end of the data variable . That is, replace witharr := strings.Split(string(data[:n]), " ")
arr := strings.Split(strings.TrimSuffix(string(data[:n]), "\n"), " ")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question