Heim  >  Artikel  >  Datenbank  >  最大公约数

最大公约数

WBOY
WBOYOriginal
2016-06-07 14:55:062123Durchsuche

求两个整数的最大公约数 无 DECLARE @num1 BIGINT, @num2 BIGINT SET @num1=14SET @num2=21 DECLARE @times INT DECLARE @min INT DECLARE @result BIGINT IF( @num1 = @num2 ) SET @min=@num2 ELSE SET @min=@num1 SET @times=@min WHILE( @times = @min ) B

求两个整数的最大公约数
DECLARE @num1 BIGINT, 
        @num2 BIGINT 

SET @num1=14
SET @num2=21 

DECLARE @times INT 
DECLARE @min INT 
DECLARE @result BIGINT 

IF( @num1 >= @num2 ) 
  SET @min=@num2 
ELSE 
  SET @min=@num1 

SET @times=@min 

WHILE( @times <= @min ) 
  BEGIN 
      IF( @num1%@times = 0 
          AND @num2%@times = 0 ) 
        BEGIN 
            SET @result=@times 

            BREAK 
        END 

      SET @times=@times - 1 
  END 

SELECT @result  
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn