@CPP

bc03 黄金三 2022-11-05 15:49:28 2022-11-05 19:15:32 2

6043超时怎么优化啊?


#include              <iostream>



using namespace std;



int f(int n) {


    if (n == 1) {


        return 1;


    }


    if (n == 2) {


        return 1;


    }


    return f(n - 1) + f(n - 2);


};


int main() {




    int n;




    cin >> n;




    cout << f(n);




    return 0;




}

Time limid exceeded 80

{{ vote && vote.total.up }}

共 3 条回复

bc03 黄金三

CPP 刷题王

数组做

for (int i = 3; i <= 46; i++) {
	a[i] = a[i - 1] + a[i - 2];
}

2个变量:

for (int i = 3; i <= n; i++) {
	c = a + b;
	a = b;
	b = c;
}

十年OI一场空,不开long long见祖宗

CPP 刷题王

###你递归不超时就怪了 递推公式了解一下