Ruby 使用堆栈查找最大公约数

Ruby 使用堆栈查找最大公约数,ruby,Ruby,我正在尝试实现一种算法,用堆栈找到最大公约数:我无法根据下面的逻辑得出正确的答案。请帮忙。这是我的密码: def d8(a,b) if (a==b) return a end s = Stack.new s.push(b) s.push(a) c1 = s.pop c2 = s.pop while c1!=c2 if s.count>0 c1 = s.pop c2 = s.pop end if c1== c2

我正在尝试实现一种算法,用堆栈找到最大公约数:我无法根据下面的逻辑得出正确的答案。请帮忙。这是我的密码:

def d8(a,b)
if (a==b)
    return a 
end
s = Stack.new
s.push(b)
s.push(a)

c1 = s.pop
c2 = s.pop

while c1!=c2
    if s.count>0
        c1 = s.pop
        c2 = s.pop
    end

    if c1== c2
        return c1
    elsif c1>c2
        c1 = c1-c2
        s.push(c2)
        s.push(c1)
    else
        c2 = c2 -c1
        s.push(c2)
        s.push(c1)
    end
end
    return nil
end

GCD不能是
nil
。两个整数总是有一个GCD。因此,函数中的逻辑已经不正确,因为在某些情况下,它有一个
返回nil

查看此
返回nil
条件,它发生在
c1==c2
时(它将退出
循环)。同时,在
while
循环中,如果c1==c2,则返回一个值
。这两种情况在逻辑上是矛盾的。换句话说,在
c1==c2
条件下退出
循环,并将该条件视为无效,然后
如果c1==c2
条件可以触发并将该条件视为有效并返回正确答案

稍微简化一下逻辑,您可以得到:

def d8(a,b)
  return a if a == b   # Just a simpler way of doing a small if statement

  s = Stack.new    # "Stack" must be a gem, not std Ruby; "Array" will work here
  s.push(b)
  s.push(a)

  #c1 = s.pop       # These two statements aren't really needed because of the first
  #c2 = s.pop       #  "if" condition in the while loop

  while c1 != c2
    if s.count > 0
      c1 = s.pop
      c2 = s.pop
    end

    # if c1 == c2 isn't needed because the `while` condition takes care of it
    if c1 > c2
      c1 = c1 - c2
    else
      c2 = c2 - c1
    end

    # These pushes are the same at the end of both if conditions, so they 
    # can be pulled out
    s.push(c2)
    s.push(c1)
  end

  return c1   # This return occurs when c1 == c2
end
这是可行的,但更明显的是,堆栈的使用是多余的,在算法中根本没有任何作用
s.count>0
将始终为
true
,并且在推送变量后立即弹出变量(基本上是无操作)。这相当于:

def d8(a,b)
  return a if a == b

  c1 = a
  c2 = b

  while c1 != c2
    if c1 > c2
      c1 = c1 - c2
    else
      c2 = c2 - c1
    end
  end

  return c1
end

GCD不能是
nil
。两个整数总是有一个GCD。因此,函数中的逻辑已经不正确,因为在某些情况下,它有一个
返回nil

查看此
返回nil
条件,它发生在
c1==c2
时(它将退出
循环)。同时,在
while
循环中,如果c1==c2,则返回一个值
。这两种情况在逻辑上是矛盾的。换句话说,在
c1==c2
条件下退出
循环,并将该条件视为无效,然后
如果c1==c2
条件可以触发并将该条件视为有效并返回正确答案

稍微简化一下逻辑,您可以得到:

def d8(a,b)
  return a if a == b   # Just a simpler way of doing a small if statement

  s = Stack.new    # "Stack" must be a gem, not std Ruby; "Array" will work here
  s.push(b)
  s.push(a)

  #c1 = s.pop       # These two statements aren't really needed because of the first
  #c2 = s.pop       #  "if" condition in the while loop

  while c1 != c2
    if s.count > 0
      c1 = s.pop
      c2 = s.pop
    end

    # if c1 == c2 isn't needed because the `while` condition takes care of it
    if c1 > c2
      c1 = c1 - c2
    else
      c2 = c2 - c1
    end

    # These pushes are the same at the end of both if conditions, so they 
    # can be pulled out
    s.push(c2)
    s.push(c1)
  end

  return c1   # This return occurs when c1 == c2
end
这是可行的,但更明显的是,堆栈的使用是多余的,在算法中根本没有任何作用
s.count>0
将始终为
true
,并且在推送变量后立即弹出变量(基本上是无操作)。这相当于:

def d8(a,b)
  return a if a == b

  c1 = a
  c2 = b

  while c1 != c2
    if c1 > c2
      c1 = c1 - c2
    else
      c2 = c2 - c1
    end
  end

  return c1
end

它的Java代码是

publicstaticintgcd(intp,intq){
StackGeneric堆栈=新的StackGeneric();
内部温度;
栈推(p);
栈推(q);
while(true){
q=stack.pop();
p=stack.pop();
如果(q==0){
打破
}
温度=q;
q=p%q;
p=温度;
栈推(p);
栈推(q);
}
返回p;
}
用while循环替换递归解决方案中的函数调用,并迭代它,直到第二个参数变为0,就像递归函数调用一样

    public static int gcd (int p, int q) {
        if (q == 0) {
            return p;
        }
        return gcd(q, p % q);
    }

它的Java代码是

publicstaticintgcd(intp,intq){
StackGeneric堆栈=新的StackGeneric();
内部温度;
栈推(p);
栈推(q);
while(true){
q=stack.pop();
p=stack.pop();
如果(q==0){
打破
}
温度=q;
q=p%q;
p=温度;
栈推(p);
栈推(q);
}
返回p;
}
用while循环替换递归解决方案中的函数调用,并迭代它,直到第二个参数变为0,就像递归函数调用一样

    public static int gcd (int p, int q) {
        if (q == 0) {
            return p;
        }
        return gcd(q, p % q);
    }
副本的副本