当前位置: 代码迷 >> Sql Server >> 来用SQL语句算最大公约数了 分不多 就100.解决思路
  详细解决方案

来用SQL语句算最大公约数了 分不多 就100.解决思路

热度:65   发布时间:2016-04-27 18:02:21.0
来用SQL语句算最大公约数了 分不多 就100...!
请用SQL语句求下列两个数的最大公约数:::


4294967294 3465465468



------解决方案--------------------
SQL code
/*求两个数的最大公约数*/create function GetGys(@num1 int,@num2 int)returns int --返回值asbegindeclare @times int  --计数器declare @min int    --存储两个数的较小者declare @result int  --保存结果if(@num1>[email protected])set @[email protected]elseset @[email protected]set @[email protected]while(@times<[email protected]) --循环beginif(@[email protected]=0 and @[email protected]=0)beginset @[email protected]breakendset @[email protected]endreturn @resultend/*求两个数的最小公倍数*/create function GetGbs(@num1 int,@num2 int)returns intasbegindeclare @result int  --结果declare @max int  --保存两个数的大者declare @times intif @num1<[email protected]set @[email protected]elseset @[email protected]set @[email protected]while(@times>[email protected])beginif(@[email protected]=0 and @[email protected]=0)beginset @[email protected]breakendset @[email protected]+1endreturn @resultend 最后测试:运行select dbo.GetGys(15,20) as 最大公约数,dbo.GetGbs(15,20) as 最小公倍数显示结果:最大公约数      最小公倍数   5                60赞同23| 评论
------解决方案--------------------
SQL code
GOCreate FUNCTION fn_num(@a BIGINT,@b BIGINT)RETURNS BIGINTASBEGIN    DECLARE @c BIGINT    SET @[email protected][email protected]    WHILE @c>0        select @[email protected],@[email protected],@[email protected][email protected]    RETURN @bENDGOSELECT dbo.fn_num(4294967294,3465465468)
  相关解决方案