用数据库语言求100~1000之间的水仙花数

2024-11-18 17:27:58
推荐回答(1个)
回答1:

declare @A int,@C varchar(4),@S int,@I int
set @A=100
while @A<=1000
begin
set @C=CAST(@A as varchar(4))
set @S=0
set @I=1
while @I<=LEN(@C)
begin
set @S=@S+POWER(CAST(SUBSTRING(@C,@I,1) as INT),LEN(@C))
set @I=@I+1
end
if @S=@A print @A
set @A=@A+1
end