1972-斐波那契數列

【C系列4.4】函數訓練之斐波那契數列 1972

Time Limit:  1 s      Memory Limit:   128 MB
Submission:202     AC:154     Score:10.00

 

Description

今天mwy老師教了cyn小朋友斐波那契數列,cyn表示很好奇,於是他決定深入研究一下,你能幫幫他嗎?(主函數已經給出,如果提交的不是c語言則需要提交全部代碼)

#include<stdio.h>

int f(int  n);
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       int n;
        scanf("%d",&n);
        printf("%d\n ",f(n));
    }
    return 0;
}

Input

第一行輸入一個T,表示有T組數據。

接下來有T行,每行只有一個整數n(1 <= n <=30),代表要找到斐波那契數列的第n個數字。

Output

對於每一個n,輸出其對應的數字。

Samples

input:
4
1
2
3
4
output:
1
1
2
3


下附AC代碼:
#include<stdio.h>
int  f(int  n);
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long  n;
        scanf("%d",&n);
        printf("%d\n",f(n));
    }
    return 0;
}


int f(int  n) {
	return (1 == n || 2 == n) ? 1 : (f(n - 1) + f(n - 2));
}

原題鏈接:http://acm.hznu.edu.cn/OJ/problem.php?cid=1092&pid=5

發佈了53 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章