求2个数字,的最大公约数和最小公倍数。实在想不出怎么写这个算法了。

解决方案 »

  1.   

    declare @t table (
    id int,
    value int
    )insert @t select
    1,40
    union all select
    2,50
    union all select
    3,60declare @i int
    select @i=min(value) from @t
    while exists (
        select 1
        from @t
        where value % @i <> 0
        )
        set @i = @i -1
    select @i as 最大公约数
    declare @t table (
    id int,
    value int
    )insert @t select
    1,5
    union all select
    2,6
    union all select
    3,15declare @max int
    select @max=max(value) from @t
    declare @i int
    set @i =1
    while exists (
        select 1
        from @t
        where @i*@max % value <> 0
        )
        set @i = @i + 1
    select @i*@max as 最小公倍数
      

  2.   

    给你一个最小公倍数的。
    --思路原创:Haiwer。
    declare @t table (id int,value int)insert @t select 1,5 union all select 2,6 union all select 3,15declare @max int
    select @max=max(value) from @t
    declare @i int
    set @i =1
    while exists (select 1 from @t where @i*@max % value <> 0)
    set @i = @i + 1
    select @i*@max as 最小公倍数