SQL写99乘法表
1:
select r1 || '*' || 1 || '=' || r1 * 1 A,
decode(r2, '', '', r2 || '*' || 2 || '=' || r2 * 2) b,
decode(r3, '', '', r3 || '*' || 3 || '=' || r3 * 3) C,
decode(r4, '', '', r4 || '*' || 4 || '=' || r4 * 4) D,
decode(r5, '', '', r5 || '*' || 5 || '=' || r5 * 5) E,
decode(r6, '', '', r6 || '*' || 6 || '=' || r6 * 6) F,
decode(r7, '', '', r7 || '*' || 7 || '=' || r7 * 7) G,
decode(r8, '', '', r8 || '*' || 8 || '=' || r8 * 8) H,
decode(r9, '', '', r9 || '*' || 9 || '=' || r9 * 9) I
from (select level r1,
lag(level+1, 1) over(order by level) r2,
lag(level+2, 2) over(order by level) r3,
lag(level+3, 3) over(order by level) r4,
lag(level+4, 4) over(order by level) r5,
lag(level+5, 5) over(order by level) r6,
lag(level+6, 6) over(order by level) r7,
lag(level+7, 7) over(order by level) r8,
lag(level+8, 8) over(order by level) r9
from dual
connect by level <10)
2:
select rn, ltrim(max(sys_connect_by_path(product, ',')), ',') product
from
(
select rn, product, min(product) over(partition by rn) product_min
,(row_number() over(order by rn, product)) + (dense_rank() over(order by rn)) numId
from
(
select b.rn, a.rn || '*' || b.rn || '=' || a.rn * b.rn product
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
order by b.rn, product
)
)
start with product = product_min
connect by numId - 1 = prior numId
group by rn
本文所用思想是在9行9列方形乘法表的基础上“切割”得到左上、右上、左下、右下四种形态的九九乘法表。最后使用switch-case语句对四种形态的九九乘法表进行整理以完善程序。首先,我们嵌套两个for循环语句得到一个方形的九九乘法表。得到的九九乘法表方形块如下。下一步,我们对方形块进行“切割”,这里我们以左下乘法表为示例。
由图我们可以添加两条约束:1.左下乘法表打印斜边的边界条件为i等于j(本文打印顺序为i×j),即当i>=j时,乘法式显示,当i2.换行条件为j==9;同理,我们可以得到右上乘法表。但是当我们想要切割左上和右下乘法表时,可以发现缺少了部分乘法公式。
python可以帮助我们输出很多内容,那么如何用python输出一个九九乘法表呢?下面我给大家分享一下。
工具/材料pycharm
01首先打开pycharm软件,点击顶部的File菜单,选择New Project选项,如下图所示
02接下来在New Project中新建一个python文件,如下图所示
03然后我们在python文件中通过for循环来进行九九乘法表的输出,如下图所示
04最后运行编写好的python程序以后,我们就可以看到下方输出了九九乘法表,如下图所示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)