首页 > 其他 > 详细

题目集1-3的总结性blog

时间:2021-04-03 00:21:34      阅读:20      评论:0      收藏:0      [点我收藏+]

一、前言

题目集一

第一次题目集共有八道题。第一题和第五题知识点主要关于数据的输入输出以及计算,第五题也考验了数据类型的转换,第二题至第四题以及第八题知识点主要关于if else语句的使用,其中第二题还涉及到了字符的转换,第六题知识点主要关于数组的使用,第七题知识点关于排序的方法。总体难度偏简单,其中难度较高的两题为第四题以及第八题。第四题计算过程过于复杂,而第八题的判断类型过多容易混淆。

题目集二

第二次题目集共有五道题。第一题知识点主要关于字符串的相关使用方法,第二题知识点主要关于数组的使用以及排序方法,第三题至第五题知识点主要关于方法的使用,以及对于日期输入输出是否正确的判断。此次题目集难度较为提升,难度较高的为后三题,其中关于日期的判断十分繁琐。

题目集三

第三次题目集共有三道题。此次题目集考验的知识点为对类的使用。第一题知识点主要关于对类的私有属性的调用,第二题知识点主要关于对类中方法的调用,第三题 知识点主要关于正则表达式的使用以及关于字符串的各种方法的使用。此次题目集难度较大,尤其对于第三题中对于输入输出的要求较高,并且一元多项式字符串较复杂,不便操作。

二、设计与分析

题目集一

7-8

本题是对于三角形类型的判断。输入三角形的三条边,判断该三角形为什么类型的三角形。

首先输入三角形的三条边并判断输入是否合法,当输入小于1或者大于200时则输出"Wrong Format":

double a,b,c;
        a = in.nextDouble();
        b = in.nextDouble();
        c = in.nextDouble();
        if(a<1||b<1||c<1||a>200||b>200||c>200)
        {
            System.out.println("Wrong Format");
        }

判断三条边是否能够组成三角形,即当两边之和小于等于第三边时,输出"Not a triangle":

if(a+b<=c||a+c<=b||b+c<=a)
            {
                System.out.println("Not a triangle");
            }

首先判断是否为等腰三角形,之后第一个if判断是否为等边三角形,第二个判断是否为等腰直角三角形,第三个判断是否为普通的等腰三角形:

if(a-b==0||b-c==0||a-c==0)
                {
                    if(a-b==0&&a-c==0&&b-c==0)
                    {
                        System.out.println("Equilateral triangle");
                    }
                    else if((a*a+b*b)-(c*c)<0.00001||(a*a+c*c)-(b*b)<0.00001||(b*b+c*c)-(a*a)<0.00001)
                    {
                        System.out.println("Isosceles right-angled triangle");
                    }

                    else
                    {
                        System.out.println("Isosceles triangle");
                    }
                }

判断是否为普通的直角三角形并输出:

else if((a*a+b*b)-(c*c)==0||(a*a+c*c)-(b*b)==0||(b*b+c*c)-(a*a)==0)
                {
                    System.out.println("Right-angled triangle");
                }

当以上条件都不满足时,则为一般三角形:

else
                {
                    System.out.println("General triangle");
                }

题目集二

7-4

本题求输入日期的下一天。输入年月日的值(均为整型数),输入该日期的下一天。

isLeapYear()方法判断输入年份是否为闰年,如果是闰年则返回true,否则返回false:

public static boolean isLeapYear(int year) {
        boolean a = false;
        if(year%4==0&&year%100!=0||year%400==0)
            a = true;
        return a;
    }

checkInputValidity()方法判断输入是否正确,月份在1-12之间,日期大于0并且小于该月份的天数,年份在1820-2020之间,如果输入错误则返回true,否则返回false:

public static boolean checkInputValidity(int year,int month,int day)
    {
        boolean a = false;
        if(!isLeapYear(year))
        {
            if(year<1820||year>2020||month<1||month>12||day<1||day>31||month==2&&day==29||(month==4||month==6||month==9||month==11)&&day==31)
            {
                a = true;
            }
        }
        else
        {
            if(year<1820||year>2020||month<1||month>12||day<1||day>31||month==2&&(day==30||day==31)||(month==4||month==6||month==9||month==11)&&day==31)
            {
                a = true;
            }
        }
        return a;
    }

nextDate()方法求下一天。先判断是否为闰年决定二月份的天数,如果日期为该月的最后一天则下一天为下个月的第一天,如果日期为该年的最后一天则下一天为下一年的1月1日,其余直接日期加一:

