-- SQL Server 打印菲波那切数列,今天去面试竟然没做出来,我是无语啦!begin
declare @i int
declare @j int
declare @k int
declare @sum int
set @i = 1
set @j = 0
set @k = 2
set @sum = 0
while @k < 100
begin
set @sum = @i + @j
print @sum
set @i = @j
set @j = @sum
set @k = @k +1
end
end
declare @i int
declare @j int
declare @k int
declare @sum int
set @i = 1
set @j = 0
set @k = 2
set @sum = 0
while @k < 100
begin
set @sum = @i + @j
print @sum
set @i = @j
set @j = @sum
set @k = @k +1
end
end
#include<iostream.h>
int fiboArray(int n);
void main()
{
int n;
cin>>n;
cout<<fiboArray(n)<<endl;
}
int fiboArray(int n)
{
if(n==1)
return 1;
else if(n==2)
return 1;
else
return fiboArray(n-1)+ fiboArray(n-2);
}
declare @i bigint
declare @j bigint
declare @k int
declare @sum bigint
set @i = 1
set @j = 0
set @k = 2
set @sum = 0
while @k < 94
begin
set @sum = @i + @j
print @sum
set @i = @j
set @j = @sum
set @k = @k +1
end
end
用numeric(38,0)也只能计算到160,考虑溢出问题可能就有难度了不过总体来说,这样的题对SQL没多大意义
begin
declare @i numeric(38,0)
declare @j numeric(38,0)
declare @k numeric(38,0)
declare @sum numeric(38,0)
set @i = 1
set @j = 0
set @k = 2
set @sum = 0
while @k < 100
begin
set @sum = @i + @j
print @sum
print @k
set @i = @j
set @j = @sum
set @k = @k +1
end
end--这个@K在100以内可以!
/*
51680708854858323072
97
83621143489848422977
98
135301852344706746049
99
还花了我一个小时写了一堆SQL
交完卷我也就拒绝了
PL/SQL程序
declare i number :=0;
j number :=1;
x number :=1;
begin
while x<1000
loop
dbms_output.put_line(x);
x:=i+j;
i:=j;
j:=x;
end loop;
end;斐波那契数列
--n以下的斐波那契数求和
DECLARE @i INT
DECLARE @j INT
DECLARE @n INT
DECLARE @sum INTSET @i=0
SET @j=1
SET @n=10000
SET @sum=0WHILE (@j<@n)
BEGIN
SET @sum=@sum+@j
SET @j=@j+@i
SET @i=@j-@i
ENDPRINT @sum
最后到华为那边,被PASS了
2006年 参加过一次文思的考试,考试卷好几页
什么智力题,什么英语题,什么sql题,什么C编程题
来个递归的.
alter proc up_ff
@a decimal(18,0)
,@ret decimal(18,0) output
as
begin
declare @sql nvarchar(max);
declare @s decimal(18,0);
declare @s1 decimal(18,0);
set @s=0;
set @s1=0;
set @sql=' exec up_ff @w,@x output';
if @a=1 or @a=2
set @ret=1;
else
begin
set @a-=1
execute sp_executesql @sql,N'@w decimal(18,0),@x decimal(18,0) output',@a,@s output;
set @a-=1;
execute sp_executesql @sql,N'@w decimal(18,0),@x decimal(18,0) output',@a,@s1 output;
set @ret=@s1+@s;
end
end
(
select id=1, v1=convert(numeric(38,0),0), v2=convert(numeric(38,0),1) union all
select id=2, v1=convert(numeric(38,0),a.v2),v2=convert(numeric(38,0),a.v1+a.v2) FROM cte a
)
SELECT v2 FROM cte OPTION (MAXRECURSION 100);/*
v2
---------------------------------------
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
......
135301852344706746049
218922995834555169026
354224848179261915075
573147844013817084101消息 530,级别 16,状态 1,第 1 行
语句被终止。完成执行语句前已用完最大递归 100。
*/
set @i = 0
;with cte as
(
select id=1, v1=@i, v2=@i+1 union all
select id=id+1, v1=v2,v2=v1+v2 FROM cte where id < 100
)
SELECT v2 FROM cte
select 1 id,convert(decimal(38,0),1) fb1,convert(decimal(38,0),1) fb2
union all
select id+1,fb2,fb1+fb2 from cte where id<100
)select * from cte
-- 公式法。。
declare @z float,@x float,@y float ,@n int
set @n=45set @z= sqrt(5.0)
set @x=(1+@z)/2
set @y=(1-@z)/2select Convert(bigint,(power(@x,@n) - power(@y,@n))/@z )
set @n=1000set @z= sqrt(5.0)
set @x=(1+@z)/2
set @y=(1-@z)/2select (power(@x,@n) - power(@y,@n))/@z -- 4.34665576869389E+208
--试了下 bigint 只能到91
declare @P int ,@sum bigint,@tmp bigint,@S bigint
select @P =1, @sum =0
while @P <= 91
begin
if @P = 1
begin
set @sum = 1
set @S = 1
set @tmp = 1
end
else
begin
set @tmp = @sum
set @sum = @sum + @S
set @S = @tmp
end
print @sum
set @P =@P + 1
end--应该像31楼说的那样 用表处理
with cte as
(
select id=1, v1=convert(numeric(38,0),0), v2=convert(numeric(38,0),1) union all
select id=2, v1=convert(numeric(38,0),a.v2),v2=convert(numeric(38,0),a.v1+a.v2) FROM cte a
)
SELECT v2 FROM cte OPTION (MAXRECURSION 100);
select 1 id,convert(decimal(38,0),1) fb1,convert(decimal(38,0),1) fb2
union all
select id+1,fb2,fb1+fb2 from cte where id<100
)select * from cte