Java教程

第一次Blog-Java实验1

本文主要是介绍第一次Blog-Java实验1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、前言:

  从开学到现在,已经陆陆续续写了三周的PTA的大作业了,对于难度来说肯定是一次比一次的难度高,但是每次偏向的重点不同。

    1、第一次只是简单的公式运算和一些字符的查找;

    2、第二次就是字母字符的转换、串口字符的分析、String的格式判断与提取,其中会用到charAt函数和subString函数;

      charAt()应用:

        方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推。

      substring(int beginIndex, int endIndex)应用:

        第一个参数int为开始的索引,对应String数字中的开始位置,

        第二个参数是截止的索引位置,对应String中的结束位置

    3、第三次的大作业就相当难了,虽然里面是点线的运算,但是中间运用了正则表达式,和一些算法,比如:点到线的距离公式,点与点的距离公式,cos与sin的计算等等一些复杂的计算,还用到了一些方法去运算。

      1、正则表达式:

      正则表达式是由一些具有特殊含义的字符组成的字符串,多用于查找、替换符合规则的字符串。在表单验证、Url映射等处都会经常用到

      2、元字符

      3、反义字符:

 

       4、限定符:

       5、懒惰限定符

2、设计与分析:

   1、实验二7-2

码源:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String number = in.nextLine();
        String l=" ";
        int j,sum=0,n=1;
            for(int i=0;i<number.length();i++)
            {
                if(number.charAt(i)=='0')
                {
                    sum=1;
                }
            }
            if(sum==0||number.length()<11)
                System.out.println("null data");
            
            if(sum!=0&&number.length()>=11)
            {
            for(j=0;j<number.length();j++)
            {
                if(number.charAt(j)=='0')
                {
                    int total=0;
                    l=number.substring(j+1, j+9);
                    for(int k=0;k<l.length();k++)
                    {
                        if(l.charAt(k)=='1')
                            total++;
                    }
                    if(total%2==0)
                    {
                        if(number.charAt(j+9)=='1')
                        {
                            if(number.charAt(j+10)=='0')
                            {
                                System.out.println(n+":"+"validate error");
                                n++;
                                j=j+11;
                            }
                            else if(number.charAt(j+10)=='1')
                            {
                                System.out.println(n+":"+number.substring(j+1,j+9));
                                n++;
                                j=j+10;
                            }
                        }
                        else if(number.charAt(j+9)=='0')
                        {
                            if(number.charAt(j+10)=='1')
                            {
                                System.out.println(n+":"+"parity check error");
                                n++;
                                j=j+10;
                            }
                            else if(number.charAt(j+10)=='0')
                            {
                                System.out.println(n+":"+"validate error");
                                n++;
                                j=j+11;
                            }
                        }
                    }
                    else if(total%2==1)
                    {
                        if(number.charAt(j+9)=='0')
                        {
                            if(number.charAt(j+10)=='1')
                            {
                                System.out.println(n+":"+number.substring(j+1,j+9));
                                n++;
                                j=j+10;
                            }
                            else if(number.charAt(j+10)=='0')
                            {
                                System.out.println(n+":"+"validate error");
                                n++;
                                j=j+11;
                            }
                        }
                        else if(number.charAt(j+9)=='1')
                        {
                            if(number.charAt(j+10)=='1')
                            {
                                System.out.println(n+":"+"parity check error");
                                n++;
                                j=j+10;
                            }
                            else if(number.charAt(j+10)=='0')
                            {
                                System.out.println(n+":"+"validate error");
                                n++;
                                j=j+11;
                            }
                        }
                    }
                }
            }
        }
    }
}

  复杂度:

 

 

  2、实验三7-1

