1. 程式人生 > >美團校招筆試題

美團校招筆試題

大富翁遊戲(類似於變態跳青蛙問題)

#include <iostream>
using namespace std;
int main()
{
    int n, ans;
    cin>>n;
    if(n<0)
        return 0;
    ans=1;
    for(int i =1;i<n;i++)
        {
        ans*=2;
    }
    cout<<ans;
    return 0;
}


找零錢,動態規劃

#include <iostream>
#include <cstring>
using namespace std;

int a[]={1,5,10,20,50,100};
long long f[6][10001];
int main()
    {
    int n;
    cin>>n;
    memset(f,0,sizeof(f));
    for(int i=0;i<=n;i++)
        f[0][i]=1;
    for(int i=1;i<6;i++)
        {
        for(int j=1;j<=n;j++)
            {
            for (int k=0;k<=(j/a[i]);k++)
                f[i][j]+=f[i-1][j-k*a[i]];
        }
    }
    cout<<f[5][n];
    return 0;
}
最長公共子序列問題:動態規劃
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    string s1, s2;
    while (getline(cin, s1), getline(cin, s2)){
        int l1 = s1.size();
        int l2 = s2.size();
        vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0));
        int result = 0;
        for (int i = 1; i <= l1; i++){
            for (int j = 1; j <= l2; j++){
                if (s1[i - 1] == s2[j - 1]){
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    result = max(dp[i][j], result);
                }
                else{
                    dp[i][j] = 0;
                }
            }
        }
        cout << result << endl;
    }
    return 0;
}