首页 > 其他分享 >blog_01

blog_01

时间:2022-09-27 18:36:12浏览次数:70  
标签:01 double System blog douArr && println out

第一次博客作业

目录

1.前言

 前两次的题目集比较简单,大多数题目是比较基础的字符串类型的题目,能带我们初步了解Java语言。而第三次题目集的题目相对于前两次的题目来说难度大幅度上升,尤其是需要理解并熟悉正则表达式的应用,如果用不来正则表达式,那么字符串的处理以及判断会很复杂。

2.设计与分析

(1)题目集2 7-2 串口字符解析

题目:
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5 ~ 8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5 ~ 8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。

输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111

输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error

试题分析:
 对于输入的数据流,需要从第一个零开始往后截取11位,并且判断该数据是否合法,若字符串本身长度小于11或者字符串全为"1",则输出"null data"。若数据合法,则其中第一位是起始位,第十位是奇偶校验位,第十一位是结束位,中间八位为有效数据。输出时若有n个数据,则输出n行,并且每行的开头得加上数据的序号(1~n)以及英文冒号。若某个数据的结束符不为1,则输出“validate error”;若某个数据奇偶校验错误,则输出“parity check error”;若数据结束符和奇偶校验均不合格,输出“validate error”。

源代码展示:

查看代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int start = 0, end = 11, count = 1, rem = 0, i, j;
        String s = "";
        if (end > str.length()) {
            System.out.println("null data");
            return;
        }
        for (i = 0, j = 0; i < str.length() && j < str.length(); ) {
            j = i;
            if ("0".charAt(0) == str.charAt(j) && j + 10 < str.length()) {
                rem = 0;
                s = str.substring(j + 1, j + 10);
                for (int k = 0; k < s.length(); k++) {
                    if ("1".charAt(0) == s.charAt(k)) {
                        rem++;
                    }
                }
                if ((rem % 2 == 0) && ("1".charAt(0) == str.charAt(j + 10))) {
                    System.out.println(count + ":parity check error");
                } else if ((rem % 2 != 0) && !("1".charAt(0) == str.charAt(j + 10))) {
                    System.out.println(count + ":validate error");
                } else if ((rem % 2 == 0) && !("1".charAt(0) == str.charAt(j + 10))) {
                    System.out.println(count + ":validate error");
                } else {
                    System.out.println(count + ":" + str.substring(j + 1, j + 9));
                }
                count++;
                j += 11;
                i = j;
            } else {
                i++;
            }
        }
        if (i == str.length() && count == 1) {
            System.out.println("null data");
            return;
        }
    }
}

SourceMonitor生成的报表内容:

img
img
img

代码分析总结:
 由于刚接触Java,还未形成写类的习惯,所以代码都堆到了主函数中,这种写法会使得代码的可读性和复用性很差。从上述的报表内容可以看出我本题的代码指标基本上都未达到被测量维度的期望值,因此还有很大的改动空间。

(2)题目集3 7-1 点线形系列1-计算两点之间的距离

题目:
输入两个点的坐标,计算两点之间的距离

输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。

输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951

试题分析:
 这题需要先判断输入的各个坐标是否合法,如果非法,则输出"Wrong Format";若合法,则进行坐标点数量的判断,如果坐标点大于两个,则输出“wrong number of points”,否则进行两点间距离的计算。

源代码展示:

