-- 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

解决方案 »

  1.   

    还是这个好:
    #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);
    }
      

  2.   

    -- 是的,就是文思!(算了一下,就算是定义为 bigint 类型,也会存在数据溢出问题,最多只能循环94次)begin
      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
      

  3.   

    溢出了
    用numeric(38,0)也只能计算到160,考虑溢出问题可能就有难度了不过总体来说,这样的题对SQL没多大意义
      

  4.   

    嗯! SQL的话应该要转换数据类型来做。
      

  5.   


    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
      

  6.   

    呵呵 经历这么多面试,也只见过文思这么面,
    还花了我一个小时写了一堆SQL
    交完卷我也就拒绝了
      

  7.   


    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;斐波那契数列
      

  8.   

    文思,拒绝过我的,只能说明我没有能力,打死我也不去了怎么是SQL SERVER的题?
      

  9.   


    --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
      

  10.   

    额,我上次去,直接面试,2个人和我谈了好多。没要笔试。
    最后到华为那边,被PASS了
      

  11.   

    文思?
    2006年 参加过一次文思的考试,考试卷好几页
    什么智力题,什么英语题,什么sql题,什么C编程题
      

  12.   


    来个递归的.
    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
      

  13.   

    试了下 把类型改为numeric(38,0) 就不会溢出
      

  14.   

    不知道这是不是出题者希望的答案: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);/*
    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。
    */
      

  15.   

    修正一下#31,更简洁一点declare @i numeric(38,0)
    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
      

  16.   

    ;with cte as(
    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
      

  17.   


    -- 公式法。。
    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 )
      

  18.   

    declare @z float,@x float,@y float ,@n int
    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
      

  19.   

    语法看不太懂,俺只能看看思路,呵呵,学习了~luoyoumou加油
      

  20.   

    很久没用过sql的飘过[email protected]
      

  21.   


    --试了下  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);
      

  22.   

    ;with cte as(
    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