LoginSignup
0
0

More than 3 years have passed since last update.

Paiza Golang サンプルコード

Posted at
package main
import (
        "bufio"
        "fmt"
        "os"
        "strconv"
        "strings"
)

func getInput() [][]string {

    var input [][]string
    scanner := bufio.NewScanner(os.Stdin)

    for scanner.Scan() {
        text := scanner.Text()
        words := strings.Split(text, " ")
        input = append(input, words)
    }

    return input
}

// 入力値が全て数値の場合
func toInt2dSlice(input [][]string) [][]int {
    var intSlice [][]int
    for _, strings := range input {
        var numbers []int
        for _, s := range strings{
            var number, _ = strconv.Atoi(s)
            numbers = append(numbers, number)
        }
        intSlice = append(intSlice, numbers)
    }
    return intSlice
}

func main() {
    input := getInput()

    /* 入力値が全て数値の場合は [][]string->[][]intするとラク
        input := toInt2dSlice(getInput())
    */

    fmt.Println(input)
    fmt.Println(input[0][0])
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0