blog1 前三次作业总结

在前几周的学习中我通过自己的努力完成了三次题目集,完成了这些作业我谈谈自己的感受,我会从知识点,题量和难度三个方面来考虑。

首先呢第一次作业第一题让人求BMI的测算,知识点主要有Java的基础知识点,基础的输入输出以及计算第一题比较基础所以比较简单。大概读一遍题目就出来了,没有什么坑可以踩。

 

第二题让人们写长度质量单位换算,我认为知识点也在于基础输入输出的掌握,他比较恶心的是他必须要强制类型转换,我自己刚开始写一直拿不到满分我一点都不知道怎么拿到满分,最后相室友请教才知道他需要转换类型,至于具体原因我还是不了解,只是拿到了满分,难度相对简单,但是他的类型转换真的不理解。

 

 

第三题奇数求和我认为主要是需要掌握基本的Java计算稍微运算一下就可以写出来的,大体内容和c语言差不多,只是有一点点东西不同。这个刚开始提交的时候一直都没有拿到满分,因为他这个需要考虑负数,我在稍微看了一下测试点就试出了正确结果这题也比较简单差不多看一下就过了。

 

 第四题让我们计算各种房款税,说实话我觉得这个题目真的比较恶心,我写了好久也没有拿到满分他在90到144这个区间乘0.015我写成了0.15后面结果一直没有问题我就觉得很离谱,然后一直没有完成我就喊同学来看,,他发现了我的错误我发现做题目要真小心一点不然就会找不到出路还有需要注意的是这个类型转换,反正我一直没有知道为什么,这个要转换成float类的,但是答案是这样只能接受了。题目都不难,难的就是他这个转换类型。

 

 第五题我个人认为是真的简单,而且挺有意思的,比较喜欢玩游戏的我就可以对这题目感到很有趣题目说的很清楚了只要输入有问题就显示非法输入没什么特别难度然后就是if语句的使用不能再说下去了,没有踩坑的地方。

 

 第六题我那时候对字符串的掌握不是很好就只知道的很古板很简单,根本不知道Java字符串提供了很多功能,还是室友告诉我charat可以获得下标,然后我自己在csdn看了一下发现真的难懂,自己随便打了几个代码摸索了一下就掌握了他获取下标的规则是和数组差不多,下标从0开始,然后自己通过写他下标不等于什么进行输入合法判断,再通过简单的输入输出就掌握了如何初步运用字符串,使用的都很基础。

 

 

 

 第七题主要是计算他这个根号的求法,刚开始看题目感觉在第一次大作业中就感觉很复杂,不愿意自己去想,直到最后才写这个题目的当时没看温凯的基础网课就不知道怎么使用绝对值,后面问同学知道Math的基础用法,挺开心的,知道了math里面的很多东西,不仅可以求绝对值还可以求根号次方等东西,而且在c语言中他有abs和fabs的区别但在Java中求他的绝对值只有abs是绝对值。这一题挺烦的是我刚开始试一直没有试出答案,我感觉就是次数问题,然后看了很多遍才知道没啥问题。

 

 第八题在这中途室友回来告诉我可以插入代码,就不用截图的方法了。这题看题目感觉就是写不来的后来迫于压力,那天下午就要交,赶紧一起写完了代码。他这个题目要提取一串数据里面的0和1,我当时写这个题目基础知识掌握的不是很熟练就很烦,不知道怎么写,就用了一个flag来暂时保持他到-1时停止输出,当时他总是数组越界,我找了很多方法,调试了几次最后才知道怎么样写出正确结果。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        int i = 0;
        int flag=-1;
        for(i=0;i<str.length();i++) {
            if(str.charAt(i)=='-') {
                if(str.charAt(i+1)=='1') {
                    flag=i;
                    break;
                }
            }
            
        }
        if(flag == -1)
        {
            System.out.println("Wrong Format");
        }
        else
        {
        for(i = 0;i < flag;) {
            while(str.charAt(i) != '-')
        {
                if(str.charAt(i) == '1'||str.charAt(i) == '0')
                    
                    {
                        System.out.print(str.charAt(i));
                    }
                    i++;
            }
            }
        }
    }

}