public static void nextDate(int year,int month,int day) {
        if(isLeapYear(year))
        {
            if(month==2&&day==29)
            {
                month++;
                day = 1;
            }
            else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
            {
                month++;
                day = 1;
            }
            else if((month==4||month==6||month==9||month==11)&&day==30)
            {
                month++;
                day = 1;
            }
            else if(month==12&&day==31)
            {
                year++;
                month = 1;
                day = 1;
            }
            else
            {
                day++;
            }
            System.out.println("Next date is:"+year+"-"+month+"-"+day);
        }
        else
        {
            if(month==2&&day==28)
            {
                month++;
                day = 1;
            }
            else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
            {
                month++;
                day = 1;
            }
            else if((month==4||month==6||month==9||month==11)&&day==30)
            {
                month++;
                day = 1;
            }
            else if(month==12&&day==31)
            {
                year++;
                month = 1;
                day = 1;
            }
            else
            {
                day++;
            }
            System.out.println("Next date is:"+year+"-"+month+"-"+day);
        }
    }

7-5

本题求输入日期的前N天。输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。

isLeapYear()方法判断输入年份是否为闰年,如果是闰年则返回true,否则返回false:

public static boolean isLeapYear(int year) {
        boolean a = false;
        if(year%4==0&&year%100!=0||year%400==0)
            a = true;
        return a;
    }

checkInputValidity()方法判断输入是否正确,月份在1-12之间,日期大于0并且小于该月份的天数,年份在1820-2020之间,如果输入错误则返回true,否则返回false:

public static boolean checkInputValidity(int year,int month,int day,int n)
    {
        boolean a = false;
        if(!isLeapYear(year))
        {
            if(year<1820||year>2020||month<1||month>12||day<1||day>31
                    ||month==2&&day==29||(month==4||month==6||month==9
                    ||month==11)&&day==31||n<-10||n>10)
            {
                a = true;
            }
        }
        else
        {
            if(year<1820||year>2020||month<1||month>12||day<1||day>31
                    ||month==2&&(day==30||day==31)||(month==4||month==6
                    ||month==9||month==11)&&day==31||n<-10||n>10)
            {
                a = true;
            }
        }
        return a;
    }

daysAgo()方法求输入日期的前N天。

先判断该年是否为闰年决定二月份的天数,当N大于0时,如果输入日期小于输入的N,则月份减一,日期用该月的天数加上输入日期减去N,如果输入月份为一月,则年份也减一,其余直接用输入日期减去N。

if(isLeapYear(year))
            {
                if(month==2&&day+x>29)
                {
                    month++;
                    day = x+day-29;
                }
                else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day+x>31)
                {
                    month++;
                    day = x+day-31;
                }
                else if((month==4||month==6||month==9||month==11)&&day+x>30)
                {
                    month++;
                    day = x+day-30;
                }
                else if(month==12&&day+x>31)
                {
                    year++;
                    month = 1;
                    day = x+day-31;
                }
                else
                {
                    day = day+n;
                }
                System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
            }

当N小于0时,如果输入的N小于输入日期减去输入月份的天数,则月份加一,日期用输入日期减去输入月份的天数和N,如果输入月份为十二月,则年份也加一,其余直接用输入日期减去N。

else
            {
                if(month==2&&day+x>28)
                {
                    month++;
                    day = x+day-28;
                }
                else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day+x>31)
                {
                    month++;
                    day = x+day-31;
                }
                else if((month==4||month==6||month==9||month==11)&&day+x>30)
                {
                    month++;
                    day = x+day-30;
                }
                else if(month==12&&day<=x)
                {
                    year++;
                    month = 1;
                    day = 31-x+day;
                }
                else
                {
                    day = day+x;
                }
                System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
            }

如果N为0,则日期不变:

else if(n==0)
        {
            System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
        }

题目集三

7-2

本题要求定义一个日期类来求输入日期的下一天。定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数。

首先定义Date类并包含三个私有属性year,month,day:

