pl/sql过程(一)
生活随笔
收集整理的這篇文章主要介紹了
pl/sql过程(一)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
--練習(xí)1:求100以?xún)?nèi)的素?cái)?shù)
declaren int := 100;--100以?xún)?nèi)的素?cái)?shù)i number(3);--循環(huán)100個(gè)數(shù)j number(3);--內(nèi)循環(huán)k number(3);--統(tǒng)計(jì)每個(gè)數(shù)的約數(shù)個(gè)數(shù),為2則是素?cái)?shù),否則不是
beginfor i in 1..n loopwhile j<=i loopif ((i mod j)=0) then--注意:表達(dá)模用的是:mod 而不是 %(百分號(hào))k:=k+1;end if;j:=j+1;end loop;j:=0;if k=2 thendbms_output.put_line(i);--注意表示等于的是:= 而不是 ==end if;k:=0;--計(jì)算完一個(gè)數(shù)是否為素?cái)?shù)后,把約數(shù)個(gè)數(shù)置為0end loop;
end;
--練習(xí)2:最大公約數(shù)(如:25和40)
--40/25 = 1...15 第一步:1.用較大數(shù)除以較小數(shù),40除25,判斷余數(shù)是否為0,為0則25為最大公約數(shù),不為0則到下一步
--25/15=1...10 第二步:2.被除數(shù)就是上一個(gè)式子的除數(shù),除數(shù)等于上一個(gè)式子的余數(shù)
--15/10=1...5 ...
--10/5=0...0 直到余數(shù)為0,則除數(shù)是最大公約數(shù)
declare i integer :=25;j integer :=40;gcd integer;temp integer;
beginif i<j thentemp := i;i := j;j := temp;end if;if mod(i,j)=0 thengcd := j;end if;while mod(i,j)>0 loopi := mod(i,j);if i<j thentemp := i;i := j;j := temp;end if;if mod(i,j)=0 thengcd := j;end if;end loop;dbms_output.put_line('25和40的最大公約數(shù)是:'||gcd);
end;
-- 練習(xí)3:最小公倍數(shù)(如25和40)
-- 最小公倍數(shù) = (數(shù)1 * 數(shù)2)/ 最大公約數(shù)
declare i integer :=25;j integer :=40;gcd integer;n integer;temp integer;
beginn:=i*j;if i<j thentemp := i;i := j;j := temp;end if;if mod(i,j)=0 thengcd := j;end if;while mod(i,j)>0 loopi := mod(i,j);if i<j thentemp := i;i := j;j := temp;end if;if mod(i,j)=0 thengcd := j;end if;end loop;dbms_output.put_line('25和40的最小公倍數(shù)是:'||n/gcd);
end;
寫(xiě)過(guò)程主要是要把邏輯理清楚,其次,要注意一些細(xì)節(jié):
1.每句代碼后一定要記得帶分號(hào)。
2.凡是循環(huán)都要記得結(jié)束循環(huán)end loop。
3.基本機(jī)構(gòu)要記清楚:if * then * end if之類(lèi)的。
4.模用的是:mod(n1,n2)=>n1 mod n2。
5.等于符號(hào)是一個(gè) = 而不是兩個(gè) ==
總結(jié)
以上是生活随笔為你收集整理的pl/sql过程(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 机器学习库一:scikit-learn
- 下一篇: 线性回归(y=ax+b)