题目一描述
给定一个字符串,该字符串的组成为数组或者数字,求数组的层数。
例如:给定[4,[2,5],[3,6,[8,9]]]
该字符串返回的结果为3
题目一分析
对于字符串可以有很多办法,自己选择的是对字符串中的‘[’和‘]’进行处理,使用一个栈记录出现字符’[’,记录栈的大小,当栈中元素变大时替换掉记录的数值。
题目中:先将当前的字符拆分,如果是’[‘就入栈并记录栈的大小和更新记录值,如果是’]'就弹出栈,因此能够记录数组的深度即层数。
代码如下
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class GameString {
public static void main(String[] args) {
try {
//使用数据输入流将控制台的数据读入
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String readLine = bf.readLine();
Stack<Character> stack = new Stack<>();
int count = 0;
//循环将字符串的每一个字符进行判断
for (int i = 0; i < readLine.length(); i++) {
char ch = readLine.charAt(i);
//遇到当前字符入栈并更新记录值
if (ch == '[') {
stack.push(ch);
count = count > stack.size() ? count : stack.size();
} else if (ch == ']') {
stack.pop();//出栈表示内层的数组已经处理过
}
}
System.out.println(count);
} catch (Exception ex) {
}
}
}
题目二描述
给定空间中三个点的坐标,求另外一点距离三个点组成的平面的距离。
例如给定点:0 0 0 0 1 0 1 0 0 0 0 1
每三个数组表示一个点的个数,一共十二个数据。
返回的结果为1,因为前三个点组成的平面为xy坐标系,点p在z轴上。,因此z轴的左标为距离。
题目二分析
平面方程Ax+By+Cz+D = 0
根据博客中的计算过程
这算是数学中的一个计算过程,使用行列式中的一些知识来计算每个参数的值。对于三个点能够求出两个相交的向量(x3-x1,y3-y1,z3-z1)和(x2-x1,y2-y1,z2-z1),根据两个向量求的平面的参数相关的行列式:
代码如下
public class GameDevelop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Point Apoint = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
Point Bpoint = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
Point Cpoint = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
Point Ppoint = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
int[] res = getParam(Apoint, Bpoint, Cpoint);
//根据参数值计算距离
int b = res[0] * res[0] + res[1] * res[1] + res[2] * res[2];
int c = res[0] * Ppoint.x + res[1] * Ppoint.y + res[2] * Ppoint.z + res[3];
System.out.println(b);// 1
System.out.println(c);// 5
double d = (Math.abs(res[0] * Ppoint.x + res[1] * Ppoint.y + res[2] * Ppoint.z + res[3]))
/ Math.sqrt((res[0] * res[0] + res[1] * res[1] + res[2] * res[2]));
int dis = (int) Math.floor(d);
System.out.println(dis);
}
//根据三个点获取参数值
public static int[] getParam(Point Apoint, Point Bpoint, Point Cpoint) {
int[] res = new int[4];
res[0] = (Bpoint.y - Apoint.y) * (Cpoint.z - Apoint.z) - (Bpoint.z - Apoint.z) * (Cpoint.y - Apoint.y);
res[1] = (Cpoint.x - Apoint.x) * (Bpoint.z - Apoint.z) - (Bpoint.x - Apoint.x) * (Cpoint.z - Apoint.z);
res[2] = (Bpoint.x - Apoint.x) * (Cpoint.y - Apoint.y) - (Cpoint.x - Apoint.x) * (Bpoint.y - Apoint.y);
res[3] = -(res[0] * Apoint.x + res[1] * Apoint.y + res[2] * Apoint.z);
return res;
}
}
//定义点的类型
class Point {
int x;
int y;
int z;
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}