class Date{
    Scanner in = new Scanner(System.in);
    private int year = in.nextInt();
    private int month = in.nextInt();;
    private int day = in.nextInt();

对私有属性进行getter和setter操作:

public int getYear(){
    return year;
}
public void setYear(int year) {
    this.year = year;
}
public int getMonth(){
    return month;
}
public void setMonth(int month) {
    this.month = month;
}
public int getDay(){
    return day;
}
public void setDay(int day) {
    this.day = day;
}

isLeapYear()方法判断输入年份是否为闰年,如果是闰年则返回true,否则返回false:

public boolean isLeapYear(int year) {
    boolean a = false;
    if(year%4==0&&year%100!=0||year%400==0)
        a = true;
    return a;
}

checkInputValidity()方法判断输入是否正确,月份在1-12之间,日期大于0并且小于该月份的天数,年份在1900-2000之间,如果输入错误则返回true,否则返回false:

public boolean checkInputValidity() {
    boolean a = false;
    if(!isLeapYear(year))
    {
        if(year<1900||year>2000||month<1||month>12||day<1||day>31||month==2&&day==29||(month==4||month==6||month==9||month==11)&&day==31)
        {
            a = true;
        }
    }
    else
    {
        if(year<1900||year>2000||month<1||month>12||day<1||day>31||month==2&&(day==30||day==31)||(month==4||month==6||month==9||month==11)&&day==31)
        {
            a = true;
        }
    }
    return a;
}

getNextDate()方法求下一天。先判断是否为闰年决定二月份的天数,如果日期为该月的最后一天则下一天为下个月的第一天,如果日期为该年的最后一天则下一天为下一年的1月1日,其余直接日期加一:

public void getNextDate() {
    if(isLeapYear(year))
    {
        mon_maxnum[2] = 29;
        if(month==12)
        {
            if(day==mon_maxnum[month])
            {
                year++;
                month = 1;
                day = 0;
            }
            else if(day > 0&&day<mon_maxnum[month])
            {
                day++;
            }
            
        }
        if(month<12)
        {
            if(day==mon_maxnum[month])
            {
                month++;
                day = 1;
            }
            else
            {
                day++;
            }
        }
        System.out.println("Next day is:"+year+"-"+month+"-"+day);
    }
    else
    {
        if(month==12)
        {
            if(day==mon_maxnum[month])
            {
                year ++;
                month = 1;
                day = 0;
            }
            else if(day > 0&&day < mon_maxnum[month])
            {
                day++;
            }
            
        }
        if(month<12)
        {
            if(day==mon_maxnum[month])
            {
                month++;
                day = 1;
            }
            else
            {
                day++;
            }
        }
        System.out.println("Next day is:"+year+"-"+month+"-"+day);
    }
}

7-3

本题要求对一元多项式进行求导。编写程序性,实现对简单多项式的导函数进行求解。

以下是本题中所包含的Java类的使用:

import java.util.Scanner;
import java.lang.String;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Math;
import java.math.BigInteger;

在主方法中,首先去除字符串首尾空格后判断常数中间是否有空格,之后去除字符串中所有空格:

str = str.trim();
checkBlank(str);
String a = str.replaceAll(" +", "");

将多项式分为各个单项式并存入对象数组中:

String[] b = a.split("(\\w)[+-]");
        String[] d = new String[b.length];
        int[] num = new int[b.length-1];
        int num1 = 0;
        int len = b.length;
        for(int i=0;i<len-1;i++)
            {
            if(i==0)
            {
                num1 =b[i].length();
            }
            else {
            num1 += b[i].length() + 2 * i - 2 * (i - 1);
            }
                d[i] = b[i] + a.charAt(num1);
            }
            d[len-1] = b[len-1];

判断输入是否合法后判断该项是否为常数项,如果是则直接输出0:

for(int i=0;i<len;i++)
        {
            check(d[i].toString());
        }
        if(a.contains("x") == false)
        {
            System.out.print(0);
        }

对各项求导后拼接在一起存入allString中,并且对输出进行优化使输出符合要求:

String allString = "";    
            for(int j=0;j<len;j++)
            {
                c[j] = derivation(d[j].toString());
            }
            if(len==1)
            {
                allString = c[0];
            }
            else
            {
            for(int k=1;k<len;k++) 
            {
                num[k-1] = a.indexOf(d[k])-1;
            }

            for(int i=1;i<len;i++)
            {
                allString += a.charAt(num[i-1]) + c[i];
            }
            allString = c[0] + allString;
            }
            if(allString.charAt(0)==‘+‘)                    
            {
                allString = allString.substring(1);
            }
            if(allString.charAt(allString.length()-1)==‘+‘||allString.charAt(allString.length()-1)==‘-‘
            ||allString.charAt(allString.length()-1)==‘^‘||allString.charAt(allString.length()-1)==‘*‘)
            {
                allString = allString.substring(0, allString.length()-1);
            }
            StringBuffer stringbuffer = new StringBuffer(allString);
            if(allString.contains("--")==true) 
            {
                int k = allString.indexOf("--");
                stringbuffer.replace(k,k+2,"+");
            }
            if(allString.contains("+-")==true) 
            {
            int k = allString.indexOf("+-");
            stringbuffer.replace(k-1,k+1,"-");
            }
            
