「AtCoder に登録したら次にやること」をGO言語でやってみた

#はじめに
けんちょんさんのガイドをなぞっていると言語習得時の初習にAtCoder便利じゃないか？と思ったのでやってみた。
javaでここら辺の問題は一通り解いているので問題で躓くことはなく、書き方を勉強している感じです。

#参考にした記事

とても参考になる記事ばかりなので、何度も読み返す。
[けんちょんさんのガイド](https://qiita.com/drken/items/fd4e5e3630d0f5859067)
[はじめてのGo言語](http://cuto.unirita.co.jp/gostudy/)
[Go 言語で標準入力から読み込む競技プログラミングのアレ](https://qiita.com/tnoda_/items/b503a72eac82862d30c6)

#補足
 - mainXXX086() -> XXX086() 見たいに呼び出しているのはVSCODEでテストケースを作るとき用です。
    - inputをmainmainXXX086()で行い、入力値を引数としてXXX086()に渡して、戻り値を出力しています。

 - [テスト]はVSCODEの`[Generate Unit tests For Function]`で作成できるやつ

#第1問: [ABC086A - Product](https://atcoder.jp/contests/abc086/tasks/abc086_a)

 - fmtパッケージにprintとかscanfがある

```golang
func mainABC086A() {

	var a, b int
	fmt.Scan(&a, &b)
	var res = abc086A(a, b)
	fmt.Println(res)
}

func abc086A(a, b int) string {
	var res string
	if a*b%2 == 0 {
		res = "Even"
	} else {
		res = "Odd"
	}
	return res
}
```

 - テスト

```golang

func Test_abc086A(t *testing.T) {
	type args struct {
		a int
		b int
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{name: "1", args: args{3, 4}, want: "Even"},
		{name: "2", args: args{1, 21}, want: "Odd"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc086A(tt.args.a, tt.args.b); got != tt.want {
				t.Errorf("abc086A() = %v, want %v", got, tt.want)
			}
		})
	}
}
```

#第2問: [ABC081A - Placing Marbles](https://atcoder.jp/contests/abc081/tasks/abc081_a)

##文字列操作

 - 参考サイト
    - [はじめてのGo言語：文字列処理](http://cuto.unirita.co.jp/gostudy/post/standard-library-string/)

 文字列操作、、、慣れないわこの形式。。。

```golang
func mainABC081A() {
	var strS string
	fmt.Scan(&strS)

	var res = abc081A(strS)
	fmt.Println(res)
}

func abc081A(strS string) int {
	var res int
	for i := 0; i < len(strS); i++ {
		if string([]rune(strS)[i:i+1]) == "1" {
			res++
		}
	}
	return res
}

```

 - テスト

```golang

func Test_abc081A(t *testing.T) {
	type args struct {
		strS string
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{"101"}, want: 2},
		{name: "2", args: args{"000"}, want: 0},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc081A(tt.args.strS); got != tt.want {
				t.Errorf("abc081A() = %v, want %v", got, tt.want)
			}
		})
	}
}
```

#第3問: [ABC081B - Shift Only](https://atcoder.jp/contests/abc081/tasks/abc081_b)

##whileの代わりのforの書き方

```golang
func mainABC081B() {
	var n int
	fmt.Scan(&n)

	var wk []int

	for i := 0; i < n; i++ {
		var in int
		fmt.Scan(&in)
		wk = append(wk, in)
	}

	var res = abc081B(n, wk)
	fmt.Println(res)

}

func abc081B(n int, wk []int) int {

	var condition = true
	var res = 0
	for condition {
		for i := 0; i < len(wk); i++ {
			if wk[i]%2 == 0 {
				wk[i] /= 2
			} else {
				condition = false
				break
			}
		}
		if condition {
			res++
		}
	}
	return res
}
```

 - テスト

```golang

func Test_abc081B(t *testing.T) {
	type args struct {
		n  int
		wk []int
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{3, []int{8, 12, 40}}, want: 2},
		{name: "2", args: args{4, []int{5, 6, 8, 10}}, want: 0},
		{name: "3", args: args{6, []int{382253568, 723152896, 37802240, 379425024, 404894720, 471526144}}, want: 8},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc081B(tt.args.n, tt.args.wk); got != tt.want {
				t.Errorf("abc081B() = %v, want %v", got, tt.want)
			}
		})
	}
}
```

#第4問: [ABC087B - Coins](https://atcoder.jp/contests/abc087/tasks/abc087_b)

```golang
func mainABC087B() {
	var a, b, c, x int

	fmt.Scan(&a, &b, &c, &x)

	var res = abc087B(a, b, c, x)
	fmt.Println(res)
}

func abc087B(a, b, c, x int) int {
	var res = 0
	for hund5 := 0; hund5 <= a; hund5++ {
		for hund1 := 0; hund1 <= b; hund1++ {
			for five := 0; five <= c; five++ {
				if hund5*500+hund1*100+five*50 == x {
					res++
				}
			}

		}
	}
	return res
}
```

 - テスト

```golang

func Test_abc087B(t *testing.T) {
	type args struct {
		a int
		b int
		c int
		x int
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{2, 2, 2, 100}, want: 2},
		{name: "2", args: args{5, 1, 0, 150}, want: 0},
		{name: "3", args: args{30, 40, 50, 6000}, want: 213},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc087B(tt.args.a, tt.args.b, tt.args.c, tt.args.x); got != tt.want {
				t.Errorf("abc087B() = %v, want %v", got, tt.want)
			}
		})
	}
}

```

#第5問 ABC083B [Some Sums](https://atcoder.jp/contests/abc083/tasks/abc083_b)

```golang
func mainABC083B() {
	var n, a, b int
	fmt.Scan(&n, &a, &b)
	var res = abc083B(n, a, b)
	fmt.Println(res)
}

func abc083B(n, a, b int) int {

	var res = 0
	for i := 1; i <= n; i++ {

		var wk = i
		var total = 0
		for wk > 0 {
			total += wk % 10
			wk /= 10
		}
		if total >= a && total <= b {
			res += i
		}

	}
	return res
}
```

 - テスト

```golang

func Test_abc083B(t *testing.T) {
	type args struct {
		n int
		a int
		b int
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{20, 2, 5}, want: 84},
		{name: "2", args: args{10, 1, 2}, want: 13},
		{name: "3", args: args{100, 4, 16}, want: 4554},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc083B(tt.args.n, tt.args.a, tt.args.b); got != tt.want {
				t.Errorf("abc083B() = %v, want %v", got, tt.want)
			}
		})
	}
}

```

#第6問 ABC088B - [Card Game for Two](https://atcoder.jp/contests/abc088/tasks/abc088_b)

##配列のソート

 - sort.Sort()にReverseを渡す
 - sort.Reverse()にIntSliceを渡す

```golang
func mainABC088B() {
	var n int
	fmt.Scan(&n)
	var wk []int
	for i := 0; i < n; i++ {
		var in int
		fmt.Scan(&in)
		wk = append(wk, in)
	}
	var res = abc088B(n, wk)
	fmt.Println(res)
}

func abc088B(n int, wk []int) int {
	var res = 0

	/*
		sort.Sort()にReverseを渡す
		sort.Reverse()にIntSliceを渡す
	*/
	sort.Sort(sort.Reverse(sort.IntSlice(wk)))

	var alice = 0
	var bob = 0
	for i := 0; i < len(wk); i++ {
		if i%2 == 0 {
			alice += wk[i]
		} else {
			bob += wk[i]
		}
	}

	res = alice - bob
	return res
}
```

 - テスト

```golang

func Test_abc088B(t *testing.T) {
	type args struct {
		n  int
		wk []int
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{2, []int{3, 1}}, want: 2},
		{name: "2", args: args{3, []int{2, 7, 4}}, want: 5},
		{name: "3", args: args{4, []int{20, 18, 2, 18}}, want: 18},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc088B(tt.args.n, tt.args.wk); got != tt.want {
				t.Errorf("abc088B() = %v, want %v", got, tt.want)
			}
		})
	}
}

```

#第7問 [ABC085B - Kagami Mochi](https://atcoder.jp/contests/abc085/tasks/abc085_b)

##配列のソート

 - 逆順のソート
    - `sort.Sort(sort.Reverse(sort.IntSlice(wk)))`

```golang
func mainABC085B() {

	var n int
	fmt.Scan(&n)

	var wk []int
	for i := 0; i < n; i++ {
		var in int
		fmt.Scan(&in)
		wk = append(wk, in)
	}
	var res = abc085B(n, wk)
	fmt.Println(res)
}

func abc085B(n int, wk []int) int {

	var res = 0

	sort.Sort(sort.Reverse(sort.IntSlice(wk)))
	var tmp = wk[0]
	res++
	for i := 0; i < n; i++ {
		if wk[i] != tmp {
			tmp = wk[i]
			res++
		}
	}

	return res
}
```

 - テスト

```golang

func Test_abc085B(t *testing.T) {
	type args struct {
		n  int
		wk []int
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{name: "1", args: args{4, []int{10, 8, 8, 6}}, want: 3},
		{name: "2", args: args{3, []int{15, 15, 15}}, want: 1},
		{name: "3", args: args{7, []int{50, 30, 50, 100, 50, 80, 30}}, want: 4},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc085B(tt.args.n, tt.args.wk); got != tt.want {
				t.Errorf("abc085B() = %v, want %v", got, tt.want)
			}
		})
	}
}

```



#第8問 [ABC085C - Otoshidama](https://atcoder.jp/contests/abc085/tasks/abc085_c)

```golang

func mainABC085C() {
	var n, y int
	fmt.Scan(&n, &y)
	var res = abc085C(n, y)
	fmt.Println(res)
}

func abc085C(n, y int) string {
	var res = ""

	for tenThous := 0; tenThous <= n; tenThous++ {
		for fiveThous := 0; fiveThous <= n; fiveThous++ {
			if tenThous+fiveThous > n {
				break
			}
			var thous = n - (tenThous + fiveThous)
			if tenThous+fiveThous+thous == n && tenThous*10000+fiveThous*5000+thous*1000 == y {
				res = strconv.Itoa(tenThous) + " " + strconv.Itoa(fiveThous) + " " + strconv.Itoa(thous)
				return res
			}

		}
	}

	return "-1 -1 -1"
}

```

 - テスト

```golang

func Test_abc085C(t *testing.T) {
	type args struct {
		n int
		y int
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{name: "1", args: args{9, 45000}, want: "0 9 0"},
		{name: "2", args: args{20, 196000}, want: "-1 -1 -1"},
		{name: "3", args: args{2000, 20000000}, want: "2000 0 0"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc085C(tt.args.n, tt.args.y); got != tt.want {
				t.Errorf("abc085C() = %v, want %v", got, tt.want)
			}
		})
	}
}
```

#第9問 [ABC049C - 白昼夢 / Daydream](https://atcoder.jp/contests/abc049/tasks/arc065_a)

##重要：文字列操作

 - string#Replcae()で文字列を置換できる。
    -  引数は左から順にベース文字列、置換前の部分文字列、置換後の部分文字列、最大置換回数
    -  ベース文字列中の全ての部分文字列を置換する場合は最大置換回数に-1を指定

 - GO言語にはどうやら文字列を逆転する関数がない様子
    - rune　というのを使うと逆転できるっぽいので、ググって実装
    - Reverse(s string)　の部分

```golang
func mainABC049C() {
	var wk string
	fmt.Scan(&wk)
	var res = abc049C(wk)
	fmt.Println(res)
}

func abc049C(wk string) string {
	var res = ""

	wk = Reverse(wk)
	wk = strings.Replace(wk, "resare", "", -1)
	wk = strings.Replace(wk, "esare", "", -1)
	wk = strings.Replace(wk, "remaerd", "", -1)
	wk = strings.Replace(wk, "maerd", "", -1)

	if len(wk) == 0 {
		res = "YES"
	} else {
		res = "NO"
	}
	return res
}

//Reverse string reverse
func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

```

 - テスト

```golang
func Test_abc049C(t *testing.T) {
	type args struct {
		wk string
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{name: "1", args: args{"erasedream"}, want: "YES"},
		{name: "2", args: args{"dreameraser"}, want: "YES"},
		{name: "3", args: args{"dreamerer"}, want: "NO"},
		{name: "4", args: args{"eraserdreameraser"}, want: "YES"},
		{name: "5", args: args{"erasedreamerase"}, want: "YES"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc049C(tt.args.wk); got != tt.want {
				t.Errorf("abc049C() = %v, want %v", got, tt.want)
			}
		})
	}
}
```

#第10問 [ABC086C - Traveling](https://atcoder.jp/contests/abc086/tasks/arc089_a)

##重要：多次元配列

 - 多次元配列で引数を渡したかったのでslice？をいじくってこんな形
 - 正確な作り方がｲﾏｲﾁわからず。。。
 - 3次元とかの多次元配列になると出来る自信はない。。。

```golang
func mainABC086C() {
	var n int
	fmt.Scan(&n)

	var wk [][]int
	wk = make([][]int, n)
	for index := 0; index < n; index++ {
		wk[index] = make([]int, 0)
		var t, x, y int
		fmt.Scan(&t, &x, &y)
		wk[index] = append(wk[index], t)
		wk[index] = append(wk[index], x)
		wk[index] = append(wk[index], y)
	}
	var res = abc086C(n, wk)
	fmt.Println(res)
}

func abc086C(n int, wk [][]int) string {

	var res = "Yes"

	var time = 0
	var pX = 0
	var pY = 0
	for index := 0; index < n; index++ {
		if wk[index][0]%2 == (wk[index][1]+wk[index][2])%2 {
			time = IntAbs(wk[index][0] - time)
			pX = IntAbs(wk[index][1] - pX)
			pY = IntAbs(wk[index][2] - pY)
			if time < pX+pY {
				res = "No"
				break
			}
		} else {
			res = "No"
			break
		}
		wk[index][0] = time
		wk[index][1] = pX
		wk[index][2] = pY
	}
	return res
}
```

 - テスト

```golang
func Test_abc086C(t *testing.T) {
	type args struct {
		n  int
		wk [][]int
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{name: "1", args: args{2, [][]int{{3, 1, 2}, {6, 1, 1}}}, want: "Yes"},
		{name: "2", args: args{1, [][]int{{2, 100, 100}}}, want: "No"},
		{name: "3", args: args{1, [][]int{{5, 1, 1}, {100, 1, 1}}}, want: "No"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := abc086C(tt.args.n, tt.args.wk); got != tt.want {
				t.Errorf("abc086C() = %v, want %v", got, tt.want)
			}
		})
	}
}

```