查看代码

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] cArr = s.toCharArray();
        int flag = 0, flag2 = 0, count = 0;
        String St = "";
        for (int i = 0; i < cArr.length - 1; i++) {
            if (cArr[i] == '0') {
                for (int j = i; cArr[j] != ',' && cArr[j] != ' '; j++) {
                    St += cArr[j];
                }
                int num = 0;
                for (int j = 0; j < St.length(); j++) {
                    if (St.charAt(j) == '0') {
                        num++;
                    }
                }
                if (num == St.length()) {
                    System.out.println("Wrong Format");
                    return;
                }
            }
            if (cArr[i] == ',') {
                if ((i > 0 && (cArr[i - 1] == ',' || cArr[i + 1] == ',')) || i == 0 || flag2 > 1 || (i > 1 && cArr[i - 1] == '0')) {
                    System.out.println("Wrong Format");
                    return;
                }
                flag2 = 0;
                count++;
            }
            if (cArr[i] == '.') {
                flag2++;
                if ((i > 2 && ((cArr[i - 1] == '0' && cArr[i - 2] == '0') || cArr[i + 1] == '.')) || i == 0 || (i > 0 && (cArr[i - 1] == '0' && cArr[i + 1] == '0')) || (i > 0 && (cArr[i - 1] == '0' && cArr[i + 1] == ',')) || (i > 0 && (cArr[i - 1] == ' ' && cArr[i + 1] == '0')) || cArr[i + 1] == '.') {
                    System.out.println("Wrong Format");
                    return;
                }
            }
            if (cArr[i] == ' ') {
                flag++;
                if ((i > 2 && (cArr[i - 1] == '+' || cArr[i - 1] == '-' || cArr[i - 1] == ',' || cArr[i - 1] == '.')) || i < 3 || flag2 > 1) {
                    System.out.println("Wrong Format");
                    return;
                }
                flag2 = 0;
            }
            if ((cArr[i] == '+' || cArr[i] == '-') && (cArr[i + 1] == '+' || cArr[i + 1] == '-')) {
                System.out.println("Wrong Format");
                return;
            }
            if ((cArr[i] < '0' || cArr[i] > '9') && cArr[i] != ' ' && cArr[i] != ',' && cArr[i] != '.' && cArr[i] != '+' && cArr[i] != '-') {
                System.out.println("Wrong Format");
                return;
            }
        }
        if (count - flag > 1 || flag >= count) {
            System.out.println("Wrong Format");
            return;
        }
        if (count > 2 && flag > 1 && count - flag == 1) {
            System.out.println("wrong number of points");
            return;
        }
        String str = "[+-]?(\\d+(\\.\\d+)?)";
        Pattern pattern = Pattern.compile(str);
        Matcher matcher = pattern.matcher(s);
        double[] douArr = new double[4];
        String[] strArr = new String[4];
        int no = 0;
        while (matcher.find()) {
            strArr[no] = matcher.group(0);
            douArr[no] = Double.parseDouble(strArr[no]);
            no++;
        }
        System.out.println(Math.sqrt(Math.pow(douArr[0] - douArr[2], 2) + Math.pow(douArr[1] - douArr[3], 2)));
    }
}

SourceMonitor生成的报表内容:

img
img
img

代码分析总结:
当时还未形成写类的习惯,所以代码都堆到了主函数中,这种写法会使得代码的可读性和复用性很差。从上述的报表内容可以看出我本题的代码指标基本上都未达到被测量维度的期望值,因此还有很大的改动空间。

(3)题目集3 7-2 点线形系列2-线的计算

题目:
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。

输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",

输出格式:
见题目描述。

试题分析:
 本题需要先对输入的整个字符串进行格式上的判断,然后再对坐标/坐标的数量进行判断,之后再对相应的选项进行判断。

源代码展示:

查看代码
import java.util.Scanner;

