实现杨辉三角
用存储过程和临时表来写,主要是想最后一个select出来结果,而且排成想要的三角形形状。 由于BIGINT数据的限制,最多可以显示67层。 结果 id ----------- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 1 1 2 1 1 3 1 2 1 4
用存储过程和临时表来写,主要是想最后一个select出来结果,而且排成想要的三角形形状。 由于BIGINT数据的限制,最多可以显示67层。结果
id
----------- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 1
2 1 1
3 1 2 1
4 1 3 3 1
5 1 4 6 4 1
6 1 5 10 10 5 1
7 1 6 15 20 15 6 1
8 1 7 21 35 35 21 7 1
CREATE proc #pr_YangHui @c int as /* SQL实现显示杨辉三角 */ /* 版本: 1.0 */ /* 作者: Haiwer */ /* 版权所有 */ /* 2006.05.10 */ set nocount on if @c<2 return --两层以下就不排了 declare @i int declare @j int declare @sql varchar(8000) declare @sql1 varchar(8000) declare @sql2 varchar(8000) create table #(id int IDENTITY(1,1),a50000 bigint) insert #(a50000) values (1) --第一层 set @i=2 while @i<=@c begin --为了实现动态层,只好动态修改临时表结构 set @sql='alter table # add a'+cast(50000+@i-1 as varchar(10))+' bigint,a'+cast(50000-@i+1 as varchar(10))+' bigint' exec (@sql) set @sql1='' set @sql2='' set @j=@i-1 while @j>=0 begin --这里判断有点乱 if @j=0 set @sql1=@sql1+',a'+cast(50000-@j as varchar(10)) else set @sql1=@sql1+',a'+cast(50000-@j as varchar(10))+',a'+cast(50000+@j as varchar(10)) if @j=@i-1 set @sql2=@sql2+',1,1' else if @j=0 set @sql2=@sql2+',a'+cast(50000-@j-1 as varchar(10))+'+a'+cast(50000-@j+1 as varchar(10)) else set @sql2=@sql2+',a'+cast(50000-@j-1 as varchar(10))+'+a'+cast(50000-@j+1 as varchar(10))+',a'+cast(50000+@j-1 as varchar(10))+'+a'+cast(50000+@j+1 as varchar(10)) set @j=@j-2 end --去掉多余的逗号 set @sql1=right(@sql1,len(@sql1)-1) set @sql2=right(@sql2,len(@sql2)-1) set @sql=cast(@i-1 as varchar(10)) exec('insert #('+@sql1+') select '+@sql2+' from # where id='+@sql) set @i=@i+1 end set @i=50000-@c+1 set @j=50000+@c-1 set @sql='' --去最长的数据,就是为了节省显示空间 select @sql1=CAST(len(cast(max(a50000) as varchar(50)))+1 AS VARCHAR(10)) from # while @i<=@j begin set @sql=@sql+',isnull(cast(a'+cast(@i as varchar(10))+' as varchar('+@sql1+')),'''')' set @i=@i+1 end exec ('select id'+@sql+' from # order by id') drop table # GO --调用 exec #pr_YangHui 8

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and
