当前位置:网站首页>SQL语句实现水仙花数求取
SQL语句实现水仙花数求取
2020-11-09 10:51:00 【osc_cx8uhydz】
SQL语句实现水仙花数求取
"水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:370是一个 "水仙花数 ",因为 370=3 的三次方+7的三次方+0 的三次方
实现方法
本文用两种方法解决水仙花数的求取
方法一
第一种使用一层while循环,从100-1000遍历,取出其个位、十位、百位的数字,进行三次方计算后相加再与原数字对比,相等就打印输出。
其取出个位、十位、百位的方法需要用到内置函数:CAST函数
CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型,如:
cast(substring(cast(@n as varchar(3)),1,1) as int
该语句将int型变量n先更改为varchar(可变长度字符串)类型,再利用substring函数取出从第1个字符开始的1个字符,即在水仙花数的求取中就是取出了3位数的百位,最后再将其转化未int型变量。
方法一整体代码如下:
declare @n int,@x as int,@y as int,@z as int
set @n=100
while @n<1000
begin
set @x=cast(substring(cast(@n as varchar(3)),1,1) as int)
set @y=cast(substring(cast(@n as varchar(3)),2,1) as int)
set @z=cast(substring(cast(@n as varchar(3)),3,1) as int)
if power(@x,3)+power(@y,3)+power(@z,3)=@n
begin
print @n
end
set @n=@n+1
end
用x,y,z分别存储其百位、十位和个位
方法二
方法二是本人一开始使用的方法,即同样定义x,y,z存储其百位、十位和个位,通过3个while循环,x从1-9遍历,y、z分别从0-9遍历,通过x,y,z三次方的和与100x+10y+z对比得到水仙花数。
而一开始错误的实现代码如下:
declare @x int, @y int,@z int,@m int
set @x=1
set @y=0
set @z=0
while @x<10
begin
while @y<10
begin
while @z<10
begin
if power(@x,3)+power(@y,3)+power(@z,3)=(@x*100+@y*10+@z)
begin
select @m=@x*100+@y*10+@z
print @m
end
set @z=@z+1
end
set @y=@y+1
end
set @x=@x+1
end
通过sql server执行代码后只有命令已成功完成语句,并没有输出结果,说明代码有误
经过检查,发现有可能是循环出问题,便用以下代码进行检验:
declare @x int, @y int,@z int,@m int
set @x=1
set @y=0
set @z=0
while @x<10
begin
while @y<10
begin
while @z<10
begin
print @z
set @z=@z+1
end
set @y=@y+1
end
set @x=@x+1
end
发现其输出结果仅有10次

发现原因是while嵌套循环外部循环变量不能进入内部循环,于是将set @y与set @z语句放入内层循环之前
修正后代码(正确)如下:
declare @x int, @y int,@z int,@m int
set @x=1
while @x<10
begin
set @y=0
while @y<10
begin
set @z=0
while @z<10
begin
if power(@x,3)+power(@y,3)+power(@z,3)=(@x*100+@y*10+@z)
begin
select @m=@x*100+@y*10+@z
print @m
end
set @z=@z+1
end
set @y=@y+1
end
set @x=@x+1
end
其输出结果与方法一相同

最后得到153,370,371,407为水仙花数
版权声明
本文为[osc_cx8uhydz]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4343506/blog/4708852
边栏推荐
- Teacher Liang's small class
- OSChina 周一乱弹 —— 程序媛的青春
- 卧槽,这年轻人不讲武德,应届生凭“小抄”干掉5年老鸟,成功拿到字节20Koffer
- 无法启动此程序,因为计算机中丢失 MSVCP120.dll。尝试安装该程序以解决此问题
- Depth first search and breadth first search
- A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
- Adding OpenGL form to MFC dialog
- 结合阿里云 FC 谈谈我对 FaaS 的理解
- 从实践谈 Ruby 语法上的几个设计不一致带来的问题。
- 深度优先搜索和广度优先搜索
猜你喜欢
随机推荐
File queue in Bifrost (1)
向北京集结!OpenI/O 2020启智开发者大会进入倒计时
When Python calls ffmpeg, 'ffmpeg' is not an internal or external command, nor a runnable program
5年程序员面试,常见面试问题解析
Capture bubbles? Is browser a fish?
Have you ever thought about why the transaction and refund have to be split into different tables
基于LabVIEW实现的几种滚动字幕
Share API on the web
App crashed inexplicably. At first, it thought it was the case of the name in the header. Finally, it was found that it was the fault of the container!
做用户,绕不开画像!
Detailed analysis of OpenGL es framework (8) -- OpenGL es Design Guide
Several rolling captions based on LabVIEW
Elasticsearch原理解析与性能调优
How to do thread dump analysis in Windows Environment
Ten year itch of programmer
Android emulator error: x86 emulation currently requires hardware acceleration solution
商品管理系统——整合仓库服务以及获取仓库列表
2 normal mode
寻找性能更优秀的动态 Getter 和 Setter 方案
OpenGL ES 框架详细解析(八) —— OpenGL ES 设计指南