源码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String number = in.nextLine();
        judge.input1(number);
        
    }
}
class judge{
    public static void input1(String number){
    String[] tokens = null;
    int sum=0,count=0,n=0;
    for(int i=0;i<number.length();i++)
    {
        if((number.charAt(i)=='+'||number.charAt(i)=='-')&&(number.charAt(i+1)=='+'||number.charAt(i+1)=='-'))
        {
            System.out.println("Wrong Format");
            n++;
        }
        else if(number.charAt(i)==',')
            count++;
        else if(number.charAt(i)==' ')
            sum++;
        else
            tokens=number.split("[, ]");
    }
    if(n!=1)
    {
        if(tokens.length>4&&tokens.length%2==0)
            System.out.println("wrong number of points");
        else if(sum!=1||count!=2||tokens.length!=4&&tokens.length%2==1)
            System.out.println("Wrong Format");
        else if(tokens.length==4)
        {
            Double x1 = Double.parseDouble(tokens[0]);
            Double y1 = Double.parseDouble(tokens[1]);
            Double x2 = Double.parseDouble(tokens[2]);
            Double y2 = Double.parseDouble(tokens[3]);
            cal.length(x1,y1,x2,y2);
        }
    }
    }
}
class cal{
    public static void length(double x1,double y1,double x2,double y2) {
        double l=0,l1;
        l1=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
        l=Math.sqrt(l1);
        System.out.println(l);
    }
    
}

复杂度:

 

 

  3、实验三7-2

源码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String number = in.nextLine();
        String[] tokens = null;
        for(int i=0;i<number.length();i++)
        {
            tokens=number.split("[:, ]");
        }
        judged.judge1(number,tokens);
    }

}

class judged{
    private static String df= "[1-5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)+";
    
    public static void judge1(String number,String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        if(tokens.length==5&&number.matches(df)&&n==1){
            twopoints.tow(tokens);
        }
        else if(tokens.length!=5&&number.matches(df)&&n==1)
            System.out.println("wrong number of points");
        else if(tokens.length==7&&number.matches(df)&&(n==2||n==3)){
            threepoints.three(tokens);
        }
        else if(tokens.length!=7&&number.matches(df)&&(n==2||n==3))
            System.out.println("wrong number of points");
        else if(tokens.length==9&&number.matches(df)&&(n==4||n==5)) {
            fourpoints.four(tokens);
        }
        else if(tokens.length!=9&&number.matches(df)&&(n==4||n==5))
            System.out.println("wrong number of points");
        else
            System.out.println("Wrong Format");
    }
}

