科大讯飞8.6 A卷笔试编程题 全AC
第一题
public int findEwordCount (String string) { int countE = 0; for (String word : string.split(" ")) { if (word.contains("e")) { countE++; } } return countE; }
第二题
四边形切成两个三角形就行。当时在想选择的两个三角形面积可能重叠。后来看到题目里有写四个点顺序连接可以构成四边形,所以选p1, p2, p3和p1,p3,p4。这样面积就不会重叠。四舍五入直接加0.5就行。三角形面积计算用海伦公式即可。
class Solution { public long getArea (Point p1, Point p2, Point p3, Point p4) { double area1 = calTri(p1, p2, p3); double area2 = calTri(p1, p3, p4); return (long) (area1 + area2 + 0.5); } private double calTri(Point p1, Point p2, Point p3) { double a = calLen(p1, p2); double b = calLen(p1, p3); double c = calLen(p2, p3); double p = (a + b + c) / 2; return Math.sqrt(p * (p - a) * (p - b) * (p - c)); } private double calLen(Point p1, Point p2) { int x = p1.x - p2.x; int y = p1.y - p2.y; return Math.sqrt(x * x + y * y); } }
第三题
dp没想出来。用的记忆化搜索+DFS。
class Solution { String str; public int findIflytek (String _str) { str = _str; return dfs(0, 'i'); } private int MOD = 10000; private int[] memo = new int[70001]; private int dfs(int i, char ch) { if (str.length() == i) { return 0; } int hash = i; if (ch == 'f') hash += MOD; else if (ch == 'l') hash += 2 * MOD; else if (ch == 'y') hash += 3 * MOD; else if (ch == 't') hash += 4 * MOD; else if (ch == 'e') hash += 5 * MOD; else if (ch == 'k') hash += 6 * MOD; if (memo[hash] != 0) return memo[hash]; int count = 0; if (str.charAt(i) == ch) { if (ch == 'i') count += dfs(i + 1, 'f'); else if (ch == 'f') count += dfs(i + 1, 'l'); else if (ch == 'l') count += dfs(i + 1, 'y'); else if (ch == 'y') count += dfs(i + 1, 't'); else if (ch == 't') count += dfs(i + 1, 'e'); else if (ch == 'e') count += dfs(i + 1, 'k'); else if (ch == 'k') ++count; } int ans = count + dfs(i + 1, ch); memo[hash] = ans; return ans; } }#科大讯飞##笔试##Java##秋招##科大讯飞笔试是种什么体验#