当前位置:网站首页>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
边栏推荐
- How does semaphore, a thread synchronization tool that uses an up counter, look like?
- 基于synchronized锁的深度解析
- 首次开通csdn,这篇文章送给过去的自己和正在发生的你
- 抢球鞋?预测股市走势?淘宝秒杀?Python表示要啥有啥
- EasyNTS上云网关设备在雪亮工程项目中的实战应用
- 亚马逊的无服务器总线EventBridge支持事件溯源 - AWS
- python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
- Commodity management system -- the search function of SPU
- Talk about my understanding of FAAS with Alibaba cloud FC
- A solution to the problem that color picker (palette) cannot use shortcut keys in sublime Text3 plug-in
猜你喜欢
Oschina plays disorderly on Monday
C++之异常捕获和处理
Android emulator error: x86 emulation currently requires hardware acceleration的解决方案
How to do thread dump analysis in Windows Environment
LTM understanding and configuration notes
[QT] subclass qthread to realize multithreading
Composition - API
range_sensor_layer
ThinkPHP门面源码解析
1.操作系统是干什么的?
随机推荐
Finally, the python project is released as exe executable program process
How to do thread dump analysis in Windows Environment
LTM understanding and configuration notes
Introduction to nmon
ubuntu 上使用微信的新方案——手机投屏
结合阿里云 FC 谈谈我对 FaaS 的理解
OSChina 周一乱弹 —— 程序媛的青春
The whole process of building a fully distributed cluster
RabbitMQ脑裂问题解决方案调查
Windows环境下如何进行线程Dump分析
1450. 在既定时间做作业的学生人数
SaaS: another manifestation of platform commercialization capability
Mac 终端(terminal) oh-my-zsh+solarized配置
Initial installation of linx7.5
Why don't we use graphql? - Wundergraph
range_sensor_layer
How does FC game console work?
Rainbow sorting | Dutch flag problem
Chrome浏览器引擎 Blink & V8
WordPress Import 上传的文件尺寸超过php.ini中定义的upload_max_filesize值--&gt;解决方法。