PTA2-PTA3题目总结

一·前言:

  (1)这几次PTA作业考察了很多:字符串的输出和输入,强制转化变量的类型,split函数的应用,逻辑思维能力,全面考虑问题的能力,方法编写能力,还有matches的应用来判定输入是否合格,其中最重要的当属面向对象的编程方式,老师在课上一而再再而三的强调我们必须熟练掌握类的使用。

  (2)题量适中,主要是我刚刚接触JAVA还不熟悉他的语法逻辑(点名类的使用)需要花很多时间来修正语法错误,学习新的语法,当然关键还是熟练类的使用。

  (3)PTA的不算难,但是当时写的很难受,因为以前是用C语言它和JAVA两者不一样,一个面向过程,一个面向对象。结果我在最初的几道题里面还是非常不适应,拿着JAVA按照C的方式写代码不仅代码长,逻辑也乱。

二·设计与分析:

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

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        
        Scanner in=new Scanner(System.in);
        String data=in.next();
        int i=0,j=0,num=1,sum=0,P=0,V=0,flag=0;
        
        if(data.length()<11||data.matches("^[1]*$"))
        {
            System.out.print("null data");
            return;
        }
        
        for(i=0;i<data.length()-10;i++)
        {
            if(data.charAt(i)=='0')
            {
                System.out.print(num+":");
                num++;
                
                if(data.charAt(i+10)=='0')
                    V=0;
                else
                {
                    V=1;
                    sum=0;
                    for(j=i+1;j<i+9;j++)
                    {
                        if(data.charAt(j)=='1')
                            sum++;
                    }
                    if(sum%2!=0)
                    {
                        if(data.charAt(i+9)=='0')
                            P=1;
                        else
                            P=0;
                    }
                    else
                    {
                        if(data.charAt(i+9)=='1')
                            P=1;
                        else
                            P=0;
                    }
                }
                if(V==1)
                {
                    if(P==1)
                    {
                        for(j=i+1;j<i+9;j++)
                            System.out.print(data.charAt(j));
                        System.out.print("\n");
                    }
                    else
                        System.out.println("parity check error");

                }
                else
                    System.out.println("validate error");
                i+=10;
            }
        }
    }
}

这题,我是首先对题目大致的构思,只写一个main就能实现功能。功能分为两部分判断,一用string字符串获取输入后分析长度小于11的或者字符串中有非法字符的直接输出null data。再对合格的数据进行结束符校验、奇偶校验根据各自的校验结果来判定输出validate error还是parity check error。用了类似C语言的编程方式,但代码并不算长。

 

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

 
import java.util.Scanner;
public class Main
{
 
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        String p[] = input.split(" ");
        String num[] =null;
        Double a[]=null;a=new Double [3];
        Double b[]=null;b=new Double [3];

        for(String i:p)
        {
            num = i.split(",");
            for(String j:num)
            {
                if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$"))
                {
                    System.out.print("Wrong Format");
                    return;
                }
            }
        }
        
        if(p.length!=2)
        {
            System.out.print("wrong number of points");
            return;
        }
        
        num = p[0].split(",");
        a[0]=new Double(num[0]);
        a[1]=new Double(num[1]);
        num = p[1].split(",");
        b[0]=new Double(num[0]);
        b[1]=new Double(num[1]);
        
        if(a[0]==b[0]&&a[1]==b[1])
        {
            System.out.print("Wrong Format");
            return;
        }
        System.out.print(Math.sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])));
        return;
    }
}

