做科大讯飞的时候,我真是脑子瓦特了,点到第三题的时候,提交运行,结果把编程题子卷全部提交了,前面两道题还没看,敲,虽然很是无能狂怒,不过还是好好看看这些题吧,这些题是我线下思考了一下写的,没有线上测试过,不能保证AC,有发现问题的同学请提出宝贵的意见:blush:
编程题1
直接暴力循环判断吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Solution { public int calcE(String str) { int res = 0, temp=0; char[] strs = str.toCharArray(); for (int i = 0; i < strs.length; i++) { if (strs[i]=='e' || strs[i] == 'E') temp = 1; if (strs[i] = ' ' && temp == 1) { temp = 0; res++; } } return res; } }
|
不保证AC
编程题2
将四边形分为两个三角形,然后计算两个三角形面积之和,三角形面积计算通过海伦公式
海伦公式,假设三角形边长分别是a,b,c,则有
$$
s = \frac{a+b+c}{2} \
S_\Delta = \sqrt{s(s-a)(s-b)(s-c)}
$$
最后将double
转换为int
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Solution { public int calcArea(int[] point1, int[] point2, int[] point3, int[] point4) { double temp = calcTrangle(point1, point2, point3) + calcTrangle(point2, point3, point4); return (int)Math.ceil(temp); }
public double calcTrangle(int[] point1, int[] point2, int[] point3) { double a = calcDistance(point1, point2); double b = calcDistance(point2, point3); double c= calcDistance(point1, point3); double s = (a+b+c)/2; return Math.sqrt(s*(s-a) * (s-b) * (s-c));
}
public double calcDistance(int[] point1, int[] point2) { return Math.sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])); }
|
不保证AC
编程题3==宕机==
我也宕机了