360笔试题编程题
第一题,DNA匹配,思路就是找不同,上面是A下面是T,或者上面T下边A,然后两种情况,一次交换可以抵消一对,抵消不了的要替换,说白了就是找上A下T和上T下A的最大值
#include<bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int AT = 0, TA = 0; for(int i = 0; i < s1.size(); ++i) { if(s1[i] != s2[i] && s1[i] == 'A') ++AT; if(s1[i] != s2[i] && s1[i] == 'T') ++TA; } cout << (AT > TA ? AT : TA) << endl; return 0; }
思路就是dp(n,m) = Pa中 + Pa没中 * Pb没中 * Pb销毁的也没中 * dp(n, m-3) + Pa没中 * Pb没中 * Pb销毁的中了 * dp(n-1, m-2)
因为出现了m-1 m-2 n-1,所以临界情况要处理一下
#include<bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0; cin >> n >> m; vector<vector<double>> dp(n+1,vector<double>(m+1, 0.0)); for(int i = 1; i <= n; ++i) { double nf = i; dp[i][0] = 1.0; dp[i][1] = nf/(nf+1.0); dp[i][2] = nf/(2.0+nf) + 2.0/(2.0+nf)*(2.0-1.0)/nf*dp[i-1][0]; } for(int i = 1; i <= n; ++i) for(int j = 3; j <= m; ++j) { double nf = i, mf = j; dp[i][j] = nf/(nf+mf) + mf/(nf+mf)*(mf-1.0)/(nf+mf-1.0)*(mf-2.0)/(mf+nf-2.0)*dp[i][j-3] + mf/(nf+mf)*(mf-1.0)/(nf+mf-1.0)*nf/(mf+nf-2.0)*dp[i-1][j-2]; } cout << setprecision(4) << dp[n][m] << endl; return 0; }最后,选择题感觉挺离谱的,特别是找规律,我真的一点规律找不到,直接全选B