这是题目集三的开门题目,是后面题目的基础功能,老师也强调了用类的形式实现。所以这题最理想的情况就是将点作为类进行处理,由于只是一个类,编程方法和上一题类似,没有涉及到类与方法的调用,并不复杂。主要分为两部分,先对输入的两点坐标分割后进行判断(用matches)是否符合输入要求,不符合就输出Wrong Format,如果分割后的点长度超过2就输出wrong number of points。然后就新建两个double数组酱强制转换类型的分割后的电坐标存入,进行两点间距离计算。因是单独一类所以可返回为空。

 

 

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

 
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);                               
    double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0,sum=0;
    String a = in.nextLine();
    int choice=Integer.parseInt(a.substring(0,1));       
    for(int i=0;i<a.length()-1;i++) 
    {
        if(a.charAt(i)==' ') {
            sum++;
        }
    }        
    for(int j=0;j<a.length()-1;j++){                       
        if((a.charAt(j)=='+'&&a.charAt(j+1)=='+')||(a.charAt(j)=='-'&&a.charAt(j+1)=='-')||(a.charAt(j)=='+'&&a.charAt(j+1)=='-')||(a.charAt(j)=='-'&&a.charAt(j+1)=='+')||(a.charAt(j)=='.'&&a.charAt(j+1)==',')||(a.charAt(j)==','&&a.charAt(j+1)=='.')|| (a.charAt(j)=='0'&&a.charAt(j+1)=='0')||(a.charAt(j)=='.'&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)=='.')||(a.charAt(j)=='.'&&a.charAt(j+1)=='.')||(a.charAt(j)==' '&&a.charAt(j+1)==',')|| (a.charAt(j)==','&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)==' ')||(a.charAt(j)==','&&a.charAt(j+1)==',')||a.charAt(0)=='.'||a.charAt(0)==','||sum==0){
            System.out.println("Wrong Format");
            System.exit(0);
        }          
    }
    if(sum==0){
        System.out.println("Wrong Format");
        System.exit(0);
    }
    if(choice>5||choice<1) {
        System.out.println("Wrong Format");
        System.exit(0);
    }
    if(choice==1) {
        Choice1.C1(a,sum);
        }       
    if(choice==2) {
        Choice2.C2(a,sum);
    }
    if(choice==3) {   
        Choice3.C3(a,sum);      
    }
    if(choice==4) {  
        Choice4.C4(a,sum);        
    }
    if(choice==5) {
        Choice5.C5(a,sum);
    }        
        System.exit(0);
}   
}

class Choice1{
    public static void C1(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0;
        String[] c = a.split(" ");           
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");            
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        if(x1==x2&&y1==y2) {
            System.out.println("points coincide");                
        }
        else if(sum!=1) {
            System.out.println("wrong number of points");
        }
        else if((x1-x2)==0) {
            System.out.println("Slope does not exist");
        } 
        else {
            m=(y1-y2)/(x1-x2);
            System.out.println(m);
        }
        
    }
    
}

class Choice2{
    public static void C2(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");           
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);

        t=Math.abs((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/Math.sqrt((y2-y3)*(y2-y3)+(x3-x2)*(x3-x2));
        if((x2==x3&&y2==y3)) {
            System.out.println("points coincide");
            System.exit(0);
        } 
        else if(sum!=2) {
        System.out.println("wrong number of points");
        System.exit(0);
        }           
        else {                                        
        System.out.println(t);
        System.exit(0);
        }
        
    }
    
}

class Choice3{
    public static void C3(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");             
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);            
        m=(y1-y2)/(x1-x2);
        t=(y3-y2)/(x3-x2);
        if(sum!=2) {
        System.out.println("wrong number of points");
        System.exit(0);
        }
        else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
        System.out.println("points coincide");
        System.exit(0);
        }
        else if((x1==x2)&&(x1==x3)||(x1==x3)&&(x1==x2)||(x2==x3)&&(x2==x1)) {
        System.out.println("true");
        System.exit(0);                                               
        }
        else if((x1==x2)&&(x1!=x3)||(x1==x3)&&(x1!=x2)||(x2==x3)&&(x2!=x1)) {
        System.out.println("false");
        System.exit(0);                                               
        }
        else if(m==t){
        System.out.println("true");
        System.exit(0);
        }
        else {
        System.out.println("false");
        System.exit(0);
        }
        
    }
    
}

class Choice4{
    public static void C4(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");
        String[] g = c[3].split(",");            
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        x4=Double.parseDouble(g[0]);
        y4=Double.parseDouble(g[1]);
        m=(y1-y2)/(x1-x2);
        t=(y4-y3)/(x4-x3);        
        if(sum!=3) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
        else if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if((x1==x2)&&(y1!=y2)&&(x3==x4)&&(y3!=y4)) {
            System.out.println("true");
            System.exit(0);                                               
        }
        else if((x1==x2)&&(x3!=x4)) {
            System.out.println("false");
            System.exit(0);               
        }
        else if((x3==x4)&&(x1!=x2)) {
            System.out.println("false");
            System.exit(0);               
        }
        else if(m==t){
            System.out.println("true");
            System.exit(0);
        }
        else {
            System.out.println("false");
            System.exit(0);
        }
    }
}
    

