全红求助!

P1255 数楼梯

Ghy_boy @ 2024-04-21 08:09:07

#include <bits/stdc++.h>
using namespace std;
long long louties[4]={0,1,2,3};
int louti , v = 0 , v1 ,v2;
void dfs(int d){
    if(d == louti){
        cout << v << endl;
        return ;
    }
    else{
        if(d == 3){
            v1 = louties[d];
            v2 = louties[d - 1];
        }else{
            int v3 = v1;
            v1 = v;
            v2 = v3;
        }
        v += (v1 + v2);
        dfs(d + 1);
    }
}
int main(){
    cin >> louti;
    if(louti <4){cout << louties[louti - 1];return 0;}
    dfs(3);
}

by _d_h_f @ 2024-04-21 09:01:03

首先递归绝对是过不了的,考虑考虑递推。


by AAAuc03 @ 2024-04-21 09:12:07

没必要,考虑考虑高精度


by liukexu1218 @ 2024-04-21 09:22:47

#include <bits/stdc++.h>
using namespace std;
long long louties[4]={0,1,2,3};
int louti , v = 0 , v1 ,v2;
void dfs(int d){
    if(d == louti){
        cout << v << endl;
    }
    else{
        if(d == 3){
            v1 = louties[d];
            v2 = louties[d - 1];
        }else{
            int v3 = v1;
            v1 = v;
            v2 = v3;
        }
        v =v+ (v1 + v2);
        dfs(d + 1);
    }
}
int main(){
    cin >> louti;
    if(louti <4){
        cout << louties[louti - 1];
        return 0;
    }
    dfs(3);
    return 0;
}

试试这样


by kkk02 @ 2024-04-21 09:53:37

#include <bits/stdc++.h>
using namespace std;
long long louties[5005]={1,1,2,3};
int louti , v1 ,v2;
void dfs(int d){
    if(d>louti){
        cout << louties[d-1] << endl;
        return;
    }
    louties[d] = (louties[d - 2] + louties[d - 1]);    
    dfs(d + 1);
}
int main(){
    cin >> louti;
    if(louti <4){cout << louties[louti - 1];return 0;}
    dfs(4);
}

改了一下,50分 满分要用高精度


|