public class pta_02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String str = "^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$";
        String sRem = s.substring(2);
        int end = sRem.length() - 1;
        if (sRem.charAt(end) == '-' || sRem.charAt(end) == '+' || sRem.charAt(end) == '.' || sRem.charAt(end) == ',' || sRem.charAt(end) == ' ' || s.charAt(0) == ' ') {
            System.out.println("Wrong Format");
            return;
        }
        if (sRem.indexOf(" -,") != -1 || sRem.indexOf(" +,") != -1 || sRem.indexOf("- ") != -1 || sRem.indexOf("+ ") != -1 || sRem.indexOf(" :") != -1 || sRem.indexOf(": ") != -1 || sRem.indexOf("  ") != -1 || sRem.indexOf("..") != -1 || sRem.indexOf(".,") != -1 || sRem.indexOf(",.") != -1 || sRem.indexOf(". ") != -1 || sRem.indexOf(" .") != -1 || sRem.indexOf(", ") != -1 || sRem.indexOf(" ,") != -1) {
            System.out.println("Wrong Format");
            return;
        }
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ',') {
                count++;
            }
        }
        double[] douArr = new double[1000];
        int no = 0;
        String[] sArr1 = s.substring(2).split(" ");
        for (int i = 0; i < sArr1.length; i++) {
            String[] sArr2 = sArr1[i].split(",");
            for (int j = 0; j < sArr2.length; j++) {
                if (!sArr2[j].matches(str)) {
                    System.out.println("Wrong Format");
                    return;
                } else {
                    douArr[no++] = Double.parseDouble(sArr2[j]);
                }
            }
        }

        String top_2 = s.substring(0, 2);
        switch (top_2) {
            case "1:":
                if (no != 4 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    CalculateSlope(new double[]{douArr[0], douArr[1], douArr[2], douArr[3]});
                break;
            case "2:":
                if (no != 6 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    VerticalDistance(new double[]{douArr[0], douArr[1], douArr[2], douArr[3], douArr[4], douArr[5]});
                break;
            case "3:":
                if (no != 6 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    DetermineStraightLine(new double[]{douArr[0], douArr[1], douArr[2], douArr[3], douArr[4], douArr[5]});
                break;
            case "4:":
                if (no != 8 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgingParallel(new double[]{douArr[0], douArr[1], douArr[2], douArr[3], douArr[4], douArr[5], douArr[6], douArr[7]});
                break;
            case "5:":
                if (no != 8 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    GetIntersectionCoordinates(new double[]{douArr[0], douArr[1], douArr[2], douArr[3], douArr[4], douArr[5], douArr[6], douArr[7]});
                break;
            default:
                System.out.println("Wrong Format");
                break;
        }
    }

    public static void CalculateSlope(double[] douArr) {
        double num = 0.0;
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        if (x1 == x2 && y1 == y2) {
            System.out.println("points coincide");
        } else {
            if (x1 != x2) {
                num = (y2 - y1) / (x2 - x1);
                System.out.println(num);
            } else {
                System.out.println("Slope does not exist");
            }
        }
    }

    public static void VerticalDistance(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
            System.out.println("points coincide");
            return;
        }
        if ((y1 - y2) / (x1 - x2) == (y2 - y3) / (x2 - x3)) {
            System.out.println("0.0");
            return;
        }
        double A, B, C;
        A = y3 - y2;
        B = x2 - x3;
        C = x3 * y2 - x2 * y3;
        double d = Math.abs(A * x1 + B * y1 + C) / Math.sqrt(Math.pow(A, 2) + Math.pow(B, 2));
        System.out.println(d);
    }

    public static void DetermineStraightLine(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
            System.out.println("points coincide");
            return;
        }
        if ((x1 == x2 && x2 == x3) || (y1 == y2 && y2 == y3)) {
            System.out.println("true");
            return;
        }
        if (((y2 - y1) / (x2 - x1)) == ((y3 - y1) / (x3 - x1))) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

    public static void JudgingParallel(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) {
            System.out.println("points coincide");
            return;
        }
        double k1 = (y2 - y1) / (x2 - x1);
        double k2 = (y4 - y3) / (x4 - x3);
        if (k2 == k1) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

    public static void GetIntersectionCoordinates(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double A1, B1, C1;
        A1 = y2 - y1;
        B1 = x1 - x2;
        C1 = x2 * y1 - x1 * y2;
        double A2, B2, C2;
        A2 = y4 - y3;
        B2 = x3 - x4;
        C2 = x4 * y3 - x3 * y4;
        double new_x = 0.0, new_y = 0.0;
        if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) {
            System.out.println("points coincide");
            return;
        }
        new_x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
        new_y = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1);
        if (x1 == x2 && y3 == y4) {
            if ((x3 < x4 && new_x > x3 && new_x < x4) || (x4 < x3 && new_x > x4 && new_x < x3) || (y1 < y2 && new_y > y1 && new_y < y2) || (y1 > y2 && new_y > y2 && new_y < y1)) {
                System.out.println(x1 + "," + y3 + " true");
                return;
            } else {
                System.out.println(x1 + "," + y3 + " false");
                return;
            }
        }
        if (y1 == y2 && x3 == x4) {
            if ((x1 < x2 && new_x > x1 && new_x < x2) || (x1 > x2 && new_x > x2 && new_x < x1) || (y3 < y4 && new_y > y3 && new_y < y4) || (y3 > y4 && new_y > y4 && new_y < y3)) {
                System.out.println(x3 + "," + y1 + " true");
                return;
            } else {
                System.out.println(x3 + "," + y1 + " false");
                return;
            }
        }
        double k1 = (y2 - y1) / (x2 - x1);
        double k2 = (y4 - y3) / (x4 - x3);
        if (k2 == k1 || (x1 == x2 && x3 == x4) || (y1 == y2 && y3 == y4)) {
            System.out.println("is parallel lines,have no intersection point");
        } else if ((x1 == x2 && x3 != x4) || (x1 != x2 && x3 == x4)) {
            if (x1 == x2 && x3 != x4) {
                if (((new_y - y1) * (new_y - y2) < 0) || (((new_x - x3) * (new_x - x4) < 0) && ((new_y - y3) * (new_y - y4) < 0))) {
                    System.out.println(new_x + "," + new_y + " true");
                } else {
                    System.out.println(new_x + "," + new_y + " false");
                }
            } else if (x1 != x2 && x3 == x4) {
                if (((new_y - y3) * (new_y - y4) < 0) || (((new_x - x1) * (new_x - x2) < 0) && ((new_y - y1) * (new_y - y2) < 0))) {
                    System.out.println(new_x + "," + new_y + " true");
                } else {
                    System.out.println(new_x + "," + new_y + " false");
                }
            }
        } else {
            if ((((new_x > x1 && new_x < x2) && (new_y > y2 && new_y < y1)) || ((new_y > y1 && new_y < y2) && (new_x > x2 && new_x < x1))) || (((new_x > x3 && new_x < x4) && (new_y > y4 && new_y < y3)) || ((new_y > y3 && new_y < y4) && (new_x > x4 && new_x < x3)))) {
                System.out.println(new_x + "," + new_y + " true");
            } else {
                System.out.println(new_x + "," + new_y + " false");
            }
        }

    }

}