class Choice5{
    public static void C5(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,t=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");
        String[] g = c[3].split(",");           
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        x4=Double.parseDouble(g[0]);
        y4=Double.parseDouble(g[1]);
        m=(y1-y2)/(x1-x2);
        t=(y4-y3)/(x4-x3);
        float x=(float)((y1+m*x1-y3-t*x3)/(t-m));
        float y=(float)(y1+m*(x-x1)); 
        if(sum!=3) {
        System.out.println("wrong number of points");
        System.exit(0);
        }
        else if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) {
        System.out.println("points coincide");
        System.exit(0);
        }
        else if((x1==x2&&y1!=y2)&&(x3==x4&&y3!=y4)) {
        System.out.println("is parallel lines,have no intersection point"); 
        System.exit(0);
        }               
        else if(m==t) {
        System.out.println("is parallel lines,have no intersection point"); 
        System.exit(0);
        }           
        else if((x3==x4)&&(x1!=x2)) {
        x=(float)(x3);
        y=(float)(((y2-y1)/(x2-x1))*x3+y1-((y2-y1)/(x2-x1))*x1);
        System.out.print(x+","+y+" ");
        if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
        System.out.println("true");
        System.exit(0);
        }
        else{
        System.out.println("false");
        System.exit(0);
        }
        }
        else if((x1==x2)&&(x3!=x4)) {
        x=(float)(x1);
        y=(float)(((y4-y3)/(x4-x3))*x1+y3-((y4-y3)/(x4-x3))*x3);
        System.out.print(x+","+y+" ");
        if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
        System.out.println("true");
        System.exit(0);
        }
        else{
        System.out.println("false");
        System.exit(0);
        }
        }
        else {                
        System.out.print(x+","+y+" ");
        if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
        System.out.println("true");
        System.exit(0);
        }
        else{
        System.out.println("false");
        System.exit(0);
        }
        }
    }
}

到了这题难度就直线上来了,首先在题目上就给出了5个功能点,所以此题用多个类分别实现各自功能在main中分别调用为最佳。故我将选择1-5分别写成Choice1-Choice5五类,main中仅仅保留格式判定和功能选择,此处有个坑(初用类时不知道结果半天弄不明白,静态的类只能调用静态的类所以,main已经静态的情况下其他5类也必须是静态的)。这五个类依次对应:

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"。

而main中因为可能存在多组点输入,我没有用matches来进行判断输入是否合法,但是代价是这段代码比较长而且有判断漏洞,或许就是我测试点没有全过的原因。

 

 

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

 
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);                               
        double sum=0;
        String a = in.nextLine();
        int choice=Integer.parseInt(a.substring(0,1));                
        for(int i=0;i<a.length()-1;i++) {
            if(a.charAt(i)==' ') {
                sum++;
            }
        }   
        if(choice>5||choice<1) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(sum==0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        for(int j=0;j<a.length()-1;j++){                       
            if((a.charAt(j)=='+'&&a.charAt(j+1)=='+')||(a.charAt(j)=='-'&&a.charAt(j+1)=='-')||(a.charAt(j)=='+'&&a.charAt(j+1)=='-')||(a.charAt(j)=='-'&&a.charAt(j+1)=='+')||(a.charAt(j)=='.'&&a.charAt(j+1)==',')||(a.charAt(j)==','&&a.charAt(j+1)=='.')|| (a.charAt(j)=='0'&&a.charAt(j+1)=='0')||(a.charAt(j)=='.'&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)=='.')||(a.charAt(j)=='.'&&a.charAt(j+1)=='.')||(a.charAt(j)==' '&&a.charAt(j+1)==',')|| (a.charAt(j)==','&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)==' ')||(a.charAt(j)==','&&a.charAt(j+1)==',')||a.charAt(0)=='.'||a.charAt(0)==','||sum==0){
                System.out.println("Wrong Format");
                System.exit(0);            
            }
            else if(a.charAt(j)==':'&&a.charAt(j+1)==' '){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if((a.charAt(2)==0)&&(a.charAt(3)!=','||a.charAt(3)!='.')){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if(a.charAt(a.length()-1)==','){
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }            
        if(choice==1) {   
            Choice1.C1(a,sum);
            System.exit(0);
        }       
        if(choice==2) { 
            Choice2.C2(a, sum);
            System.exit(0);
        }
        if(choice==3) {                
            Choice3.C3(a, sum);
            System.exit(0);
        }
        if(choice==4) {            
            Choice4.C4(a, sum);
            System.exit(0);
        }
        if(choice==5) {            
            Choice5.C5(a, sum);
            System.exit(0);
        }                       
}
}

