-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathac412.go
50 lines (44 loc) · 792 Bytes
/
ac412.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package problem412
import (
"strconv"
"fmt"
)
func fizzBuzz(n int) []string {
ans := make([]string, 0)
for i := 1; i <= n; i++ {
ans = append(ans, number(i))
}
return ans
}
func number(n int) string {
if 0 == n%3 && 0 == n%5 {
return "FizzBuzz"
} else if 0 == n%5 {
return "Buzz"
} else if 0 == n%3 {
return "Fizz"
} else {
return strconv.Itoa(n)
}
}
func fizzBuzz2(n int) []string {
ans := make([]string, 0)
for i, fizz, buzz := 1, 0, 0; i <= n; i++ {
fizz++
buzz++
if 3 == fizz && 5 == buzz {
ans = append(ans, "FizzBuzz")
fizz = 0
buzz = 0
} else if 3 == fizz {
ans = append(ans, "Fizz")
fizz = 0
} else if 5 == buzz {
ans = append(ans, "Buzz")
buzz = 0
} else {
ans = append(ans, fmt.Sprintf("%d", i))
}
}
return ans
}