SourceMonitor生成的报表内容:
img
img
img

代码分析总结:
 这一题我是先过判断符号格式是否错误,如输入的字符串末尾出现出现“+”、“-”、“.”、“,”或者整个字符串中出现“ -,”、“ +,”等情况时,输出"Wrong Format"。然后通过switch来进行选择,如果第一个字符不为1~5则输出"Wrong Format",否则进入对应的case进行相应的计算/判断。但是写的代码有点过于复杂,并且判断条件过于局限,所以还有待改进。

(4)题目集3 7-3 点线形系列3-三角形的计算

题目:
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"

输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0

选项4中所输入线的两个点坐标重合,输出"points coincide",

试题分析:
本题需要先对输入的整个字符串进行格式上的判断,然后再对坐标/坐标的数量进行判断,之后再对相应的选项进行判断/计算。

源代码展示:

查看代码
import java.math.BigDecimal;
import java.util.Scanner;

public class pta_03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String str = "^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$";
        String sRem = s.substring(2);
        int end = sRem.length() - 1;
        if (sRem.charAt(end) == '-' || sRem.charAt(end) == '+' || sRem.charAt(end) == '.' || sRem.charAt(end) == ',' || sRem.charAt(end) == ' ' || s.charAt(0) == ' ') {
            System.out.println("Wrong Format");
            return;
        }
        if (sRem.indexOf(" -,") != -1 || sRem.indexOf(" +,") != -1 || sRem.indexOf("- ") != -1 || sRem.indexOf("+ ") != -1 || sRem.indexOf(" :") != -1 || sRem.indexOf(": ") != -1 || sRem.indexOf("  ") != -1 || sRem.indexOf("..") != -1 || sRem.indexOf(".,") != -1 || sRem.indexOf(",.") != -1 || sRem.indexOf(". ") != -1 || sRem.indexOf(" .") != -1 || sRem.indexOf(", ") != -1 || sRem.indexOf(" ,") != -1) {
            System.out.println("Wrong Format");
            return;
        }
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ',') {
                count++;
            }
        }
        double[] douArr = new double[1000];
        int no = 0;
        String[] sArr1 = s.substring(2).split(" ");
        for (int i = 0; i < sArr1.length; i++) {
            String[] sArr2 = sArr1[i].split(",");
            for (int j = 0; j < sArr2.length; j++) {
                if (!sArr2[j].matches(str)) {
                    System.out.println("Wrong Format");
                    return;
                } else {
                    douArr[no++] = Double.parseDouble(sArr2[j]);
                }
            }
        }

        String top_2 = s.substring(0, 2);
        switch (top_2) {
            case "1:":
                if (no != 6 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType1(douArr);
                break;
            case "2:":
                if (no != 6 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    TriangleProperties(douArr);
                break;
            case "3:":
                if (no != 6 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType2(douArr);
                break;
            case "4:":
                if (no != 10 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    CalculateArea(douArr);
                break;
            case "5:":
                if (no != 8 || no % count != 0) {
                    if (no % 2 != 0 || no % count != 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    InsideTriangle(douArr);
                break;
            default:
                System.out.println("Wrong Format");
                break;
        }
    }

    public static void JudgmentType1(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if (IsDataError(douArr)) {
            System.out.println("data error");
            return;
        }
        double a, b, c;
        a = Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x3 - x2, 2));
        b = Math.sqrt(Math.pow(y3 - y1, 2) + Math.pow(x3 - x1, 2));
        c = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
        double diff1 = Math.abs(a - b);
        double diff2 = Math.abs(a - c);
        double diff3 = Math.abs(b - c);
        double standard = 1e-6;
        if ((diff1 <= standard) || (diff2 <= standard) || (diff3 <= standard)) {
            System.out.print("true ");
        } else {
            System.out.print("false ");
        }
        if (diff1 <= standard && diff2 <= standard && diff3 <= standard) {
            System.out.print("true");
        } else {
            System.out.print("false");
        }

    }

    public static void TriangleProperties(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if (IsDataError(douArr)) {
            System.out.println("data error");
            return;
        }
        double a, b, c;
        a = Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x3 - x2, 2));
        b = Math.sqrt(Math.pow(y3 - y1, 2) + Math.pow(x3 - x1, 2));
        c = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
        String perimeter = String.format("%.6f", a + b + c);
        double cosC = (a * a + b * b - c * c) / (2.0 * a * b);
        String area = String.format("%.6f", Math.sqrt(1 - Math.pow(cosC, 2)) * a * b * 0.5);
        String new_x = String.format("%.6f", (x1 + x2 + x3) / 3.0);
        String new_y = String.format("%.6f", (y1 + y2 + y3) / 3.0);
        System.out.println(Double.valueOf(perimeter) + " " + Double.valueOf(area) + " " + Double.valueOf(new_x) + "," + Double.valueOf(new_y));
    }

    public static void JudgmentType2(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if (IsDataError(douArr)) {
            System.out.println("data error");
            return;
        }
        double a, b, c;
        a = Math.pow(y3 - y2, 2) + Math.pow(x3 - x2, 2);
        b = Math.pow(y3 - y1, 2) + Math.pow(x3 - x1, 2);
        c = Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2);
        if (a + b < c || a + c < b || b + c < a) {
            System.out.print("true ");
        } else {
            System.out.print("false ");
        }
        if (a + b == c || a + c == b || b + c == a) {
            System.out.print("true ");
        } else {
            System.out.print("false ");
        }
        if (a + b > c && a + c > b && b + c > a) {
            System.out.print("true");
        } else {
            System.out.print("false");
        }
    }

    public static void CalculateArea(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double x5 = douArr[8], y5 = douArr[9];
        double[] triangleCoordinates = {x3, y3, x4, y4, x5, y5};
        if (x1 == x2 && y1 == y2) {
            System.out.println("points coincide");
            return;
        }
        if (IsDataError(triangleCoordinates)) {
            System.out.println("data error");
            return;
        }
        double A1 = GetA(y1, y2), B1 = GetB(x1, x2), C1 = GetC(x1, y1, x2, y2);
        double Aab = GetA(y3, y4), Bab = GetB(x3, x4), Cab = GetC(x3, y3, x4, y4);
        double Aac = GetA(y3, y5), Bac = GetB(x3, x5), Cac = GetC(x3, y3, x5, y5);
        double Abc = GetA(y4, y5), Bbc = GetB(x4, x5), Cbc = GetC(x4, y4, x5, y5);
        if (((A1 * x3 + B1 * y3 + C1 == 0) && (A1 * x4 + B1 * y4 + C1 == 0)) || ((A1 * x3 + B1 * y3 + C1 == 0) && (A1 * x5 + B1 * y5 + C1 == 0)) || ((A1 * x4 + B1 * y4 + C1 == 0) && (A1 * x5 + B1 * y5 + C1 == 0))) {
            System.out.println("The point is on the edge of the triangle");
            return;
        }
        double area = Area(triangleCoordinates);
        double[] r1 = GetResult(area, A1, B1, C1, Aab, Bab, Cab, Aac, Bac, Cac, triangleCoordinates, x3, y3);
        double[] r2 = GetResult(area, A1, B1, C1, Aab, Bab, Cab, Abc, Bbc, Cbc, triangleCoordinates, x4, y4);
        double[] r3 = GetResult(area, A1, B1, C1, Aac, Bac, Cac, Abc, Bbc, Cbc, triangleCoordinates, x5, y5);
        double[] max = {-1, -1, -1};
        double[] num = {r1[0], r2[0], r3[0]};
        for (int i = 0; i < num.length; i++) {
            if (max[0] < num[i]) {
                max[0] = num[i];
            }
        }
        if (max[0] == r1[0]) {
            max = r1;
        } else if (max[0] == r2[0]) {
            max = r2;
        } else if (max[0] == r3[0]) {
            max = r3;
        }

        if (max[0] == 0) {
            System.out.println(0);
        } else if (max[0] == 1) {
            System.out.println(1);
        } else if (max[0] == 2) {
            if (max[1] < max[2])
                System.out.println((int) max[0] + " " + max[1] + " " + max[2]);
            else
                System.out.println((int) max[0] + " " + max[2] + " " + max[1]);
        }

    }

    public static void InsideTriangle(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double[] triangleCoordinates = {x2, y2, x3, y3, x4, y4};
        if (IsDataError(triangleCoordinates)) {
            System.out.println("data error");
            return;
        }
        double area = Double.valueOf(String.format("%.6f", Area(triangleCoordinates)));
        double Aab = GetA(y2, y3), Bab = GetB(x2, x3), Cab = GetC(x2, y2, x3, y3);
        double Aac = GetA(y2, y4), Bac = GetB(x2, x4), Cac = GetC(x2, y2, x4, y4);
        double Abc = GetA(y3, y4), Bbc = GetB(x3, x4), Cbc = GetC(x3, y3, x4, y4);
        boolean t1 = ((Aab * x1 + Bab * y1 + Cab == 0) && (x1 - x2) * (x1 - x3) <= 0 && (y1 - y2) * (y1 - y3) <= 0);
        boolean t2 = ((Aac * x1 + Bac * y1 + Cac == 0) && (x1 - x2) * (x1 - x4) <= 0 && (y1 - y2) * (y1 - y4) <= 0);
        boolean t3 = ((Abc * x1 + Bbc * y1 + Cbc == 0) && (x1 - x3) * (x1 - x4) <= 0 && (y1 - y3) * (y1 - y4) <= 0);
        if (t1 || t2 || t3) {
            System.out.println("on the triangle");
            return;
        }
        double area1 = Area(new double[]{x1, y1, x2, y2, x3, y3});
        double area2 = Area(new double[]{x1, y1, x2, y2, x4, y4});
        double area3 = Area(new double[]{x1, y1, x3, y3, x4, y4});
        double areaAll = Double.valueOf(String.format("%.6f", area1 + area2 + area3));
        if (area == areaAll) {
            System.out.println("in the triangle");
        } else {
            System.out.println("outof the triangle");
        }

    }

    /*----------------------------------------------------------------------------------*/
    public static boolean IsDataError(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        if (((y3 - y2) / (x3 - x2)) == ((y2 - y1) / (x2 - x1)) || (x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
            return true;
        } else {
            return false;
        }
    }

    public static double GetA(double y1, double y2) {
        double A;
        A = y2 - y1;
        return A;
    }

    public static double GetB(double x1, double x2) {
        double B;
        B = x1 - x2;
        return B;
    }

    public static double GetC(double x1, double y1, double x2, double y2) {
        double C;
        C = x2 * y1 - x1 * y2;
        return C;
    }

    public static double SideLength(double x1, double y1, double x2, double y2) {
        double d = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
        return d;
    }

    public static double Area(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double a = SideLength(x1, y1, x2, y2);
        double b = SideLength(x1, y1, x3, y3);
        double c = SideLength(x2, y2, x3, y3);
        double cos = (a * a + b * b - c * c) / (2.0 * a * b);
        double area = Math.sqrt(1 - Math.pow(cos, 2)) * a * b * 0.5;
        return area;
    }

    public static double[] GetXY(double A1, double B1, double C1, double A2, double B2, double C2) {
        double[] XY = new double[2];
        XY[0] = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
        XY[1] = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1);
        return XY;
    }

    public static int GetPointCount(double A1, double B1, double C1, double A2, double B2, double C2, double A3, double B3, double C3, double[] tria) {
        int count = 0;
        if (A1 * B2 == A2 * B1 || A1 * B3 == A3 * B1) {
            return 0;
        }
        double[] newXY1 = GetXY(A1, B1, C1, A2, B2, C2);
        BigDecimal x1 = new BigDecimal(newXY1[0]), y1 = new BigDecimal(newXY1[1]);
        double[] newXY2 = GetXY(A1, B1, C1, A3, B3, C3);
        BigDecimal x2 = new BigDecimal(newXY2[0]), y2 = new BigDecimal(newXY2[1]);
        boolean t1 = ((x1.doubleValue() - tria[0]) * (x1.doubleValue() - tria[2]) <= 0 && (y1.doubleValue() - tria[1]) * (y1.doubleValue() - tria[3]) <= 0);
        boolean t2 = ((x1.doubleValue() - tria[0]) * (x1.doubleValue() - tria[4]) <= 0 && (y1.doubleValue() - tria[1]) * (y1.doubleValue() - tria[5]) <= 0);
        boolean t3 = ((x1.doubleValue() - tria[2]) * (x1.doubleValue() - tria[4]) <= 0 && (y1.doubleValue() - tria[3]) * (y1.doubleValue() - tria[5]) <= 0);
        boolean t4 = ((x2.doubleValue() - tria[0]) * (x2.doubleValue() - tria[2]) <= 0 && (y2.doubleValue() - tria[1]) * (y2.doubleValue() - tria[3]) <= 0);
        boolean t5 = ((x2.doubleValue() - tria[0]) * (x2.doubleValue() - tria[4]) <= 0 && (y2.doubleValue() - tria[1]) * (y2.doubleValue() - tria[5]) <= 0);
        boolean t6 = ((x2.doubleValue() - tria[2]) * (x2.doubleValue() - tria[4]) <= 0 && (y2.doubleValue() - tria[3]) * (y2.doubleValue() - tria[5]) <= 0);
        if (x1.subtract(x2).doubleValue() == 0 && y1.subtract(y2).doubleValue() == 0) {
            count = 1;
            return count;
        } else if ((t1 || t2 || t3) && (t4 || t5 || t6)) {
            count = 2;
            return count;
        } else {
            return 0;
        }
    }

    public static double[] GetResult(double area, double A1, double B1, double C1, double A2, double B2, double C2, double A3, double B3, double C3, double[] tria, double x, double y) {
        int pointCount = 0;
        pointCount = GetPointCount(A1, B1, C1, A2, B2, C2, A3, B3, C3, tria);
        if (pointCount == 0) {
            double[] dou = {0, 0, 0};
            return dou;
        } else if (pointCount == 1) {
            double[] dou = {1, 0, 0};
            return dou;
        } else if (pointCount == 2) {
            double[] newXY1 = GetXY(A1, B1, C1, A2, B2, C2);
            double[] newXY2 = GetXY(A1, B1, C1, A3, B3, C3);
            double area1 = Area(new double[]{x, y, newXY1[0], newXY1[1], newXY2[0], newXY2[1]});
            double area2 = area - area1;
            double[] dou = {2, Double.valueOf(String.format("%.6f", area1)), Double.valueOf(String.format("%.6f", area2))};
            return dou;
        }
        double[] dou = {-1, -1, -1};
        return dou;
    }
    /*----------------------------------------------------------------------------------*/
}