class twopoints{
    public static void tow(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        double k=0;
        k=(y1-y2)/(x1-x2);
        if(n==1)
        {
            if(x1-x2==0)
            {
                if(y1-y2==0)
                    System.out.println("points coincide");
                else
                    System.out.println("Slope does not exist");
            }
            else
                System.out.println(k);
    
        }
    }
}
class threepoints{
    public static void three(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        Double x3 = Double.parseDouble(tokens[5]);
        Double y3 = Double.parseDouble(tokens[6]);
        boolean judge=false;
        double l,k1,k2;
        k1=(y1-y2)/(x1-x2);
        k2=(y3-y2)/(x3-x2);
        if(n==2)
        {
            if((x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x2-x3==0&&y2-y3==0))
            {
                    System.out.println("points coincide");
            }
            else
            {
            l = Math.abs(((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/(Math.sqrt(Math.pow(y2-y3, 2) +Math.pow(x2-x3, 2))));
            System.out.println(l);
            }
        }
        else if(n==3)
        {
            if((x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x2-x3==0&&y2-y3==0))
            {
                    System.out.println("points coincide");
            }
            else
            {
                if(k1==k2||(x1==x2&&x2==x3))
                    judge=true;
                else if((y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)==0)
                    judge=true;
                else
                    judge=false;
                System.out.println(judge);    
            }
        }
    }
}
class fourpoints{
    public static void four(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        Double x3 = Double.parseDouble(tokens[5]);
        Double y3 = Double.parseDouble(tokens[6]);
        Double x4 = Double.parseDouble(tokens[7]);
        Double y4 = Double.parseDouble(tokens[8]);
        boolean judge=false;
        boolean judge2=false;
        double x,y;
        double k1=0,k2=0;
        k1=(y2-y1)/(x2-x1);
        k2=(y4-y3)/(x4-x3);
        if(n==4)
        {
            if((x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x1-x4==0&&y1-y4==0)||(x2-x3==0&&y2-y3==0)||(x2-x4==0&&y2-y4==0)||(x3-x4==0&&y3-y4==0))
            {
                    System.out.println("points coincide");
            }
            else
            {
                if((x2-x1==0&&x4-x3!=0)||(x4-x3!=0&&x2-x1==0))
                    judge=false;
                else
                {
                    if(k1==k2)
                        judge=true;
                    else
                        judge=false;
                }
                System.out.println(judge);
            }
        }
        else if(n==5)
        {
            if((x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x1-x4==0&&y1-y4==0)||(x2-x3==0&&y2-y3==0)||(x2-x4==0&&y2-y4==0)||(x3-x4==0&&y3-y4==0))
            {
                    System.out.println("points coincide");
            }
            if(k1==k2||(y1-y2)*(x3-x4)-(y3-y4)*(x1-x2)==0)
                System.out.println("is parallel lines,have no intersection point");
            else
            {
                x=(x3*(y3-y4)*(x2-x1)-x1*(y2-y1)*(x3-x4)+(y1-y3)*(x2-x1)*(x3-x4))/((y3-y4)*(x2-x1)-(y2-y1)*(x3-x4));
                y=(y1*(x2-x1)*(y3-y4)-y3*(x3-x4)*(y2-y1)+(x3-x1)*(y3-y4)*(y2-y1))/((x2-x1)*(y3-y4)-(x3-x4)*(y2-y1));
                if((x>x1&&x<x2)||(x>x2&&x<x1))
                {
                    if((y>y1&&y<y2)||(y>y2&&y<y1))
                    {
                        judge2=true;
                    }
                    else
                        judge2=false;
                        
                }
                else if((x>x3&&x<x4)||(x>x4&&x<x3))
                {
                    if((y>y3&&y<y4)||(y>y4&&y<y3))
                    {
                        judge2=true;
                    }
                    else
                        judge2=false;
                }
                else 
                    judge2=false;
                System.out.println(x+","+y+" "+judge2);
            }
        }
    }
}

复杂度:

 

 

  4、实验三7-3

源码:

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

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String number = in.nextLine();
        String[] tokens = null;
        for(int i=0;i<number.length();i++)
        {
            tokens=number.split("[:, ]");
        }
        judged.judge1(number,tokens);
    }

}

class judged{
    private static String df= "[1-5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)+";
    
    public static void judge1(String number,String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        if(tokens.length==7&&number.matches(df)&&(n==1||n==2||n==3)){
            threepoints1.three(tokens);
        }
        else if(tokens.length!=7&&number.matches(df)&&(n==1||n==2||n==3))
            System.out.println("wrong number of points");
        else if(tokens.length==11&&number.matches(df)&&n==4){
            fivepoints1.five(tokens);
        }
        else if(tokens.length!=11&&number.matches(df)&&n==4)
            System.out.println("wrong number of points");
        else if(tokens.length==9&&number.matches(df)&&n==5) {
            fourpoints1.four(tokens);
        }
        else if(tokens.length!=9&&number.matches(df)&&n==5)
            System.out.println("wrong number of points");
        else
            System.out.println("Wrong Format");
    }
}