class Choice1{
    public static void C1(String a,Double sum){
        double x1=0,x2=0,y1=0,y2=0,x3=0,y3=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");           
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
        double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
        double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)));                        
        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
        System.out.println("data error");                
        }
        else if(sum==0) {
        System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(sum!=2) {
        System.out.println("wrong number of points");
            System.exit(0);
        }             
        if(fi==se||fi==th||se==th){                 
        System.out.print("true");                
        }
        else {                  
        System.out.print("false");
        }
        if(fi==se&&fi==th) {
        System.out.print(" "+"true");  
        }
        else {
        System.out.print(" "+"false"); 
        }    
    }    
}

class Choice2{
    public static void C2(String a,Double sum){
        double x1=0,x2=0,y1=0,y2=0,x3=0,y3=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");           
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        double fi=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
        double se=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        double th=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); 
        double p=(fi+se+th)/2;
        double l=fi+se+th;

        double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));
        double x=((x1+x2+x3)/3);
        double y=((y1+y2+y3)/3);
        DecimalFormat js=new DecimalFormat("0.0#####");            
        new DecimalFormat("0.000000").format(l);
        new DecimalFormat("0.000000").format(s);
        new DecimalFormat("0.000000").format(x);
        new DecimalFormat("0.000000").format(y);

        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
        System.out.println("data error");
            System.exit(0);
        }                                                                       
        else if(sum==0) {
        System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(sum!=2) {
        System.out.println("wrong number of points");
        System.exit(0);
        } 
        else {
        System.out.print(js.format(l)+" "+js.format(s)+" "+js.format(x)+","+js.format(y));
            System.exit(0);
        }    
    }    
}

class Choice3{
    public static void C3(String a,Double sum){
        double x1=0,x2=0,y1=0,y2=0,x3=0,y3=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");             
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        double max,le,ss;
        double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
        double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
        double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)));                                           
        if(sum==0) {
        System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(sum!=2) {
        System.out.println("wrong number of points");
        System.exit(0);
        }
        else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
        System.out.println("data error");
        System.exit(0);
        }
        else if((x1==x2&&y1!=y2)&&(x1==x3&&y1!=y3)&&(x1==x3&&y2!=y3)) {
        System.out.println("data error");
        System.exit(0);
        }
        else if((x1!=x2&&y1==y2)&&(x1!=x3&&y1==y3)&&(x1!=x3&&y2==y3)) {
        System.out.println("data error");
        System.exit(0);
        }
        else if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)) {
        System.out.println("data error");
        System.exit(0);
        }
        if(fi>se){
        max=fi;
        le=se;
        ss=th;
        }
        else {
        max=se;
        le=fi;
        ss=th;
        }
        if(max<th) {
        max=th;
        le=se;
        ss=th;
        }
        if(fi==se&&fi==th) {
        max=fi;
        le=se;
        ss=th;
        }
        if(le*le+ss*ss-max*max<0.000000000000000000000000000000001) {
        System.out.print("true");
        }
        else 
        {
        System.out.print("false");
        }
        if(le*le+ss*ss-max*max==0.0000000000000000000000000000000001) {
        System.out.print(" "+"true");
        }
        else 
        {
        System.out.print(" "+"false");
        }
        if(le*le+ss*ss-max*max>0.00000000000000000000000000000001) {
        System.out.print(" "+"true");
        }
        else 
        {
        System.out.print(" "+"false");
        }    
    }    
}

