先决条件 – PL/SQL简介 在PL/SQL代码中,命令组排列在一个块中。与声明或语句相关的块组。我们在部分中声明变量,在部分中声明变量,在部分中执行变量。
null
给定两个数字,任务是找到这些数字的GCD(最大公因数)或HCF(最高公因数)值。 例如:
Input: num1 = 4, num2 = 6 Output: gcd of (num1, num2) = 2 Input: num1 = 8, num2 = 48 Output: gcd of (num1, num2) = 8
方法 就是取两个数字,用 欧几里得的 算法。
以下是所需的实施:
DECLARE -- declare variable num1, num2 and t -- and these three variables datatype are integer num1 INTEGER ; num2 INTEGER ; t INTEGER ; BEGIN num1 := 8; num2 := 48; WHILE MOD(num2, num1) != 0 LOOP t := MOD(num2, num1); num2 := num1; num1 := t; END LOOP; dbms_output.Put_line( 'GCD of ' ||num1 || ' and ' ||num2 || ' is ' ||num1); END ; -- Program End |
输出:
GCD of 8 and 48 is 8
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END