SourceMonitor生成的报表内容:
img
img
img

代码分析总结:
 这一题的代码将许多不同的功能写入了不同的类中,使得代码的复用性较好。但是通过观察上述的报表图知道,我本题的代码指标仍然有几项未达到被测量维度的期望值,因此还有待改进。

3.采坑心得

1. 题目集2 7-2

 一开始忘了每当获取到下一个数据时就要把字符“1”的计数置零,否则会影响下一个数据的奇偶校验判断的结果。
img

2. 题目集3 7-1

 一开始没有排除“00”这种多个连续的“0”出现的情况,并且最外层的for循环的结束条件还错了,导致提交代码后一些点过不去
img

3. 题目集3 7-2

 原来计算交点的方法可能出现了精度丢失的问题,提交代码后一直有个点过不去,后面换了种方法,利用函数的一般式求交点,再次提交代码后就通过了。
(PS:“01”这类整数部分以“0”开头数据也为非法数据,需要输出"Wrong Format")
img

4. 题目集3 7-3

 之前计算面积的时候使用的是double类型的数据,但是有时候会出现精度丢失的情况,后续的判断可能会出错,上网查了一下之后发现BigDemical类型的数据可以有效防止精度丢失,于是将交点改用了BigDemical类型,运算结束后再转回double类型,这样再次提交代码后成功通过。