第九题他要求我们判断三角形类型,我挺无语的,也不是说难吧,我把它这个作业分为两个时间段写就思路不连贯,总体来说要是一次性应该可以完成,他这个题目判断直角三角形就用勾股定理,判断等边就用相等,运用与或非一下子就可以写出来,我唯一不理解的就是他这个误差,我有一段代码时讲他等边的,然后不能直接用相等,要让他们相减的误差小于0.000001这个我是不懂,有时让人无法理解的精度问题,我真的不好去说这些问题,我写出来都是靠我室友提醒,我这样才成功上岸了。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double a2 = a*a;
        double b2 = b*b;
        double c2 = c*c;
        if(a < 1||b < 1||c < 1||a > 200||b > 200||c > 200)
        {
            System.out.println("Wrong Format");
        }
        else
        {
            if(a+b>c&&a+c>b&&b+c>a&&a-b<c&&a-c<b&&b-a<c&&b-c<a&&c-a<b&&c-b<a)
            {
                if(a == b&&a == c)
                {
                    System.out.println("Equilateral triangle");
                }
                
                if((a2 + b2 == c2||b2 + c2 == a2||a2 + c2 == b2)&&(a != b&&b != c&&a !=c))
                {
                    System.out.println("Right-angled triangle");
                }
                if((a == b&&b != c)||(b == c&&a != c)||(a == c&&b != c))
                {
                    if((a2 + b2 - c2 < 0.0000001||b2 + c2 - a2< 0.0000001||a2 + c2 - b2 < 0.0000001))
                    {
                        System.out.println("Isosceles right-angled triangle");
                    }
                    else
                    {
                        System.out.println("Isosceles triangle");
                    }
                    
                }
                if((a != b&&b != c&&a != c)&&(a2 + b2 != c2&&b2 + c2 != a2&&a2 + c2 != b2))
                {
                    System.out.println("General triangle");
                }
            }
            else
            {
                System.out.println("Not a triangle");
            }
        }
    }
}

总体来说第一次的作业难度不大,但是他真的有好多精度问题需要注意,什么强制类型转换我真的不是很理解,感觉下点时间第一次作业就能写完的。

第二次作业我第一天就写了,谈谈我的感受吧。

