備忘録 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)