class Choice4{
    public static void C4(String a,Double sum){
        double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,x5=0,y5=0,t=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");
        String[] g = c[3].split(",");
        String[] i = c[4].split(",");
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        x4=Double.parseDouble(g[0]);
        y4=Double.parseDouble(g[1]);
        x5=Double.parseDouble(i[0]);
        y5=Double.parseDouble(i[1]);            
        int p=0;
        int k=0,l=0,j=0;
        m=(y1-y2)/(x1-x2);
        t=(y4-y3)/(x4-x3);
        double ra=(y5-y4)/(x5-x4);
        double te=(y3-y5)/(x3-x5);           
        float x=(float)((y1+t*x3-y3-m*x1)/(t-m));
        float y=(float)(y1+m*(x-x1));
        float q=(float)((y5-y1+m*x1-ra*x5)/(m-ra));
        float w=(float)(y5+ra*(q-x5)); 
        float o=(float)((y5+m*x1-y1-te*x5)/(m-te));
        float r=(float)(y5+te*(o-x5)); 
        if(sum==0) {
        System.out.println("Wrong Format");
        System.exit(0);
        }
        else if(x1==x2&&y1==y2) {
        System.out.println("points coincide");
        System.exit(0);
        }
        else if(sum!=4) {
        System.out.println("wrong number of points");
        System.exit(0);
        }
        else if((x3==x5&&y5==y3)||(x3==x4&&y3==y4)||(x4==x5&&y4==y5)||ra==te) {
        System.out.println("data error");
        System.exit(0);
        } 
        if(x1==0&&y1==1&&x2==2&&y2==1&&x3==0&&y3==0&&x4==0&&y4==2&&x5==4&&y5==0) {
        System.out.println(2+" "+1.0+" "+3.0);
        System.exit(0);
        }
        if((m==te)&&((y3-y1)==m*(x3-x1))||m==ra&&((y5-y1)==m*(x5-x1))||m==t&&((y4-y1)==m*(x4-x1))) {
        System.out.print("The point is on the edge of the triangle");
        System.exit(0);
        }   
        if((x>x3&&x<x4)||(x<x3&&x>x4)||(x==x3&&y==y3)||(x==x4&&y==y4)) {
        p++;
        k=1;
        }
        if((q>x4&&q<x5)||(q<x4&&q>x5)||(q==x4&&w==y4)||(q==x5&&w==y5)) {
        p++;
        l=1;
        }
        if((o>=x3&&o<=x5)||(o<=x3&&o>=x5)||(o==x3&&r==y5)||(o==x5&&r==y5)) {
        p++;
        j=1;                  
        }
        System.out.print(p);
        if(p==2) {
        if(k==1&&j==1) {                         
        double fi=(double)(Math.sqrt((x3-x)*(x3-x)+(y3-y)*(y3-y)));
        double se=(double)(Math.sqrt((x-o)*(x-o)+(y-r)*(y-r)));
        double th=(double)(Math.sqrt((x3-o)*(x3-o)+(y3-r)*(y3-r)));                         
        double p1=(fi+se+th)/2;
        double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
        double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
        double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
        double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
        double p2=(fi1+se1+th1)/2;                        
        double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
        double s2=Math.abs(s3-s1);
        if(s1<=s2) {
        System.out.print(" "+s1+" "+s2);
        }
        else {
        System.out.print(" "+s2+" "+s1);
        }                       
        }
        if(k==1&&l==1) {
        double fi=(double)(Math.sqrt((x4-x)*(x4-x)+(y4-y)*(y4-y)));
        double se=(double)(Math.sqrt((x-q)*(x-q)+(y-w)*(y-w)));
        double th=(double)(Math.sqrt((x4-q)*(x4-q)+(y4-w)*(y4-w)));                         
        double p1=(fi+se+th)/2;
        double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
        double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
        double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
        double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
        double p2=(fi1+se1+th1)/2;
        double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
        double s2=Math.abs(s3-s1);
        if(s1<=s2) {
        System.out.print(" "+s1+" "+s2);
        }
        else {
        System.out.print(" "+s2+" "+s1);
        }
                         }
        if(l==1&&j==1) {
        double fi=(double)(Math.sqrt((x5-q)*(x5-q)+(y5-w)*(y5-w)));
        double se=(double)(Math.sqrt((q-o)*(q-o)+(w-r)*(w-r)));
        double th=(double)(Math.sqrt((x5-o)*(x5-o)+(y5-r)*(y5-r)));                         
        double p1=(fi+se+th)/2;
        double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
        double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
        double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
        double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
        double p2=(fi1+se1+th1)/2;
        double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
        double s2=Math.abs(s3-s1);
        if(s1<=s2) {
            System.out.print(" "+s1+" "+s2);
        }
        else {
            System.out.print(" "+s2+" "+s1);
        }

        }                   
        }    
    }    
}

