【问题标题】:How to find the GCD of three numbers within a single method如何在单个方法中找到三个数字的 GCD
【发布时间】:2012-11-26 20:56:21
【问题描述】:

我必须确保 3 个数字之间的 GCD 不大于 1。

这是我到目前为止的方法代码:

private int greatestCommonFactor(int a, int b, int c)
{
    for(int n = 0; n <= number; n++)
    {
        if()
    }

    return 1;
}

当我开始在实验室工作时,return 1 已经在那里了。如何确保 GCD 不超过 1?并返回所有三个整数?

如果它有助于弄清楚需要做什么,下面是代码的其余部分:

import static java.lang.System.*;

public class Triples
{
 private int number;

public Triples()
{
    this(0);
}

public Triples(int num)
{
    number = num;
}

public void setNum(int num)
{
    number = num;
}

private int greatestCommonFactor(int a, int b, int c)
{
    for(int n = 0; n <= number; n++)
    {
        if()
    }

    return 1;
}

public String toString()
{
    String output="";
    int max = number;
    for(a = 1; a <= max; a++)
    {
        for(b = a +1; b <= max; b++)
        {
            for(c = b + 1; c <= max; c++)
            {
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                }
            }
        }
    }


    return output+"\n";
}
}

更新

这是我为同一个实验室编写的新代码:

import static java.lang.System.*;

public class Triples
{
 private int number;

public Triples()
{
    this(0);
}

public Triples(int num)
{
    number = num;
}

public void setNum(int num)
{
    number = num;
}

private int greatestCommonFactor(int a, int b, int c)
{
    for(int n = 0; n <= number; n++)
    {
    int max = number;
    for(a = 1; a <= max; a++)
    {
        a = n;
        for(b = a +1; b <= max; b++)
        {
            b =n;
            for(c = b + 1; c <= max; c++)
            {
                c = n;
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                    {
                        if(a%2<=1 && b%2<=1 && c%2<=1)
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }
    }

    return 1;
}

public String toString()
{
    String output="";
    output = greatestCommonFactor(a, b, c);


    return output+"\n";
}
}

【问题讨论】:

  • “返回所有三个整数”是什么意思?
  • 插入行private int greatestCommonFactor(int a, int b, int c)
  • 如果有帮助,这里是指向实验室工作表的 google 文档版本的链接:docs.google.com/open?id=0B_ifaCiEZgtcX08tbW1jNThZZmM
  • 我认为这就是问题所暗示的:你必须找到 a、b 和 c 的 GCD。 1 是“默认值”,因为 1 绝对是一个因素,并且函数应该返回一个 int。您必须编写用于确定 GCD 的代码,并且您的代码应放在 'return 1;' 之前
  • @TJ-,我的代码不是在return 1; 之前吗?

标签: java int greatest-common-divisor


【解决方案1】:

您可以使用Euclid's algorithm 计算ab 的GCD。调用结果d。那么abc的GCD就是cd的GCD;为此,您可以再次使用欧几里得算法。

【讨论】:

    【解决方案2】:

    如果您不关心效率,这是一种蛮力方法:

    private int greatestCommonFactor(int a, int b, int c)
    {
        limit = Math.min(a, b);
        limit = Math.min(limit, c);
        for(int n = limit; n >= 2; n--)
        {
            if ( (a % n == 0) && (b % n == 0) && (c % n == 0) ) {
                return n;
            }
        }
    
        return 1;
    }
    

    说明:

    • 您可以通过只检查最少(a, b, c) 来节省一些工作。任何大于此的数字肯定不会是所有 3 的 GCD。
    • 您需要从n = limit 而不是n = 0 开始循环并倒数。
    • 一旦我们遇到一个对(a, b, c) 产生零余数的数字,那一定是 GCD。
    • 如果在循环中找不到任何内容,则 GCD 默认为 1。

    【讨论】:

    • 因为这是一个家庭作业问题,最好在学生自己尝试解决问题之前不给出解决方案。
    • 仍在编辑,注意到我在睡眠债上犯了很多小错误:
    • 前段时间我不得不做一个 GCD 实验室,但我无法在家中访问已完成的文件并记得进行反向计数,但我想不出如何在这里实现它
    • 所以我想我可以让它在它出现的每个数字上使用 mod 运算符 n 与(引用我的代码)然后如果说 n%2==1 然后我可以分配它到a 但后来我被困在如何分配其他变量
    • 您的解决方案中的一个错误是从未为 a、b 和 c 分配实际值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多