0
0

More than 3 years have passed since last update.

備忘録 Golang paiza見本問題 Dランク 一番小さな値

Posted at

備忘録 go言語 paiza見本問題解説

見本問題は解説、公開しても問題ないようですので、備忘録を兼ねて公開します。
Dランク "一番小さい値"

package main
import ("fmt") 

type El struct{//構造体の定義。elemは要素, nextには次のデータのアドレスを格納する
    elem int
    next *El
}
type Data struct{
    x, y, z, v, w El//データの名前を定義
}

func f1(a *Data) int{
    var dum int = a.x.elem//一番初めの値のコピー
    var b *El = &a.x//ポインタでないa.xにアドレスを代入することはできないため、ダミーの作成
    for i := 0 ; i < 5; i++{//データは5つであるため5回のループ
        if (*b).elem < dum{//データが最小の場合のケース
            dum = b.elem
        }
       b = (*b).next//bのアドレスに次のアドレスを代入する。
    } 
    return dum//最小値を返却
}

func main(){
    var a = Data{}//aの仮宣言
    a.x.next = &a.y//順次ネクストに次のデータのアドレスを代入する
    a.y.next = &a.z
    a.z.next = &a.v
    a.v.next = &a.w
    a.w.next = nil//終端ノード
    fmt.Scanf("%d\n%d\n%d\n%d\n%d", &a.x.elem, &a.y.elem, &a.z.elem, &a.v.elem, &a.w.elem)
    fmt.Print(f1(&a)


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