3/26 NEFUOJ题目

函数阶乘
Problem:824
Time Limit:1000ms
Memory Limit:65536K
Description
从键盘输入一个数值n(n <= 15),计算出数值n的阶乘。
Input
输入数据有多组,每组1个整数值n (0 <= n <=15)。
Output
对应输入数值的阶乘的值。
Sample Input
0
1
2
3
10
Sample Output
1
1
2
6
3628800
Hint
Source

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int ans=1;
       if(n==0)
       {
           cout<<"1"<<endl;
       }
       else
       {for(int i=n;i>0;i--)
       {
           ans=ans*i;
       }
       cout<<ans<<endl;}
    }
    return 0;
}

求斐波那契数函数版
Problem:827
Time Limit:1000ms
Memory Limit:65536K
Description
根据斐波那契递推关系,求解斐波那契数值
斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)
Input
输入数据有多组,一个正整数表示要求取的第几项斐波那契数值;(2 <= n <= 40)
Output
输出斐波那契数值
Sample Input
5
8
10
20
Sample Output
5
21
55
6765
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int f(int a)
{
    if(a==1)
    {
        return 1;
    }
    if(a==2)
    {
        return 1;
    }
    return f(a-1)+f(a-2);
}
int main()
{
    int n,ans=0;
    while(cin>>n)
    {
        cout<<f(n)<<endl;
    }
    return 0;
}

求斐波那契数函数版
Problem:827
Time Limit:1000ms
Memory Limit:65536K
Description
根据斐波那契递推关系,求解斐波那契数值
斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)
Input
输入数据有多组,一个正整数表示要求取的第几项斐波那契数值;(2 <= n <= 40)
Output
输出斐波那契数值
Sample Input
5
8
10
20
Sample Output
5
21
55
6765
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int f(int a)
{
    if(a==1)
    {
        return 1;
    }
    if(a==2)
    {
        return 1;
    }
    return f(a-1)+f(a-2);
}
int main()
{
    int n,ans=0;
    while(cin>>n)
    {
        cout<<f(n)<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值