nyist 13 Fibonacci數

來源:互聯網
上載者:User

標籤:os   io   資料   for   div   時間   amp   ios   

Fibonacci數時間限制:3000 ms  |  記憶體限制:65535 KB難度:1 
描述
無窮數列1,1,2,3,5,8,13,21,34,55...稱為Fibonacci數列,它可以遞迴地定義為
F(n)=1 ...........(n=1或n=2)
F(n)=F(n-1)+F(n-2).....(n>2)
現要你來求第n個斐波納奇數。(第1個、第二個都為1)
 
輸入
第一行是一個整數m(m<5)表示共有m組測試資料
每次測試資料只有一行,且只有一個整形數n(n<20)
輸出
對每組輸入n,輸出第n個Fibonacci數
範例輸入
3135
範例輸出
125





 

 

 

 

#include<stdio.h>
int main()
{
int a[10000],m,n,i;
a[0]=1;
a[1]=1;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
}
printf("%d\n",a[n-1]);
}
return 0;
}

 


************************************

 

 

#include <stdio.h>
int main( )
{ int m,n,i;
int a[20]={1,1,2,3} ;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=2;i<n;i++) // for(i=2;i<=n;i++)
{
a[i]=a[i-1]+a[i-2];
}
printf("%d\n",a[n-1]);
}

}


******************************************

 

#include<iostream>
using namespace std;
int main()
{
int i,a[20],m,t;
a[1]=1; a[2]=1;
for(i=3;i<=20;i++) a[i]=a[i-2]+a[i-1];// 計算前20個數

cin>>m;
while(m--)
{
cin>>t;
cout<<a[t]<<endl;
}
//while(1);
return 0;
}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.