img

4.改进建议

 题目集3中的7-1如果在代码开头部分先用正则表达式来筛选的话应该会更简单些,目前我的代码相对来说更加复杂。题目集3中的7-2和7-3中有些不同的选项可能需要用到相同的功能,因此我认为我的代码应该尽可能多的将多次用到的相同的功能写成一个类,需要此功能的时候直接调用就行了,能提高自己写的代码的复用性。

5.总结

通过这三次题目集的练习:

  1. 让我明白了写Java的代码时,不应该只将目光放在实现结果上,而是应该关注代码的总体结构,结构清晰分工明确,实现代码也就又快又准;
  2. 封装能够在实现类的功能时保证类的独立性类在独立时,能够在任何情况下被快速引用,所以写代码应该尽可能地使代码模块化,避免大量重复的代码段,让自己的代码有可观的复用性;
  3. 我掌握了正则表达式的基本用法,并且让我明白了正则表达式对于字符处理类的题目的便利;

建议:希望老师在有空的时候能够讲解一下这些pta上的题目的解题思路,供大家参考一下。




最后附上Source Monitor的使用说明链接:https://blog.csdn.net/u012075442/article/details/115653071

点击此处回到顶部

标签:01,double,System,blog,douArr,&&,println,out
From: https://www.cnblogs.com/nchu202071/p/16730376.html