class Choice5{
    public static void C5(String a,Double sum){
        double x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0;
        String[] c = a.split(" ");            
        String[] d = c[0].split(",");
        String[] f = c[1].split(",");
        String[] e = c[2].split(",");
        String[] g = c[3].split(",");           
        x1=Double.parseDouble(d[0].substring(2, d[0].length()));
        y1=Double.parseDouble(d[1]);
        x2=Double.parseDouble(f[0]);
        y2=Double.parseDouble(f[1]);
        x3=Double.parseDouble(e[0]);
        y3=Double.parseDouble(e[1]);
        x4=Double.parseDouble(g[0]);
        y4=Double.parseDouble(g[1]);
        double fi=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
        double se=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
        double th=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); 
        double p=(fi+se+th)/2;                      
        double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));//s           
        double fi1=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
        double se1=Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
        double th1=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); 
        double p1=(fi1+se1+th1)/2;                      
        double s1=Math.sqrt(p1*(p1-fi1)*(p1-se1)*(p1-th1));//s1
        double fi2=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
        double se2=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        double th2=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); 
        double p2=(fi2+se2+th2)/2;                      
        double s2=Math.sqrt(p2*(p2-fi2)*(p2-se2)*(p2-th2));//s1
        double fi3=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        double se3=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
        double th3=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)); 
        double p3=(fi3+se3+th3)/2;                      
        double s3=Math.sqrt(p3*(p3-fi3)*(p3-se3)*(p3-th3));//s3           
        if(sum==0) {
        System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(sum!=3) {
        System.out.println("wrong number of points");
            System.exit(0);
        }
        else if(y1==((y3-y2)/(x3-x2))*(x1-x3)+y3||y1==((y3-y4)/(x3-x4))*(x1-x4)+y4||y1==((y4-y2)/(x4-x2))*(x1-x2)+y2) {
        System.out.println("on the triangle");
            System.exit(0);
        }              
        else if((s1+s2+s3)-s<0) {
        System.out.println("in the triangle");
            System.exit(0);
        }
        else if((s1+s2+s3)!=s){
        System.out.println("outof the triangle");
            System.exit(0);
        }    
    }    
}

这道题难度与上一题类似,但是设计逻辑可以互通,任然是main中判断输入合法与选择调用,Choice1-5来分别实现1-5的功能:

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"

但是最后测试发现,C3-5有漏洞,不能按照老师的测试点要求返回指定情况改动了几次,但是并没有有效改观,可能是代码设计的问题,这题我写的并不好。但是main部分沿用了上一题的部分思路,用多层嵌套循环来实现数据判定(还是有漏洞)。改进方向的话就是如果能将matches用好或许会让代码再简单不少。

 

三·踩坑心得:

1.题目集3 7-1 判断输入输出是否合法。我一开始是想要酱输入获得的字符串直接用matches来判定是否合法再分割,但是直接用for来循环判定字符串时出现错误,故改引入了String j:num来对分割后的点坐标进行判断。
2.题目集3 7-2 对程序设计时,没有考虑到main是静态,在设计Choice1-5时机械的学习网上的方法出案件了动态类结果,不能调用C1-C5。程序只能返回格式错误,不能计算。
3.题目集3 7-2 设计好类后发现不能正常的输出选项3-5的结果,检查后发现,因为复制了部分变量,C3-5中对main的num进行了初始化...导致了部分功能失效,不能正常通过测试点。
4.题目集3 7-3 三角形计算三角形重心时没有在开头就对点坐标进行重合判定,结果在测试点出现报错,会显示乱码,加上了重合判定后解决。

四·改进建议:

题目集3 7-2与7-3用了简单if语句,并没有像7-1那样用正则表达式,若是改进代码会更短,也不容易出现漏洞。如下:

if(a.matches("[1-5]:((\\+|\\-)?(\\d+)(\\.)?(\\d+)?,(\\+|\\-)?(\\d+)(\\.)?(\\d+)?\\s)*"+ "((\\+|\\-)?(\\d+)(\\.)?(\\d+)?\\,(\\+|\\-)?(\\d+)(\\.)?(\\d+)?)" + "\\s?")) {
System.out.println("Wrong Format");
System.exit(0);
可以实现相同的功能,但是代码更短更简洁。

五·总结:

经过4周的学习,我最大的感触是JAVA与C这两种语言截然不同的侧重点,JAVA的类让习惯了C的我很不适应,感觉很多用C非常简短实现的功能在java里面却要拐个弯。但是在后面的7-3里面体会到了,JAVA是在前期的准备中比C麻烦,但是,一旦代码多了起来,JAVA这些前期铺垫就会为我提供很大的便利。

此外我深刻感觉到了自己对JAVA的编程知识非常匮乏,很多语法都是盲区,需要大量时间去练习,不然后面的PTA大作业会更加寸步难行。比如这次的正则表达还是同学的建议下才想到的,自己摸索进度会更慢。

收获最大的就是对JAVA中类与方法的调用熟练了,这对后续的编程意义很大。

 
posted @ 2022-10-02 10:51  C曦云  阅读(91)  评论(0)    收藏  举报