科大讯飞(算法岗0912)
1. 泰勒
double mysin(double x) { double p = 0.000001, g = 0, t = x; int n = 1; do { g += t; n++; t = -t*x*x / (2 * n - 1) / (2 * n - 2); } while (fabs(t) >= p); return g; } int main() { double r, s, k; cin >> r >> s; if (r * r <= s*s) { k = sqrt(mysin(r) * mysin(r) + mysin(s)*mysin(s)); } else { k = mysin(r*s) / 2; } cout << k; return 0; }
2. 错误率
int helper(vector<string>& s1, vector<string>& s2) { int s1l = s1.size(); int s2l = s2.size(); vector<vector<int>> dp(s1l, vector<int>(s2l, 0)); for (int i = 0; i < s1l; i++) { dp[i][0] = i; } for (int i = 0; i < s2l; i++) { dp[0][i] = i; } for (int i = 1; i < s1l; i++) { for (int j = 1; j < s2l; j++) { if (s1[i] == s2[j]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = min({ dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] }) + 1; } } } return dp[s1l - 1][s2l - 1]; }
3. 木棍与正方形
int helper(vector<int>& num, int avg) { int res = 0; for (auto n : num) { res += abs(n - avg); } return res; } int main() { vector<int> num; int tmp, sum = 0; do { cin >> tmp; sum += tmp; num.push_back(tmp); } while (getchar() != '\n'); int avg = ceil(sum / num.size()); int res = min(helper(num, avg), helper(num, avg - 1)); cout << res; getchar(); return 0; }