[南阳OJ-No.13]Fibonacci数|无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为 F(n)=1 ...........(n=1或

本文介绍了一种使用不同编程语言实现斐波那契数列的方法,并通过比较多种实现方式的时间和空间效率,提供了从递归到迭代的优化方案。

南阳OJ-No.13

时间限制:3000ms,空间限制:65535KB

描述

无穷数列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数

样例输入

3
1
3
5

样例输出

1
2
5


java

**时间46,内存311**

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int temp;

        for (int n=0; n<a; n++) {
            temp = cin.nextInt();

            System.out.println(Fibonacci(temp));
        }
    }

    public static int Fibonacci(int n) {
        if (n == 1) {
            return 1;
        }else if (n == 2){
            return 1;
        } else {
            return Fibonacci(n-1) + Fibonacci(n-2);
        }
    }
}

**时间25,内存311**

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int temp;

        for (int n=0; n<a; n++) {
            temp = cin.nextInt();

            System.out.println(Fibonacci(temp));
        }
        /***/
        cin.close();
        /***/
    }

    public static int Fibonacci(int n) {
        if (n == 1) {
            return 1;
        }else if (n == 2){
            return 1;
        } else {
            return Fibonacci(n-1) + Fibonacci(n-2);
        }
    }
}

主动关闭Scanner流会节省时间!!!

**时间24,内存311**

import java.util.Scanner;

public class Main_13 {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int temp;

        while (a>0) {
            temp = cin.nextInt();

            System.out.println(Fibonacci(temp));
            a--;
        }

        cin.close();
    }

    public static int Fibonacci(int n) {
        if (n == 1) {
            return 1;
        }else if (n == 2){
            return 1;
        } else {
            return Fibonacci(n-1) + Fibonacci(n-2);
        }
    }
}

将for改成while节省1时间!!!

**时间3,内存61**
依旧是用户名为 Bryan 的一大神给出的算法,投币看代码~

import java.util.Scanner;

public class Main {
    public static Scanner cin=new Scanner(System.in);

    public static void main(String[] args) {
        int line = 0;
        int number = cin.nextInt();

        for(int i = 0; i < number; i++){    
            line = cin.nextInt();
            System.out.println(f(line));
        }
    }

    static int f(int i){
        if (i <= 2 && i >= 0 ) {
            return 1;
        }
        return f(i - 1) + f(i - 2);
    }
}

将数据转换为 static 类型的重要性!!!(且Scanner主动关闭与否不影响时间)

**时间1,内存61**
Google出来的算是最高效的算法了,2017.2.3标记下,看不懂~

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException{
        int lineNum = Main.readInt();
        int f1 = 1;
        int f2 = 1;
        int fn = 0;
        for(int i = 0; i < lineNum; i++){
            int num = Main.readInt();
            if(num <= 2){
                System.out.println(f1);
            }else{
                f1 = 1;
                f2 = 1;
                for(int j = 3; j <= num; j++){
                    fn = f1 + f2;
                    f1 = f2;
                    f2 = fn;
                }
                System.out.println(fn);
            }
        }
        in.close();
    }
    public static int readInt() throws IOException{
        String str = in.readLine();
        int num = Integer.parseInt(str);
        return num;     
    }
}

c++

时间0,内存240

#include<iostream> 
using namespace std;

int f(int i)
{
    if (i <= 2 && i >= 0 ) {
        return 1;
    }
    return f(i - 1) + f(i - 2);
}

int main()
{
  int a;
  cin >> a;
  int temp;

  for(int i = 0; i < a; i++){   
        cin >> temp;
        cout << f(temp) << endl;
    }
}
### 构造函匹配错误分析 在 C++ 中,当遇到 `no matching function for call` 的错误提示时,通常是因为构造函的签名与实际调用时不一致。对于 `CShape` 类及其子类(如 `CRectangle`),如果存在双精度浮点 (`double`) 参的构造函调用失败的情况,则可能是以下几个原因造成的: 1. **缺少合适的构造函定义** 如果 `CShape` 其派生类未提供接受两个 `double` 参的构造函,则会引发此错误。 2. **基类构造函未显式调用** 当派生类对象创建时,默认情况下不会自动调用基类的默认构造函以外的其他构造函。因此,需要通过初始化列表显式调用所需的基类构造函[^1]。 3. **类型转换问题** 即使提供了接收 `double` 参的构造函,但如果参类型无法隐式转换为期望的类型,也可能导致匹配失败。 以下是针对该问题的具体解决方案以及代码示例。 --- ### 解决方案 #### 定义支持 `double` 参的构造函 确保 `CShape` 及其派生类具有能够处理 `double` 参的构造函。例如: ```cpp class CShape { public: // 支持 double 参的构造函 explicit CShape(double param1, double param2) : m_param1(param1), m_param2(param2) { std::cout << "CShape Constructor called with doubles." << std::endl; } protected: double m_param1, m_param2; }; // 派生类 CRectangle 继承自 CShape class CRectangle : public CShape { public: // 显式调用基类构造函 CRectangle(double width, double height) : CShape(width, height) { std::cout << "CRectangle Constructor called." << std::endl; } }; ``` 在此实现中: - 基类 `CShape` 提供了一个带两个 `double` 参的构造函- 子类 `CRectangle` 使用初始化列表显式调用了基类的构造函[^1]。 --- #### 调整虚析构函 为了确保动态分配的对象能够在运行时正确释放内存并调用相应的析构函,应将基类的析构函声明为虚函。这已在引用中提到过: ```cpp virtual ~CShape() { std::cout << "CShape Destructor called." << std::endl; } ``` 这样可以保证即使通过基类指针删除派生类对象时,也会调用正确的析构函链。 --- #### 测试代码 下面是一个完整的测试场景,验证上述更改后的行为是否正常工作: ```cpp #include <iostream> using namespace std; class CShape { public: explicit CShape(double param1, double param2) : m_param1(param1), m_param2(param2) { cout << "CShape Constructor called with (" << param1 << ", " << param2 << ")." << endl; } virtual ~CShape() { cout << "CShape Destructor called." << endl; } protected: double m_param1, m_param2; }; class CRectangle : public CShape { public: CRectangle(double width, double height) : CShape(width, height) { cout << "CRectangle Constructor called with (" << width << ", " << height << ")." << endl; } ~CRectangle() override { cout << "CRectangle Destructor called." << endl; } }; int main() { CRectangle rect(5.0, 10.0); return 0; } ``` 输出结果如下: ``` CShape Constructor called with (5, 10). CRectangle Constructor called with (5, 10). CRectangle Destructor called. CShape Destructor called. ``` --- ### 注意事项 1. 若仍出现构造函匹配错误,请仔细检查调用处的实际参类型是否完全匹配目标构造函的要求。 2. 对于复杂继承体系中的多态对象管理,始终建议使用智能指针(如 `std::unique_ptr` 和 `std::shared_ptr`)替代原始指针,以减少资源泄漏风险[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值