相关文章

  • JDBC 尚硅谷 days01-数据库连接方式
    1.JDBC(JavaDatabaseConnectivitu):是一个独立于特定数据库管理系统、通用的SQL数据库存储和操作的公共接口;2.JDBC接口包括两个层次面向应用的API:JavaAPI,抽象接口,......
  • 做题记录整理dp14 P5336 [THUSC2016]成绩单(2022/9/27)
    P5336[THUSC2016]成绩单这题难度标的虚高首先一眼区间dp,然后写出递推方程然后发现爆空间,再上离散化然后就没了。。。撑死也就是蓝题不过学到了一个离散化技巧#incl......
  • P4017 最大食物链计数
    P4017最大食物链计数最大食物链计数题目背景你知道食物链吗?Delia生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条。于是她来就来求助你......
  • 01.base-sys 基础搭建
    脚手架base-sys0.简介1.基于SpringBoot+MybatisPlus-Plus的快速开发脚手架,具有完整的权限管理功能,前端使用Vue;2.技术选型:SpringBoot2.7.0容器+M......
  • 01.web基础
    概述前端开发:web开发,通过html,css,javascript等以及它们衍生出来的技术和框架,来实现互联网产品的用户界面交互设计网页、网站:网页是单独的静态的页面,不需要后端程序,网......
  • 【NOIP2017 提高组】列队
    [【NOIP2017提高组】列队](https://www.luogu.com.cn/problem/P3960)将每一行的第1到m-1个和第m列分离出来分析知这n+1个“区间”要维护弹出第k个和插入最后使用平衡树,......
  • 【NOIP2017 提高组]】宝藏
    【NOIP2017提高组】宝藏f[S][i]表示集合S的点构成的生成树,树高为i({i}为0)的最小花费转移:枚举S的子集S'为高度为i-1的树(S'可扩展出S):f[S][i]<-f[S'][i]+cost预处......
  • Wallys//Industrial_Wireless_AP//network,card//QCN6024/QCN9074/QCN9024,WiFi,Card
    QCN9074WiFiCardIPQ6010,802.11ax,2x22.4G&5G,SupportOpenWRT//QCN9074802.11ax4x4MU-MIMO6GHzwifi6EQCN9074 11ax 4x46G M.2   1.DR9074-6E(PN02......
  • VS2008 - VS2010默认环境设置
    我们在安装VS2008后会默认让我们选择一个默认开发环境如果我们想做另外一种开发时,就得切换成另外一种开发环境 在开始菜单打开:MicrosoftVisualStudio2008>>Visual......
  • 标准号:GB/T 14531-2017
    https://openstd.samr.gov.cn/bzgk/gb/std_list?p.p1=0&p.p90=circulation_date&p.p91=desc&p.p2=14531中文标准名称:办公家具阅览桌、椅、凳 英文标准名称:Office......