class threepoints1{
    public static void three(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        Double x3 = Double.parseDouble(tokens[5]);
        Double y3 = Double.parseDouble(tokens[6]);
        double a,b,c,A,B,C;
        double meter,area;
        double[] core;
        boolean judge1=false,judge2=false;
        double k1,k2;
        k1=(y1-y2)/(x1-x2);
        k2=(y3-y2)/(x3-x2);
        a=length1.longth(x1,x2,y1,y2);
        b=length1.longth(x1,x3,y1,y3);
        c=length1.longth(x2,x3,y2,y3);
        if(n==1)
        {
            if(k1==k2||(y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)==0||(x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x2-x3==0&&y2-y3==0))
                System.out.println("data error");
            else
            {
                if(a==b&&b==c)
                    judge1=true;
                if(a==b||a==c||b==c)
                    judge2=true;
                System.out.println(judge2+" "+judge1);
            }
        }
        else if(n==2)
        {
            if(k1==k2||(y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)==0||(x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x2-x3==0&&y2-y3==0))
                System.out.println("data error");
            else
            {
                DecimalFormat df1 = new DecimalFormat("0.0#####");
                meter=a+b+c;
                area=0.5*b*c*triangle.sin(a,b,c);
                core=nodical.nod(x1,y1,(x2+x3)/2,(y2+y3)/2,x2,y2,(x1+x3)/2,(y1+y3)/2);
                System.out.println(df1.format(meter)+" "+df1.format(area)+" "+df1.format(core[0])+","+df1.format(core[1]));
            }
        }
        else if(n==3) 
        {
            if(k1==k2||(y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)==0||(x1-x2==0&&y1-y2==0)||(x1-x3==0&&y1-y3==0)||(x2-x3==0&&y2-y3==0))
                System.out.println("data error");
            else
            {
                boolean judge3=false,judge4=false,judge5=false;
                A=triangle1.cos(a,b,c);
                B=triangle1.cos(b,a,c);
                C=triangle1.cos(c,b,a);
                if(A>0||B>0||C>0)
                {
                    if(A>0||B>0||C>0)
                    {
                        if(A<0||B<0||C<0)
                            judge3=true;
                        else if(A==0||B==0||C==0)
                            judge4=true;
                        else
                            judge5=true;
                    }
                }
                System.out.println(judge3+" "+judge4+" "+judge5);
            }
        }
    }
}

class length1{
    public static double longth(double x1,double x2,double y1,double y2) {
        double l1,l;
        l1=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
        l=Math.sqrt(l1);
        return l;
    }
}

class triangle{
    public static double sin(double a,double b,double c) {
        double num,num1;
        num=(b*b+c*c-a*a)/(2*b*c);
        num1=Math.sqrt(1-num*num);
        return num1;
    }
}

class triangle1{
    public static double cos(double a,double b,double c) {
        double num;
        num=(b*b+c*c-a*a)/(2*b*c);
        return num;
    }
}

class nodical{
    public static double[] nod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
        double x,y;
        double[] a=new double[2];
        x=(x3*(y3-y4)*(x2-x1)-x1*(y2-y1)*(x3-x4)+(y1-y3)*(x2-x1)*(x3-x4))/((y3-y4)*(x2-x1)-(y2-y1)*(x3-x4));
        y=(y1*(x2-x1)*(y3-y4)-y3*(x3-x4)*(y2-y1)+(x3-x1)*(y3-y4)*(y2-y1))/((x2-x1)*(y3-y4)-(x3-x4)*(y2-y1));
        a[0]=x;
        a[1]=y;
        return a;
    }
}

class fivepoints1{
    public static void five(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        Double x3 = Double.parseDouble(tokens[5]);
        Double y3 = Double.parseDouble(tokens[6]);
        Double x4 = Double.parseDouble(tokens[7]);
        Double y4 = Double.parseDouble(tokens[8]);
        Double x5 = Double.parseDouble(tokens[9]);
        Double y5 = Double.parseDouble(tokens[10]);
        double[] core1,core2,core3;
        double a,b,c,area,area1,area2;
            if(x1-x2==0&&y1-y2==0)
                System.out.println("points coincide");
            else
            {
                if((y3-y4)*(x4-x5)-(y4-y5)*(x3-x4)==0||(x3==x4&&y3==y4)||(x3==x5&&y3==y5)||(x4==x5&&y4==y5))
                    System.out.println("data error");
                else
                {
                    System.out.println("1");
                }
            }
    }
}