            System.out.print(stringbuffer);

checkBlank()方法用于判断输入的常数中间是否有空格,如果有则输出"Wrong Format":

public static void checkBlank(String str) {
        String[] b = str.split("\\d");
        for(int i=0;i<b.length;i++)
        {
            if(b[i].contains("+") == false && b[i].contains("-") == false && b[i].contains("*") == false 
                    && b[i].contains("^") == false && b[i].contains(" ") == true)
            {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
    }

check()方法使用正则表达式来判断输入字符串是否符合题目要求,主要分为系数和指数都不为1,系数为1,指数为1,常数,系数和指数都为1:

public static void check(String str) {
        String regex = "([^0]((\\-?)(\\d+)||\\d)[*][x]\\^[^0]((\\-?)(\\d+)||\\d))"
            +"||((\\-?)(\\d+)||\\d)"
            +"||([+])((\\-?)(\\d+)||\\d)"
            +"||([^0]((\\-?)(\\d+)||\\d)[*][x])"
            +"||([x]\\^[^0]((\\-?)(\\d+)||\\d))"
            +"||[x]";
        boolean b = Pattern.matches(regex,str);
        if(b == false)
            {
                System.out.print("Wrong Format");
                System.exit(0);
            }
    }

derivation()方法用于对输入的一元多项式进行求导,将多项式中的单项式分别传入该方法,先判断该单项式是否为常数项,如果是则直接返回空字符串。再判断系数与指数是否为1,如果皆为1,则直接返回“1”,如果指数为1,则直接返回系数,否则按求导方法求导后返回字符串。

    public static String derivation(String str) {
        int len;
        if(str.contains("x"))
        {
            String[] m = str.split("[*^]");
            len = m.length;
            String n = "";
            if(len==1)
            {
                n = "1";
            }
            if(len==2&&str.contains("*"))
            {        
                n = m[0];
            }
            if(len==2&&str.contains("^"))
            {
                if(m[1].equals("-1"))
                {
                    n = "-x^" + (Integer.valueOf(m[1])-1);
                }
                else if(m[1].equals("1"))
                {
                    n = "1";
                }
                else 
                {
                    n = m[1] + "*x^" + (Integer.valueOf(m[1])-1);
                }
            }
           if(len==3)
            {
                int num = 0;
               if(m[0].length() > 4 || m[len-1].length()> 4)
               {
                   long i = Long.parseLong(m[0]);
                    long j = Long.parseLong(m[len-1]);
                    BigInteger a,b;
                    a = BigInteger.valueOf(i);
                    b = BigInteger.valueOf(j);
                    n = a.multiply(b) + "*x^" + (Integer.valueOf(m[len-1]) - 1);
               }
               else if(m[len-1].equals("1"))
               {
                   n = m[0];
               }
               else
               {
                 num = Integer.parseInt(m[0]) * Integer.parseInt(m[len-1]);
                 n = num + "*x^" + (Integer.valueOf(m[len-1]) - 1);
               }
            }
           if(n.length() > 2 && n.substring(n.length()-2).equals("^1"))
           {
               n = n.substring(0, n.length()-2);
           }
           return n;
        }
        else
        {
            return "";
        }
    }

三、采坑心得

在这三次题目集中,对源码的提交过程出现了许多问题,在解决问题的过程中本人也获得了许多心得。

在题目集一的7-8中,我由于对Java数据偏差的不了解,在判断是否为等腰直角三角形的地方出现错误,导致第十四个测试点过不去:

技术分享图片

在题目集二的7-1中,我对于Java中的许多方法都不清楚,不知道该如何判断输入是否为空:

技术分享图片

在题目集三的7-3中,我忽略了大数的计算,从而当输入数据过大时,程序无法运行:

技术分享图片

通过这三次题目集,我充分认识到自己对于Java的了解还只是冰山一角,Java包含的内容十分丰富,我应该在日常的学习中不断拓展,丰富自己在此领域了解的内容。

四、改进建议

在题目集二的7-5中,老师并未给出类的名称及应使用的方法的名称,所以当我自己对方法进行命名时,会出现使用拼音命名的不规范的行为,应当时刻注意命名的规范性。

在题目集三的7-3中,因为该题难度过高,所以我在写代码过程中忽略了考虑代码的整齐,修正代码时直接在源码上修正后并不进行排版,导致代码看上去十分混乱,是的代码更容易出错,并且难以理解。并且在此题忘记了使用题目中要求的类的使用。不管题目的难度如何,应时刻注意自己代码的整齐,这样在下次修改或者他人观看时也会更加直观易懂。并且不能忽略题目的要求。

对于各种数据的命名,我可能会直接用字母表示,但是这样会导致不易理解该字母所代表的数据。所以以后在数据命名上应尽量使用该数据的英文,这样也能使代码更加容易理解。

五、总结

本阶段三次题目集难度总体来说中等,有难有易。本阶段主要学习了较为基础的数据输入输出,判断语句,循环语句,数组,字符串,方法的调用以及类的使用及调用。以后我们应对字符串所包含的内容以及类的使用等进行深入学习与研究。

对于本课程的建议是希望老师以后能给我们多宽限一点时间,有时题目难度太大花费时间很长,同时又要完成实验完成其他课程作业,时间会稍有紧张。

题目集1-3的总结性blog

原文:https://www.cnblogs.com/yqcblogs/p/14612557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!