小编Liv*_*ool的帖子

Java HashSet包含无法正常工作的函数

我正在写一个简单的程序如下:给定两个数字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)

java hash hashcode hashset

2
推荐指数
1
解决办法
2725
查看次数

标签 统计

hash ×1

hashcode ×1

hashset ×1

java ×1