class fourpoints1{
    public static void four(String[] tokens) {
        Double n = Double .parseDouble(tokens[0]);
        Double x1 = Double.parseDouble(tokens[1]);
        Double y1 = Double.parseDouble(tokens[2]);
        Double x2 = Double.parseDouble(tokens[3]);
        Double y2 = Double.parseDouble(tokens[4]);
        Double x3 = Double.parseDouble(tokens[5]);
        Double y3 = Double.parseDouble(tokens[6]);
        Double x4 = Double.parseDouble(tokens[7]);
        Double y4 = Double.parseDouble(tokens[8]);
        if(((x2==x3&&y2==y3)||(x2==x4&&y2==y4)||(x3==x4&&y3==y4))||Math.abs((y4-y2)*(x3-x2)-(y3-y2)*(x4-x2))==0) {
            System.out.println("data error");
            return;
        }
        if(Math.abs((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1))==0) {
            System.out.println("on the triangle");
        return;
        }
        if(Math.abs((y4-y1)*(x2-x1)-(y2-y1)*(x4-x1))==0) {
            System.out.println("on the triangle");
        return;
        }
        if(Math.abs((y4-y1)*(x3-x1)-(y3-y1)*(x4-x1))==0) {
            System.out.println("on the triangle");
        return;
        }
        double max,max2,q,w;
        boolean falg=false;
        int t=0;
        max=max2=q=w=0;
        Random rand = new Random();
         max=(x2>x3? x2:x3)>x4? (x2>x3? x2:x3):x4;
         max2=(y2<y3? y2:y3)<y4? (y2<y3? y2:y3):y4;
        double randNumber1 =rand.nextInt(99) + max;
        double randNumber2 =rand.nextInt(99) + max2;
        if(Judge.judge3(x1, y1, randNumber1, randNumber2, x2, y2, x3, y3)==1) {
            t++;
        }
        if(Judge.judge3(x1, y1, randNumber1, randNumber2, x2, y2, x4, y4)==1) {
            t++;
        }
        if(Judge.judge3(x1, y1, randNumber1, randNumber2, x3, y3, x4,y4)==1) {
            t++;
        }
        if(t==1) {
            System.out.println("in the triangle");
        }
        else
            System.out.println("outof triangle");
    }

}
class Judge{
    public static int judge3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
        double x,y;
        x=y=0;
        x=(x3*(y3-y4)*(x2-x1)-x1*(y2-y1)*(x3-x4)+(y1-y3)*(x2-x1)*(x3-x4))/((y3-y4)*(x2-x1)-(y2-y1)*(x3-x4));
        y=(y1*(x2-x1)*(y3-y4)-y3*(x3-x4)*(y2-y1)+(x3-x1)*(y3-y4)*(y2-y1))/((x2-x1)*(y3-y4)-(x3-x4)*(y2-y1));
        if(((x>x1&&x<x2)||(x<x1&&x>x2))&&((y>y1&&y<y2)||(y<y1&&y>y2))&&((x>x3&&x<x4)||(x<x3&&x>x4))&&((y>y3&&y<y4)||(y<y3&&y>y4)))
            return 1;
        else
            return 0;
    }

}
    

复杂度:

 

 

 

 

3、踩坑心得:

  1、第一次大作业中7-2在第一次进行提交的不能保留小数

 

   2、第一次大作业中7-7中不知道那个点不能过,但是测试样例和自己输入的极限样例都可以过,但是运行得不到满分,可能是自己还没想到那种情况吧!

   3、第二次大作业中的7-3学号识别,一开始审题不清导致一直有个点不能过,要害有个前提是20级。

 

  4、第二次的大作业中只有三个题目但是难度最大的是第二个题目,里面对于串口字符的加法有很大的坑有的要+11,但有的只要+10。

 

 

 

   5、第三次的大作业我一个都没有满分,因为我倒在了判断错误格式上了,但是我觉得我将点输入多了的情况判断,正确的格式判断,其余的就是格式错误的格式了,但是这样交上去仍然不行。

 4、改进建议:

  1、第二次7-2我认为可以将串口字符的判断可以重新建立一个类,进行判断串口字符是否符合,如果符合的话,然后再建立一个新类去进行输出的判断。

  2、在第三次大作业中我可以将一些点与线的的距离,点与点的距离等等,做一个类,然后在一些需要的地方去引用,判断,而不是将这些代码堆在一起,照成代码的复杂性高,可读性不高,而且在后期进行修改时会遇到一些麻烦,并且将一些类与方法的调用更明了一些。

  eg:1、算点与点的长度    

