08-06科大讯飞开发笔试
第一题:给出以“ ”隔开的字符串,找出字符串中包含‘e’的单词个数
第二题:给出四个点,计算四边形的面积。
static class Point{
int x;
int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
Point p3 = new Point(1,3);
Point p4 = new Point(0,2);
long area = getArea(p1,p2,p3,p4);
System.out.println(area);
}
private static long getArea(Point p1, Point p2, Point p3, Point p4) {
// 两点的长度:√((x1-x2)^2 + (y1-y2)^2))
double len12 = Math.sqrt(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2));
double len23 = Math.sqrt(Math.pow(p2.x - p3.x,2) + Math.pow(p2.y - p3.y,2));
double len34 = Math.sqrt(Math.pow(p3.x - p4.x,2) + Math.pow(p3.y - p4.y,2));
double len41 = Math.sqrt(Math.pow(p4.x - p1.x,2) + Math.pow(p4.y - p1.y,2));
// 利用这条边将四边形切割开
double len24 = Math.sqrt(Math.pow(p4.x - p2.x,2) + Math.pow(p4.y - p2.y,2));
// 海伦公式:半周长:k=(a+b+c)/2, 面积:s = √(k(k-a)(k-b)(k-c))
double k1 = (len41 + len12 + len24) / 2;
double k2 = (len23 + len34 + len24) / 2;
double s1 = Math.sqrt(k1 * (k1 - len41) * (k1 - len12) * (k1 - len24));
double s2 = Math.sqrt(k2 * (k2 - len23) * (k2 - len34) * (k2 - len24));
// 这里加0.5是为了四舍五入
return (long)(s1 + s2 + 0.5);
} 第三题:lc115,不同的子序列public static void main(String[] args) {
String str = "iflytekiflytek";
int res = findIflytek(str);
System.out.print(res);
}
public static int findIflytek (String str) {
// write code here
String s = "iflytek";
int n = str.length();
int m = s.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= n; i++) dp[0][i] = 1;
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(s.charAt(i - 1) == str.charAt(j - 1)){
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
}else{
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[m][n];
} 
查看13道真题和解析