TLE???

P1255 数楼梯

AC_kkk0926 @ 2024-01-28 11:30:03

求改

TLE了5个,AC了5个

#include <bits/stdc++.h>
using namespace std;
int ac(int x){
    if(x==1){
        return 1;
    }
    else if(x==2){
        return 2;
    }
    else{
        return ac(x-1)+ac(x-2);
    }
}
int main(){
    int a;
    cin>>a;
    cout<<ac(a);
    return 0;
}

by _QyGyQ_ @ 2024-01-28 11:31:20

这肯定炸,建议记忆化或者直接for递推


by _QyGyQ_ @ 2024-01-28 11:31:31

@AC_kkk0926


by AC_kkk0926 @ 2024-01-28 11:32:51

@meng_cen 额(⊙﹏⊙)不懂......


by _QyGyQ_ @ 2024-01-28 11:39:05

@AC_kkk0926

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+7;
int v[N];
int main(){
    v[1]=1;
    v[2]=2;
    int n;
    cin>>n;
    for(int i=3;i<=n;i++){
        v[i]=v[i-1]+v[i-2];
    }
    cout<<v[n];
    return 0;
}

这是大概思路,不过只有50分,100分要高精度


by AC_kkk0926 @ 2024-01-28 11:45:33

@meng_cen 我不会高精度


by _QyGyQ_ @ 2024-01-28 12:00:50

啊,这


by _QyGyQ_ @ 2024-01-28 12:02:00

@AC_kkk0926 建议去学一下P1601,题解会说明原理


by AC_kkk0926 @ 2024-01-28 18:50:13

@meng_cen ok


|