I recently heard that Go's standard package doesn't have a max function, type binding is pretty tight, and there are a lot of strict aspects, so I tried to see how strict it really is compared to Python3. ..
A https://atcoder.jp/contests/abc188/tasks/abc188_a The problem is that if you decide three points in basketball, you will decide whether or not the development will be reversed.
It seems that the input can be entered with fmt.Scan (& a)
for the declared type.
a.go
package main
import (
"fmt"
"math"
)
func main() {
var X, Y int
fmt.Scan(&X, &Y)
if math.Abs(X-Y) < 3 {
fmt.Print("Yes")
} else {
fmt.Print("No")
}
}
I thought this would be fine, but apparently math.Abs ()
only accepts float64
as an argument. So if you declare var X, Y float64
instead of var X, Y int
, it works.
https://golang.org/pkg/math/
The math
package seems to only accept the basic float64
, so using int
doesn't seem to make much sense.
I decided to use float64 for all subsequent declarations.
By the way, if you write this in Python3
a.py
X, Y = map(int, input().split())
if abs(X - Y) < 3:
print("Yes")
else:
print("No")
Of course, Python's abs ()
can be int or float.
B
https://atcoder.jp/contests/abc188/tasks/abc188_b
The question of whether the dot product of Vectors is zero.
Assuming that you can input the array with fmt.Scan (& A)
,
var X float64
var A, B []float64
fmt.Scan(&X, &A, &B)
I thought that there was a way to write something like this, but there was no such thing, so I wrote it as Gugu.
b.go
package main
import "fmt"
func main() {
var N int
fmt.Scan(&N)
A, B := make([]float64, N), make([]float64, N)
for i := 0; i < N; i++ {
fmt.Scan(&A[i])
}
for i := 0; i < N; i++ {
fmt.Scan(&B[i])
}
total := float64(0)
for i := 0; i < N; i++ {
total += A[i] * B[i]
}
if total == 0 {
fmt.Print("Yes")
} else {
fmt.Print("No")
}
}
Using a for statement with standard input for arrays is a bit tricky. I want to make a template around here so that I can easily put it in.
b.py
_ = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
total = 0
for i in range(len(X)):
total += X[i] * Y[i]
if total == 0:
print("Yes")
else :
print("No")
In Python, it's nice to call it with one line of list (map (type, input (). Split ()))
.
C https://atcoder.jp/contests/abc188/tasks/abc188_c
It seems like dividing the array into the first half and the second half, holding the maximum value and index of each, and comparing them at the end.
c.go
package main
import (
"fmt"
"math"
)
func main() {
var N int
fmt.Scan(&N)
sN := int(math.Pow(2, float64(N)))
A := make([]int64, sN)
for i := 0; i < sN; i++ {
fmt.Scan(&A[i])
}
fmax, smax := int64(0), int64(0)
findex, sindex := -1, -1
for i, rating := range A {
if i < sN/2 {
if rating > fmax {
fmax = rating
findex = i
}
} else {
if rating > smax {
smax = rating
sindex = i
}
}
}
if fmax < smax {
fmt.Println(findex + 1)
} else {
fmt.Println(sindex + 1)
}
}
You have to think about whether to use int
or float64
depending on the scene, such as Pow
becoming float64
and i
of the for loop index becoming int
. The brain death float64
didn't work.
If you use slice and your own max
function, you can write it a little smarter.
c.py
N = int(input())
sNh = 2 ** (N - 1)
A = list(map(int, input().split()))
fA = A[:sNh]
sA = A[sNh:]
print(A.index(min(max(fA), max(sA))) + 1)
As for Python3, it was easy to implement because it has min, max functions and Array index functions.
I have only answered 3 questions,
--Use of int
and float64
properly
--There seems to be no function in the standard package that seems to be in any language, such as the max
function.
--Array initialization is lazy
The above three points are at a level that seems to be solved once a template is made, so it seems unlikely that it will be difficult to do AtCoder with Go, but neither Go nor AtCoder are recommended for beginners. It's more fun to learn only the minimum Python notation and concentrate on solving AtCoder problems without stumbling.
Recommended Posts