今天去开发版块转了转,看到一个帖子大家在热烈的讨论乘法口诀。记得以前学习C++的时候最基本的题就是在屏幕上输出一个惩罚口诀。当初是用两个循环就实现了,今天看到很多人用各种不同的方法,学习了不少。在这里把大家的方法总结汇总一下。
=================================================================================
//by benchim888
//利用while 循环
set serveroutput on;
declare
i int;
j int;
begin
i:=1;
j:=1;
while i < 10
loop
while j <= i
loop
dbms_output.put(i||'*'||j||'=');
if length(i*j) = 1 and j!=1 then
dbms_output.put(' ');
end if;
dbms_output.put(i*j||' ');
j:=j+1;
end loop;
j:=1;
i:=i+1;
dbms_output.put_line(' ');
end loop;
end;
/
***********************************
利用for循环
set serveroutput on;
declare
begin
for i in 1..9 loop
for j in 1 .. i loop
dbms_output.put(i);
dbms_output.put('*');
dbms_output.put(j);
dbms_output.put('=');
if length(i*j) = 1 and j!=1 then
dbms_output.put(' ');
end if;
dbms_output.put(i*j);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
end;
/
/*
dbms_putline.put
dbms_output.put_line(' ') 或者dbms_output.new_line;
dbms_output.put_line 相当于先dbms_output.put再dbms_output.new_line
*/
下面的方法就是用SQL语句来写了哦,呵呵。
//by wildwave
select a.rn,substr(max(sys_connect_by_path(a.rn||'*'||b.rn||'='||a.rn*b.rn,',')),2)
from(select rownum rn from all_objects where rownum<=9)a,
(select rownum rn from all_objects where rownum<=9)b
where a.rn>=b.rn
connect by prior a.rn=a.rn
and prior b.rn=b.rn-1
start with b.rn=1
group by a.rn
order by 1
RN SUBSTR(MAX(SYS_CONNECT_BY_PATH
1 1*1=1
2 2*1=2,2*2=4
3 3*1=3,3*2=6,3*3=9
4 4*1=4,4*2=8,4*3=12,4*4=16
5 5*1=5,5*2=10,5*3=15,5*4=20,5*5=25
6 6*1=6,6*2=12,6*3=18,6*4=24,6*5=30,6*6=36
7 7*1=7,7*2=14,7*3=21,7*4=28,7*5=35,7*6=42,7*7=49
8 8*1=8,8*2=16,8*3=24,8*4=32,8*5=40,8*6=48,8*7=56,8*8=64
9 9*1=9,9*2=18,9*3=27,9*4=36,9*5=45,9*6=54,9*7=63,9*8=72,9*9=81
select a.rn,
substr(max(sys_connect_by_path(
case when a.rn*b.rn>=10 then
substr(translate(b.rn||'*'||a.rn||'='||a.rn*b.rn,'1234567890*=','一二三四五六七八九十'),1,
case when mod(a.rn*b.rn,10)=0 or a.rn*b.rn>20 then 3 else 2 end)||'十'||
translate(mod(a.rn*b.rn,10),'1234567890','一二三四五六七八九')
else translate(b.rn||'*'||a.rn||'='||a.rn*b.rn,'123456789=*','一二三四五六七八九得')end
,',')),2) 口诀
from(select rownum rn from all_objects where rownum<=9)a,
(select rownum rn from all_objects where rownum<=9)b
where a.rn>=b.rn
connect by prior a.rn=a.rn
and prior b.rn=b.rn-1
start with b.rn=1
group by a.rn
order by 1
RN 口诀
1 一一得一
2 一二得二,二二得四
3 一三得三,二三得六,三三得九
4 一四得四,二四得八,三四十二,四四十六
5 一五得五,二五一十,三五十五,四五二十,五五二十五
6 一六得六,二六十二,三六十八,四六二十四,五六三十,六六三十六
7 一七得七,二七十四,三七二十一,四七二十八,五七三十五,六七四十二,七七四十九
8 一八得八,二八十六,三八二十四,四八三十二,五八四十,六八四十八,七八五十六,八八六十四
9 一九得九,二九十八,三九二十七,四九三十六,五九四十五,六九五十四,七九六十三,八九七十二,九九八十一
select replace(substr(max(sys_connect_by_path(口诀,'#')),2),'#',chr(10)) from(
select a.rn,
substr(max(sys_connect_by_path(
case when a.rn*b.rn>=10 then
substr(translate(b.rn||'*'||a.rn||'='||a.rn*b.rn,'1234567890*=','一二三四五六七八九十'),1,
case when mod(a.rn*b.rn,10)=0 or a.rn*b.rn>20 then 3 else 2 end)||'十'||
translate(mod(a.rn*b.rn,10),'1234567890','一二三四五六七八九')
else translate(b.rn||'*'||a.rn||'='||a.rn*b.rn,'123456789=*','一二三四五六七八九得')end
,',')),2) 口诀
from(select rownum rn from all_objects where rownum<=9)a,
(select rownum rn from all_objects where rownum<=9)b
where a.rn>=b.rn
connect by prior a.rn=a.rn
and prior b.rn=b.rn-1
start with b.rn=1
group by a.rn)
start with rn=1
connect by prior rn=rn-1
//这里好像少了个列标题?!
一一得一
一二得二,二二得四
一三得三,二三得六,三三得九
一四得四,二四得八,三四十二,四四十六
一五得五,二五一十,三五十五,四五二十,五五二十五
一六得六,二六十二,三六十八,四六二十四,五六三十,六六三十六
一七得七,二七十四,三七二十一,四七二十八,五七三十五,六七四十二,七七四十九
一八得八,二八十六,三八二十四,四八三十二,五八四十,六八四十八,七八五十六,八八六十四
一九得九,二九十八,三九二十七,四九三十六,五九四十五,六九五十四,七九六十三,八九七十二,九九八十一