class length1{
public static double longth(double x1,double x2,double y1,double y2) {
double l1,l;
l1=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
l=Math.sqrt(l1);
return l;
}
}

解释:传入两个坐标,计算这两个坐标之间的距离

   2、线与线的sin与cos值

 

class triangle{
public static double sin(double a,double b,double c) {
double num,num1;
num=(b*b+c*c-a*a)/(2*b*c);
num1=Math.sqrt(1-num*num);
return num1;
}
}

 

class triangle1{
public static double cos(double a,double b,double c) {
double num;
num=(b*b+c*c-a*a)/(2*b*c);
return num;
}
}

解释 :先用余弦定理求出cos再用1-cos*cos开根号变成sin。

 

   3、交点

class nodical{
public static double[] nod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
double x,y;
double[] a=new double[2];
x=(x3*(y3-y4)*(x2-x1)-x1*(y2-y1)*(x3-x4)+(y1-y3)*(x2-x1)*(x3-x4))/((y3-y4)*(x2-x1)-(y2-y1)*(x3-x4));
y=(y1*(x2-x1)*(y3-y4)-y3*(x3-x4)*(y2-y1)+(x3-x1)*(y3-y4)*(y2-y1))/((x2-x1)*(y3-y4)-(x3-x4)*(y2-y1));
a[0]=x;
a[1]=y;
return a;
}
}

解释:传入四个点,经过计算得到交点的x,y。

五、优化和输出

第一次作业无脑合并同类项就好了,其实没啥好说的
第二次作业三角函数合并,我的优化方法可以见我的另一篇博客
第三次作业其实只写了一下合并同类项,因为怕写得多了反而出错,权衡一下收益并没有深入(然后还是出错了QAQ)
其实第三次也是可以划归到合并三角函数的:对于每一个表达式,我们如果可以定义其哈希值,那么sin(x)sin((x^2+1))便显然可以以xx^2+1作为两个sin的键,其指数作为值,这样键值关系就出现了,便依然可以做到对于每个三角函数迅速找到与其对应的cos^2 sin^2 与 1。于是就可以用类似第二次的方法进行优化。但是考虑到嵌套因子的存在,这个优化所需的时间开销应当是会比第二次大不少的。

六、总结  

  对于自己写的程序来说bug还是有很多的,但形成的原因也是有很多的,比如类的混乱,还有随机数的产生导致有些点可以过,但随机数可能使那些点不能过。还有那些正则表达式超级长,看到就脑阔疼,今后我个人会尽量避免写出这样的代码,除非真的万不得已。

  当然在写第三次大作业的时候7-3中的第4次情况写了一大段,但是当运行的时候就不能运行,一开始以为时是单纯的进不了循环,当测试几个样例的时候发现可以进入,那个时候就快崩溃了,当时代码写的太杂了,而且都是很混乱的,导致自己根本改不好,只能无奈的全删了,去骗分。当时就后悔了,应该讲那些代码写进一个类,在类里面进行判断,这样代码既不会写错,到时候改的时候也会更清晰明了一点。不会像这样气急败坏的删除了。

  面向对象不一定比面向过程,面向结果优越,但是优秀的封装和类一定好太多了,第三次作业能写满分,我相信他的封装和代码一定比别人更清晰明了,也更容易看的懂。

  展望:

  经过这三个周的洗礼,我觉得我的代码能力有明显的提升,当然有些地方也有些不足,同时自我学习能力也会更强,因为那些正则表达式是自己在网络上找的资料学习的,当然大家的进步速度也是迅猛的,我也要加把劲。

  在后面两次作业中,能明显感受到代码量增加,第一次全部加起来都没有400行,但是第二三次大作业,一个题目就会有200-350行代码左右,因为里面包含很多算法和方法。

   期望:

  在这三周我学到了很多东西,例如一些函数的运用,同时也吸取了教训,积累了经验。

这篇关于第一次Blog-Java实验1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!