通過SQL繪制楊輝三角的實(shí)現(xiàn)方法介紹建站知識(shí)
導(dǎo)讀:1建站知識(shí)本篇文章小編為大家介紹,通過SQL繪制楊輝三角的實(shí)現(xiàn)方法介紹。需要的朋友參考下seo網(wǎng)站排名優(yōu)化軟件建設(shè)網(wǎng)站公司。
無意中在csdn上看到一帖有關(guān)繪制楊輝三角的sql表達(dá)式,感覺很有意思。后來自己想下不借助臨時(shí)表,根據(jù)楊輝三角的組合數(shù)計(jì)算方法C(n,m)=n!/[m!(n-m)!],進(jìn)行繪制。
以下是完整的SQL代碼:
復(fù)制代碼 代碼如下:
use tempdbgoset nocount ondeclare @rows int=10, --行數(shù),根據(jù)實(shí)際來控制 @x int=1,@y int=1,@sql nvarchar(max),@cols int/*根據(jù)楊輝三角的組合數(shù)計(jì)算方法:C(n,m)=n!/[m!(n-m)!]進(jìn)行繪制參照:*/set @cols=@rows*2-1;with cte_n as( select r from (select row_number() over(order by a.object_id) as r from sys.all_columns a )網(wǎng)站優(yōu)化seo培訓(xùn) x where r<=@rows*2),cte_1 as(select n.r,b.data_lse from cte_n n cross apply(select 'select '+stuff((select ',rtrim('+isnull(F1.v+'/(('+F2.v+')*'+F3.v+')','''''') +') as '+quotename(isnull(nullif((m.r +(@rows-n.r)+(m.r-1)*1)%@cols,0),@cols)) from cte_n m outer apply(select stuff((select '*'+rtrim(i.r) from cte_n i where i.r<=isnull((nullif(n.r-1,0)),1) for xml path('')),1,1,'') as v ) F1 out百度seo網(wǎng)站優(yōu)化er apply(select stuff((se網(wǎng)站seo優(yōu)化培訓(xùn)lect '*'+rtrim(i.r) from cte_n i where i.r<=isnull((nullif(m.r-1,0)),1) for xml path('')),1,1,'') as v ) F2 outer apply(select stuff((select '*'+rtrim(i.r) from cte_n i where i.r<=isnull((nullif(n.r-m.r,0)),1) for xml path('')),1,1,'') as v ) F3 where m.r<@rows*2 order by isnull(nullif((m.r +(@rows-n.r)+(m.r-1)*1)%@cols,0),@cols) asc for xml path('') ),1,1,'') as data_lse )b where n.r <=@rows )
select @sql=isnull(@sql+' union all ','')+data_lse from cte_1exec(@sql)
(【注】:當(dāng)前腳本在SQL Server 2012上測(cè)試通過)效果圖:
這方法雖然沒有借助臨時(shí)表,也有一個(gè)最大的不足就是不能設(shè)置太多行,因?yàn)樵诠剑–(n,m)=n!/[m!(n-m)!])中有n! 和m! 算式,設(shè)置行數(shù)太多會(huì)導(dǎo)致階乘數(shù)據(jù)太大,發(fā)生數(shù)據(jù)類型轉(zhuǎn)換溢出。有時(shí)間再想辦法看能否從表示式中"/"除位置進(jìn)行優(yōu)化相關(guān)seo網(wǎng)站排名優(yōu)化軟件建設(shè)網(wǎng)站公司。聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-23 ,文章通過SQL繪制楊輝三角的實(shí)現(xiàn)方法介紹建站知識(shí)主要講述標(biāo)簽,標(biāo)題,SQL網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請(qǐng)保留鏈接: http://www.bifwcx.com/article/web_6026.html
為你推薦與通過SQL繪制楊輝三角的實(shí)現(xiàn)方法介紹建站知識(shí)相關(guān)的文章
-
通王TWCMS 2.0.3網(wǎng)站模板程序下載
(126)人喜歡 2024-01-15 -
Windows官方原版在哪里下載
(175)人喜歡 2024-01-15 -
WordPress網(wǎng)站模板發(fā)帖標(biāo)題顏色設(shè)置
(131)人喜歡 2024-01-07 -
修改discuz論壇帖子標(biāo)題80字符的長(zhǎng)度限制
(249)人喜歡 2024-01-07 -
wordpress程序調(diào)用不帶超鏈接的Tag標(biāo)簽
(234)人喜歡 2024-01-05 -
網(wǎng)站在不同時(shí)期需調(diào)整內(nèi)容更新的方向
(112)人喜歡 2023-08-12