我正在写一个简单的程序如下:给定两个数字M和N,p来自[M,N],q来自[1,p-1],找到p/q的所有不可约分数.我的想法是蛮力所有可能的p,q值.并使用HashSet来避免重复的分数.但是,不知何故,contains函数没有按预期工作.
我的代码
import java.util.HashSet;
import java.util.Set;
public class Fraction {
private int p;
private int q;
Fraction(int p, int q) {
this.p = p;
this.q = q;
}
public static int getGCD(int a, int b) {
if (b == 0)
return a;
else
return getGCD(b, a % b);
}
public static Fraction reduce(Fraction f) {
int c = getGCD(f.p, f.q);
return new Fraction(f.p / c, f.q / c);
}
public static HashSet<Fraction> getAll(int m, int n) {
HashSet<Fraction> res = …Run Code Online (Sandbox Code Playgroud)