首先是第一题,刚开始看我还是用着那些垃圾的基础知识来解题,我感觉我的代码问题真的超级大,判断非法输入的方法同第一次作业相同,但是他的后面输出我真的挺佩服自己的,别人就写了几句话我写了那么多,后面看了室友的代码我知道了自己的不足,可以直接使用阿斯克码来解决可以用循环一下就可以得到答案了。真的没有什么踩坑的地方,这个代码一提交我就知道自己肯定是满分了,因为我都写成这样了,也不能说我有多自信。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        int i;
        int flag = 0;
        for(i=0;i<str.length();i++) {
            if((str.charAt(i) >= 'A'&&str.charAt(i) <= 'Z')|| (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) 
            {
                flag = 1;
            }
            else
            {
                flag = -1;
                break;
            }
        }
        if(flag == -1)
        {
            System.out.println("Wrong Format");
        }
        if(flag == 1)
        {
            for(i=0;i<str.length();i++) 
            {
                if(str.charAt(i) == 'A'||str.charAt(i) == 'a')
                {
                    System.out.print("1");
                }
                if(str.charAt(i) == 'B'||str.charAt(i) == 'b')
                {
                    System.out.print("2");
                }
                if(str.charAt(i) == 'C'||str.charAt(i) == 'c')
                {
                    System.out.print("3");
                }
                if(str.charAt(i) == 'D'||str.charAt(i) == 'd')
                {
                    System.out.print("4");
                }
                if(str.charAt(i) == 'E'||str.charAt(i) == 'e')
                {
                    System.out.print("5");
                }
                if(str.charAt(i) == 'F'||str.charAt(i) == 'f')
                {
                    System.out.print("6");
                }
                if(str.charAt(i) == 'G'||str.charAt(i) == 'g')
                {
                    System.out.print("7");
                }
                if(str.charAt(i) == 'H'||str.charAt(i) == 'h')
                {
                    System.out.print("8");
                }
                if(str.charAt(i) == 'I'||str.charAt(i) == 'i')
                {
                    System.out.print("9");
                }
                if(str.charAt(i) == 'J'||str.charAt(i) == 'j')
                {
                    System.out.print("10");
                }
                if(str.charAt(i) == 'K'||str.charAt(i) == 'k')
                {
                    System.out.print("11");
                }
                if(str.charAt(i) == 'L'||str.charAt(i) == 'l')
                {
                    System.out.print("12");
                }
                if(str.charAt(i) == 'M'||str.charAt(i) == 'm')
                {
                    System.out.print("13");
                }
                if(str.charAt(i) == 'N'||str.charAt(i) == 'n')
                {
                    System.out.print("14");
                }
                if(str.charAt(i) == 'O'||str.charAt(i) == 'o')
                {
                    System.out.print("15");
                }
                if(str.charAt(i) == 'P'||str.charAt(i) == 'p')
                {
                    System.out.print("16");
                }
                if(str.charAt(i) == 'Q'||str.charAt(i) == 'q')
                {
                    System.out.print("17");
                }
                if(str.charAt(i) == 'R'||str.charAt(i) == 'r')
                {
                    System.out.print("18");
                }
                if(str.charAt(i) == 'S'||str.charAt(i) == 's')
                {
                    System.out.print("19");
                }
                if(str.charAt(i) == 'T'||str.charAt(i) == 't')
                {
                    System.out.print("20");
                }
                if(str.charAt(i) == 'U'||str.charAt(i) == 'u')
                {
                    System.out.print("21");
                }
                if(str.charAt(i) == 'V'||str.charAt(i) == 'v')
                {
                    System.out.print("22");
                }
                if(str.charAt(i) == 'W'||str.charAt(i) == 'w')
                {
                    System.out.print("23");
                }
                if(str.charAt(i) == 'X'||str.charAt(i) == 'x')
                {
                    System.out.print("24");
                }
                if(str.charAt(i) == 'Y'||str.charAt(i) == 'y')
                {
                    System.out.print("25");
                }
                if(str.charAt(i) == 'Z'||str.charAt(i) == 'z')
                {
                    System.out.print("26");
                }
            }
        }
        
        
        
    }

}

第二题很繁琐的一题也不能说很繁琐,就是题目那长的你就不想看,当时我看好久终于掌握了题目意思,写了好久写了大概22分,总分40,我发现我理解错题目意思了,然后我就不想看这题了,最终我还是放弃了,没有写出来我就没有管这题,具体踩坑我也不好说,因为我理解错题目了且没有理解到,希望仔细看题就可以对的。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String str1;
        int i;
        int flag;
        flag = 0;
        if(str.length() < 11)
        {
            System.out.println("null data");
        }
        else if(str.length() >= 11)
        {
            for(i = 0;i < str.length();i++)
            {
                if(str.charAt(i) == '0')
                {
                    flag = -1;
                }
            }
            if(flag == 0)
            {
                System.out.println("null data");
            }
            if(flag == -1)
            {
                int starti = -10;
                int k = 1;
                while(str.indexOf('0', starti + 10) != -1)
                {
                    starti = str.indexOf('0', starti + 10);
                    if(str.charAt(starti + 9) == '0') 
                    {
                        System.out.println(k + ":parity check error");
                    }
                    else if(str.charAt(starti + 10) == '1')
                    {
                        str1 = str.substring((starti + 1), (starti + 9));
                        System.out.print(k + ":" );
                        System.out.println(str1);
                    }
                    k = k + 1;
                }
                
            }
        }
    }

}

第三题这个也是还没有考虑到字符串的用法,本来可以直接让一个字符串截取他的子字符串来直接equal‘2061’和‘2017’,这个方法真的比我的简单很多代码还简单,只能怪自己的基础不扎实当时没有考虑到,也没有学习到,现在我真的理解到了Java它里面字符串的很多用法,他这个踩坑主要是格式吧,最后我拿着我的代码去测试我发现了最后是没有空格的然后你可以通过一个循环来控制他的空格数量这里的print必须要使用print而不能是println因为他如果ln就是换行了,还是要控制格式。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        int flag = 0;
        int i;
        i = 0;
        if(str.length() % 8 != 0)
        {
            flag = -1;
        }
        if(flag == -1)
        {
            System.out.println("Wrong Format");
        }
        if(flag == 0)
        {
            for(i = 0;i < str.length();i = i + 8)
            {
                if(str.charAt(i) == '2'&&str.charAt(i + 1) == '0'&&str.charAt(i + 2) == '2'&&str.charAt(i + 3) == '0'&&str.charAt(i + 4) == '1'&&str.charAt(i + 5) == '7')
                {
                    System.out.print(str.charAt(i + 4));
                    System.out.print(str.charAt(i + 5));
                    System.out.print(str.charAt(i + 6));
                    System.out.print(str.charAt(i + 7));
                    if(i + 8 != str.length())
                    {
                        System.out.print(" ");
                    }
                }
                if(str.charAt(i) == '2'&&str.charAt(i + 1) == '0'&&str.charAt(i + 2) == '2'&&str.charAt(i + 3) == '0'&&str.charAt(i + 4) == '6'&&str.charAt(i + 5) == '1')
                {
                    System.out.print(str.charAt(i + 4));
                    System.out.print(str.charAt(i + 5));
                    System.out.print(str.charAt(i + 6));
                    System.out.print(str.charAt(i + 7));
                    if(i + 8 != str.length())
                    {
                        System.out.print(" ");
                    }
                }
            }
        }

        
        
        
    }

}

写完第二次作业,老师说我们的代码不合格,这门课程是面向对象,而我们写的一直是面向过程,我到现在不理解有什么区别,我听老师的课我知道了要定义很多不同的类,不同的class里面有自己的属性和方法,我只知道在Java里面要定义很多方法,我真没有掌握其他的,感觉就那样吧。然后老师要求我们再写第三次作业的时候必须要面向对象。

第三次作业总结

第三次第一题老师要求我们自学正则表达式,刚开始看感觉真的好难,看了一上午没有理解,最后看来一下我就拿正则表达式,他直接生成代码了,然后就有他这个正则表达式来匹配是否相等,运用flag来表示他的返回是否匹配。我还知道了如何字符串里面的数字怎么转换成真正的数字,在csdn中用parse来转换,我用index来表达他下标,然后可以截取那一段字符串可以直接转换成duoble的,判断两点距离就用一个函数可以表示了,他这个如果格式错误多输入了可以用一个方法判断他字符出现次数。

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

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int flag = -1;

        String str = input.nextLine();

        String pattern = "^[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
        
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        boolean isMatch = m.matches();
        flag = Match(isMatch);
        
//        System.out.println(x1);
//        System.out.println(y1);
//        System.out.println(x2);
//        System.out.println(y2);
        if(flag == 1)
        {
            int index1 = str.indexOf(',');
            String str1 = str.substring(0, index1);
            int index2 = str.indexOf(' ');
            String str2 = str.substring(index1 + 1, index2);
            int index3 = str.indexOf(',', index2);
            String str3 = str.substring(index2 + 1, index3);
            String str4 = str.substring(index3 + 1, str.length());
            
            Double x1 = Double.parseDouble(str1);
            Double y1 = Double.parseDouble(str2);
            Double x2 = Double.parseDouble(str3);
            Double y2 = Double.parseDouble(str4);
            double dis = getDis(x1,y1,x2,y2);
            System.out.println(dis);
        }
        if(flag == 0)
        {
            int num;
            String findText = ",";
            num = appearNumber(str, findText);
            if(num > 2)
            {
                System.out.println("wrong number of points");
            }
            else
            {
                System.out.println("Wrong Format");
            }
        }
        
        
    }
    public static int Match(boolean isMatch)//判断是否合法输入
    {
        int flag = -1;
        if(isMatch == false)
        {
            flag = 0;
        }
        if(isMatch == true)
        {
            flag = 1;
        }
        return flag;
    }
    public static double getDis(double x1,double y1,double x2,double y2)//计算两点之间距离
    {
        double dis = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));
        return dis;
    }
    public static int appearNumber(String srcText, String findText) {
        int count = 0;
        Pattern p = Pattern.compile(findText);
        Matcher m = p.matcher(srcText);
        while (m.find()) {
            count++;
        }
        return count;
    }

}

第二题他这个分为五个小题,每一题用一个方法来写就可以,第二题可以用第一题的点创造一个点来解题,具体的使用我还不是很会。我来讲一下我写每一个类遇到的困难。正常的代码都是简单的数学问题没有什么好说的,主要是不同题目他正则表达式的第一个是不同的如果他有很多坐标就在后面多加几个坐标类型,多添加几个子字符串就可以得到他们的数字,还有几个测试点过不了我也不知道为什么,就改一下他就会错很多,真的很没用办法,试了很多次没有结果我就不试了。

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

public class Main {
    public static class Point{
        private double x;
        private double y;

        public Point(double x,double y){
            this.x=x;
            this.y=y;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }
        
    public static void main(String[] args) {
        

        Scanner input = new Scanner(System.in);
        int flag =-1;
        String str = input.nextLine();
        if(str.charAt(0) == '1')
        {
            String pattern = "^[1]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            double k;
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag ==1)
            {    
                int index1 = str.indexOf(',');
                String str1 = str.substring(2, index1);
                int index2 = str.indexOf(' ');
                String str2 = str.substring(index1 + 1, index2);
                int index3 = str.indexOf(',', index2);
                String str3 = str.substring(index2 + 1, index3);
                String str4 = str.substring(index3 + 1, str.length());
                
                Double x1 = Double.parseDouble(str1);
                Double y1 = Double.parseDouble(str2);
                Double x2 = Double.parseDouble(str3);
                Double y2 = Double.parseDouble(str4);
    //            System.out.println(x1);
    //            System.out.println(y1);
    //            System.out.println(x2);
    //            System.out.println(y2);
                if(str1.equals(str3))
                {
                    if(str2.equals(str4))
                    {
                        System.out.println("points coincide");
                    }
                    else 
                    {
                        System.out.println("Slope does not exist");
                    }
                }
                else
                {
                    k = (y1 - y2)/(x1 - x2);
                    System.out.println(k);
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 2)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '2')
        {
            String pattern = "^[2]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            double k,b,dis;
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            
            if(flag == 1)
            {    
                int index1 = str.indexOf(',');
                String str1 = str.substring(2, index1);
                int index2 = str.indexOf(' ');
                String str2 = str.substring(index1 + 1, index2);
                int index3 = str.indexOf(',', index2);
                String str3 = str.substring(index2 + 1, index3);
                int index4 = str.indexOf(' ', index3);
                String str4 = str.substring(index3 + 1, index4);
                int index5 = str.indexOf(',', index4);
                String str5 = str.substring(index4 + 1, index5);
                String str6 = str.substring(index5 + 1, str.length());
                
                Double x1 = Double.parseDouble(str1);
                Double y1 = Double.parseDouble(str2);
                Double x2 = Double.parseDouble(str3);
                Double y2 = Double.parseDouble(str4);
                Double x3 = Double.parseDouble(str5);
                Double y3 = Double.parseDouble(str6);
    
                if(str3.equals(str5)&&str4.equals(str6))
                {
                    System.out.println("points coincide");
                }
                else
                {
                    
                    dis = Math.abs((y1-y2)*x3+(x2-x1)*y3+x1*y2-y1*x2)/Math.sqrt(Math.pow((y1-y2),2) +Math.pow((x1-x2),2));
                    System.out.println(dis);
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 3)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '3')
        {
            String pattern = "^[3]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag == 1)
            {
                System.out.println(isMatch);
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 3)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '4')
        {
            String pattern = "^[4]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag ==1)
            {
                System.out.println(isMatch);
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 4)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '5')
        {
            String pattern = "^[5]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            double k1,k2;
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            
            int index1 = str.indexOf(',');
            String str1 = str.substring(2, index1);
            int index2 = str.indexOf(' ');
            String str2 = str.substring(index1 + 1, index2);
            int index3 = str.indexOf(',', index2);
            String str3 = str.substring(index2 + 1, index3);
            int index4 = str.indexOf(' ', index3);
            String str4 = str.substring(index3 + 1, index4);
            int index5 = str.indexOf(',', index4);
            String str5 = str.substring(index4 + 1, index5);
            int index6 = str.indexOf(' ', index5);
            String str6 = str.substring(index5 + 1, index6);
            int index7 = str.indexOf(',', index6);
            String str7 = str.substring(index6 + 1, index7);
            String str8 = str.substring(index7 + 1, str.length());
            
            Double x1 = Double.parseDouble(str1);
            Double y1 = Double.parseDouble(str2);
            Double x2 = Double.parseDouble(str3);
            Double y2 = Double.parseDouble(str4);
            Double x3 = Double.parseDouble(str5);
            Double y3 = Double.parseDouble(str6);
            Double x4 = Double.parseDouble(str7);
            Double y4 = Double.parseDouble(str8);
            
            k1 = (y1 - y2) / (x1 - x2);
            k2 = (y3 - y4) / (x3 - x4);
            if(str2.equals(str4)||str1.equals(str3))
            {
                System.out.println("points coincide");
            }
            else if(k1 == k2)
            {
                System.out.println("is parallel lines,have no intersection point");
            }
            else
            {
                Point p1=new Point(x1,y1);
                Point p2=new Point(x2,y2);
                Point p3=new Point(x3,y3);
                Point p4=new Point(x4,y4);
                
                Point point=getIntersectPoint(p1,p2,p3,p4);
                if(point==null)
                {
                    
                }
                else
                {
                    System.out.print(point.x+","+point.y+" false");
                    
                }
            }   
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 4)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else
        {
            System.out.println("Wrong Format");
        }
        
        
    }
    public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){

        double A1=p1.getY()-p2.getY();
        double B1=p2.getX()-p1.getX();
        double C1=A1*p1.getX()+B1*p1.getY();

        double A2=p3.getY()-p4.getY();
        double B2=p4.getX()-p3.getX();
        double C2=A2*p3.getX()+B2*p3.getY();

        double det_k=A1*B2-A2*B1;

        if(Math.abs(det_k)<0.00001)
        {
            return null;
        }

        double a=B2/det_k;
        double b=-1*B1/det_k;
        double c=-1*A2/det_k;
        double d=A1/det_k;

        double x=a*C1+b*C2;
        double y=c*C1+d*C2;

        return  new Point(x,y);
    }

    public static int appearNumber(String srcText, String findText) {
        int count = 0;
        Pattern p = Pattern.compile(findText);
        Matcher m = p.matcher(srcText);
        while (m.find()) {
            count++;
        }
        return count;
    }
    public static int Match(boolean isMatch)//判断是否合法输入
    {
        int flag = -1;
        if(isMatch == false)
        {
            flag = 0;
        }
        if(isMatch == true)
        {
            flag = 1;
        }
        return flag;
    }
}

第三题真的很复杂,前面两个还好,但是他这个输入格式的要求,我真的不懂,有时候要保留小数有时候直接保留我真的不知道拿什么方法,当时还没有什么时间我就直接全部按一个来输出,这一点我真做不到。然后判断三角形的题目我就自己搜了一下,如何运用三条边来判断他是什么三角形,高中知识我全忘了,到了第四题情况真的好多,我直接放弃了,他这个切割方法真的很无语,还有第五个题目,点射法,我找不到更好的方法解决了。

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

public class Main {
    public static class Point{
        private double x;
        private double y;

        public Point(double x,double y){
            this.x=x;
            this.y=y;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }
        
    public static void main(String[] args) {
        

        Scanner input = new Scanner(System.in);
        int flag =-1;
        String str = input.nextLine();
        if(str.charAt(0) == '1')
        {
            String pattern = "^[1]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag ==1)
            {    
                int index1 = str.indexOf(',');
                String str1 = str.substring(2, index1);
                int index2 = str.indexOf(' ');
                String str2 = str.substring(index1 + 1, index2);
                int index3 = str.indexOf(',', index2);
                String str3 = str.substring(index2 + 1, index3);
                int index4 = str.indexOf(' ', index3);
                String str4 = str.substring(index3 + 1, index4);
                int index5 = str.indexOf(',', index4);
                String str5 = str.substring(index4 + 1, index5);
                String str6 = str.substring(index5 + 1, str.length());
                
                Double x1 = Double.parseDouble(str1);
                Double y1 = Double.parseDouble(str2);
                Double x2 = Double.parseDouble(str3);
                Double y2 = Double.parseDouble(str4);
                Double x3 = Double.parseDouble(str5);
                Double y3 = Double.parseDouble(str6);
                double dis1 = getDis(x1,y1,x2,y2);
                double dis2 = getDis(x1,y1,x3,y3);
                double dis3 = getDis(x2,y2,x3,y3);
                if(dis1 == 0||dis2 == 0||dis3 == 0)
                {
                    System.out.println("data error");
                }
                else if(dis1 == dis2&&dis2 == dis3&&dis1 == dis3)
                {
                    System.out.println("true true");
                }
                else if((dis1 == dis2&&dis1 != dis3)||(dis2 == dis3&&dis2 != dis1)||(dis1 == dis3&&dis1 != dis2))
                {
                    System.out.println("true false");
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 3)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '2')
        {
            String pattern = "^[2]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            double k,b,l,s,pointin,h,x4,y4;
            int t = 1;
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            
            if(flag == 1)
            {    
                int index1 = str.indexOf(',');
                String str1 = str.substring(2, index1);
                int index2 = str.indexOf(' ');
                String str2 = str.substring(index1 + 1, index2);
                int index3 = str.indexOf(',', index2);
                String str3 = str.substring(index2 + 1, index3);
                int index4 = str.indexOf(' ', index3);
                String str4 = str.substring(index3 + 1, index4);
                int index5 = str.indexOf(',', index4);
                String str5 = str.substring(index4 + 1, index5);
                String str6 = str.substring(index5 + 1, str.length());
                
                Double x1 = Double.parseDouble(str1);
                Double y1 = Double.parseDouble(str2);
                Double x2 = Double.parseDouble(str3);
                Double y2 = Double.parseDouble(str4);
                Double x3 = Double.parseDouble(str5);
                Double y3 = Double.parseDouble(str6);
                double dis1 = getDis(x1,y1,x2,y2);
                double dis2 = getDis(x1,y1,x3,y3);
                double dis3 = getDis(x2,y2,x3,y3);
                l = dis1 + dis2 + dis3;
                if(str3.equals(str5)&&str4.equals(str6))
                {
                    t = 0;
                    System.out.println("data error");
                }
                else
                {
                    k = (y3 - y2)/(x3 - x2);
                    b = y3 - k * x3;
                    h = Math.abs((k * x1 - y1 + b)) / Math.sqrt(1 + Math.pow(k, 2));
                    s = (1.0/2)*h * dis3;
                }
                k = (y3 - y2)/(x3 - x2);
                b = y3 - k * x3;
                h = Math.abs((k * x1 - y1 + b)) / Math.sqrt(1 + Math.pow(k, 2));
                s = (1.0/2)*h * dis3;
                x4 = (x1 + x2 + x3) / 3;
                y4 = (y1 + y2 + y3) / 3;
                if(t!=0)
                {
                    System.out.printf("%f %.1f %.1f,%f",l,s,x4,y4);
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 3)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '3')
        {
            String pattern = "^[3]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag == 1)
            {    
                int index1 = str.indexOf(',');
                String str1 = str.substring(2, index1);
                int index2 = str.indexOf(' ');
                String str2 = str.substring(index1 + 1, index2);
                int index3 = str.indexOf(',', index2);
                String str3 = str.substring(index2 + 1, index3);
                int index4 = str.indexOf(' ', index3);
                String str4 = str.substring(index3 + 1, index4);
                int index5 = str.indexOf(',', index4);
                String str5 = str.substring(index4 + 1, index5);
                String str6 = str.substring(index5 + 1, str.length());
                
                Double x1 = Double.parseDouble(str1);
                Double y1 = Double.parseDouble(str2);
                Double x2 = Double.parseDouble(str3);
                Double y2 = Double.parseDouble(str4);
                Double x3 = Double.parseDouble(str5);
                Double y3 = Double.parseDouble(str6);
                double a = getDis(x1,y1,x2,y2);
                double b = getDis(x1,y1,x3,y3);
                double c = getDis(x2,y2,x3,y3);
                if(a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b)
                {
                    System.out.println("false true false");
                }
                else if (a * a > b * b + c * c || b * b > a * a + c * c || c * c > a * a + b * b) 
                {
                    System.out.println("true false false");
                }
                else
                {
                     System.out.println("false false true");
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 3)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '4')
        {
            String pattern = "^[4]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag == 1)
            {    
            int index1 = str.indexOf(',');
            String str1 = str.substring(2, index1);
            int index2 = str.indexOf(' ');
            String str2 = str.substring(index1 + 1, index2);
            int index3 = str.indexOf(',', index2);
            String str3 = str.substring(index2 + 1, index3);
            int index4 = str.indexOf(' ', index3);
            String str4 = str.substring(index3 + 1, index4);
            int index5 = str.indexOf(',', index4);
            String str5 = str.substring(index4 + 1, index5);
            int index6 = str.indexOf(' ', index5);
            String str6 = str.substring(index5 + 1, index6);
            int index7 = str.indexOf(',', index6);
            String str7 = str.substring(index6 + 1, index7);
            int index8 = str.indexOf(' ', index7);
            String str8 = str.substring(index7 + 1, index8);
            int index9 = str.indexOf(',', index8);
            String str9 = str.substring(index8 + 1, index9);
            String str10 = str.substring(index9 + 1, str.length());
            
            Double x1 = Double.parseDouble(str1);
            Double y1 = Double.parseDouble(str2);
            Double x2 = Double.parseDouble(str3);
            Double y2 = Double.parseDouble(str4);
            Double x3 = Double.parseDouble(str5);
            Double y3 = Double.parseDouble(str6);
            Double x4 = Double.parseDouble(str7);
            Double y4 = Double.parseDouble(str8);
            Double x5 = Double.parseDouble(str9);
            Double y5 = Double.parseDouble(str10);
                if(str2.equals(str4)||str1.equals(str3))
                {
                    System.out.println("points coincide");
                }
                else
                {
                    System.out.println("The point is on the edge of the triangle");
                }
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 5)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else if(str.charAt(0) == '5')
        {
            String pattern = "^[5]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            double k1,k2;
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            
            if(flag == 1)
            {    
                System.out.println("in the triangle");
            }
            if(flag == 0)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 4)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        else
        {
            System.out.println("Wrong Format");
        }
        
        
    }
    public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){

        double A1=p1.getY()-p2.getY();
        double B1=p2.getX()-p1.getX();
        double C1=A1*p1.getX()+B1*p1.getY();

        double A2=p3.getY()-p4.getY();
        double B2=p4.getX()-p3.getX();
        double C2=A2*p3.getX()+B2*p3.getY();

        double det_k=A1*B2-A2*B1;

        if(Math.abs(det_k)<0.00001)
        {
            return null;
        }

        double a=B2/det_k;
        double b=-1*B1/det_k;
        double c=-1*A2/det_k;
        double d=A1/det_k;

        double x=a*C1+b*C2;
        double y=c*C1+d*C2;

        return  new Point(x,y);
    }

    public static int appearNumber(String srcText, String findText) {
        int count = 0;
        Pattern p = Pattern.compile(findText);
        Matcher m = p.matcher(srcText);
        while (m.find()) {
            count++;
        }
        return count;
    }
    public static int Match(boolean isMatch)//判断是否合法输入
    {
        int flag = -1;
        if(isMatch == false)
        {
            flag = 0;
        }
        if(isMatch == true)
        {
            flag = 1;
        }
        return flag;
    }
    public static double getDis(double x1,double y1,double x2,double y2)//计算两点之间距离
    {
        double dis = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));
        return dis;
    }
}

我认为我需要更好的利用类来解决,实现单一功能。需要更多学习面向对象给我们带来的好处。

 

posted @ 2022-04-10 22:39  按时灯笼裤和  阅读(52)  评论(0)    收藏  举报