1447.最简分数
1447.最简分数
题解
暴力,双重循环即可,重复用最大公约数算即可
代码
package main import "strconv" func simplifiedFractions(n int) (result []string) { for down := 2; down <= n; down++ { for up := 1; up < down; up++ { if gcd(up, down) == 1 { result = append(result, strconv.Itoa(up)+"/"+strconv.Itoa(down)) } } } return } func gcd(a, b int) int { for a != 0 { a, b = b%a, a } return b }