當前位置:首頁 » 編程語言 » sql編程最大公因數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql編程最大公因數

發布時間: 2023-02-01 21:57:45

❶ 用T-SQL流程式控制制語句編寫程序,求兩個數的最大公約數和最小公倍數

因為你的代碼中,@c的最後值一直 是2,也就是說,goto label1 會一直執行下去
下面這段代碼能夠實現你的需求

DECLARE @a int,@b int,@c int,@d int,@gongyueshu int,@gongbeishu int
set @a=18
set @b=6
if(@a = 0 or @b = 0)
begin
set @gongyueshu = 0
set @gongbeishu = 0
end
else
begin
declare @atemp int ,@btemp int
set @atemp = @a
set @btemp = @b
set @gongyueshu = 1
if(@a > @b)
set @c = @b
else
set @c = @a
if @c = 1
begin
set @gongbeishu = @a * @b
end
else
begin
set @d = 2
lable1:
if @d > @c or @d > @a or @d > @b
goto lable2
if @a % @d = 0 and @b % @d = 0
begin
set @a = @a / @d
set @b = @b / @d
set @gongyueshu = @gongyueshu * @d
goto lable1
end
else
begin
set @d = @d +1
goto lable1
end

lable2:set @gongbeishu=@atemp*@btemp/@gongyueshu

select '最大公約數是:' + cast(@gongyueshu as varchar(10))
select '最小公倍數是:' + cast(@gongbeishu as varchar(10))
end
end

❷ 編寫PL/SQL實現求最大公約數和最小公倍數

create or replace FUNCTION GETGYS2(NUM1 NUMBER, NUM2 NUMBER) RETURN NUMBER IS
RESULTNUM NUMBER;
NUM3 NUMBER;
MINNUM NUMBER;
BEGIN
IF NUM1 >= NUM2 THEN
MINNUM := NUM2;
ELSE
MINNUM := NUM1;
END IF;
NUM3 := MINNUM;
LOOP
IF (MOD(NUM1, NUM3) = 0 AND MOD(NUM2, NUM3) = 0) THEN
RESULTNUM := NUM3;
EXIT;
END IF;
NUM3 := NUM3 - 1;
END LOOP;
RETURN(RESULTNUM);
dbms_output.put_line('最大公約數='||RESULTNUM);
dbms_output.put_line('最小公倍數='||num1/RESULTNUM*num3);
END GETGYS2;