高抬贵手

导航

pta前三次题目集的总结

开学一个月,java课程总共布置了三次pta作业,可以感受到,每次难度都在加大,每次都可以遇到不一样的bug,这就是平时理论学习的漏洞,所以决定对这前三次作业选用几个做一个总结,便于吸收经验,集思广益,互相进步。

题目集一7-8

题目:输入三角形三条边,判断该三角形为什么类型的三角形

我的代码:

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();
        if(a>=1 && a<=200 && b>=1 && b<=200 && c>=1 && c<=200){
            if(a+b>c && a+c>b && b+c>a){
                if(a==b && b==c){
                    System.out.println("Equilateral triangle");
                }
                else if(a==b || b==c || a==c){
                        if((a*a+b*b-c*c<1)||(a*a+c*c-b*b<1)||(b*b+c*c-a*a<1)){
                                System.out.println("Isosceles right-angled triangle");
                        }
                        else    System.out.println("Isosceles triangle");
                    }
                    else if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){
                                System.out.println("Right-angled triangle");
                        }
                        else    System.out.println("General triangle");
            }
            else    System.out.println("Not a triangle");
        }
        else    System.out.println("Wrong Format");
    }
}

分析:

 

由于比较简单,没有多考虑,代码不够美观,用的也是比较容易想到的方法,好在代码量不是很大。至于如何改进,我觉得应该做到面向对象,多设置方法,才能做到代码思路清晰。

题目集二 7-4

题目:输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。

我的代码:

import java.util.Scanner;

public class Main {
    public static boolean isLeapYear(int year){//判断year是否为闰年,返回boolean类型
        boolean isLeapYear;
        isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
        return isLeapYear;
    }
    public static boolean checkInputValidity(int year,int month,int day){//判断输入日期是否合法,返回布尔值
        int []A={31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            A[1]=29;
        boolean checkInputValidity;
        checkInputValidity=(year>=1820 && year<=2020 && month<=12 && month>=1 && day<=A[month-1] && day>=1);
              return checkInputValidity;
    }
    public static void nextDate(int year,int month,int day){//求输入日期的下一天
        int []B={31,28,31,30,31,30,31,31,30,31,30,31,0};
        if(isLeapYear(year))
            B[1]=29;
        if(day+1>B[month-1]){
            if(month==12){
                month=1;
                year=year+1;
                day=0;
            }
            else {
                month=month+1;
                day=0;
            }
        }
        System.out.print("Next date is:");
        System.out.println(year+"-"+month+"-"+(day+1));
    }
    public static void main(String[] args){//主方法
        Scanner input=new Scanner(System.in);
        int year=input.nextInt();
        int month=input.nextInt();
        int day=input.nextInt();
        if(!checkInputValidity(year,month,day))
            System.out.println("Wrong Format");
        else nextDate(year,month,day);
    }
}

分析:

 

这道题我自我觉得还是比较满意的,应题目要求设置了很多方法来规划每个点,不过在算法方面,我采取的是最简单想到的,也相对有点繁琐,改进的话可以在这方面入手。

题目集二 7-5

题目:输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。

我的代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){//主方法
        Scanner input=new Scanner(System.in);
        int year=input.nextInt();
        int month=input.nextInt();
        int day=input.nextInt();
        int []B={31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            B[1]=29;
        boolean checkInputValidity;
        int n=input.nextInt();
        checkInputValidity=(year>=1820 && year<=2020 && month<=12 && month>=1 && day<=B[month-1] && day>=1 && n>=-10 && n<=10);
        if(!checkInputValidity)
            System.out.print("Wrong Format");
        else{
            if(n<0){
                if(month==12 && day-n>B[month-1]){
                    year=year+1;
                    month=1;
                    day=day-n-31;
                }
                else if(day-n>B[month-1]){
                    day=day-n-B[month-1];
                    month=month+1;
                    }
                    else day=day-n;
            }else if(n>0){
                    if(month==1 && day-n<=0){
                        year=year-1;
                        month=12;
                        day=31-n+day;
                    }
                    else if(day-n<=0){
                        day=B[month-2]-n+day;
                        month=month-1;
                    }
                    else day=day-n;
            }
            System.out.print(n+" days ago is:"+year+"-"+month+"-"+day);
        }

    }
    public static boolean isLeapYear(int year){//判断year是否为闰年,返回boolean类型
        boolean isLeapYear;
        isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
        return isLeapYear;
    }
}

分析:

 

这道题和上面那道有点相似,大部分相似,由于没有规定使用方法,所以我当时一笔带过,由于没有面向对象的概念,没有这种习惯,改进可以从这里入手。

题目集三 7-2

题目:

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。

我的代码:

import java.util.Scanner;

public class Main {
    public static boolean Date(int year){//判断year是否为闰年,返回boolean类型
        boolean isLeapYear;
        isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
        return isLeapYear;
    }
    public static boolean checkInputValidity(int year,int month,int day){//判断输入日期是否合法,返回布尔值
        int []A={31,28,31,30,31,30,31,31,30,31,30,31};
        if(Date(year))
            A[1]=29;
        boolean checkInputValidity;
        checkInputValidity=(year>=1900 && year<=2000 && month<=12 && month>=1 && day<=A[month-1] && day>=1);
        return checkInputValidity;
    }
    public static void nextDate(int year,int month,int day){//求输入日期的下一天
        int []B={31,28,31,30,31,30,31,31,30,31,30,31,0};
        if(Date(year))
            B[1]=29;
        if(day+1>B[month-1]){
            if(month==12){
                month=1;
                year=year+1;
            }
            else {
                month=month+1;
            }
            day=0;
        }
        System.out.print("Next day is:");
        System.out.println(year+"-"+month+"-"+(day+1));
    }
    public static void main(String[] args){//主方法
        Scanner input=new Scanner(System.in);
        int year=input.nextInt();
        int month=input.nextInt();
        int day=input.nextInt();
        if(!checkInputValidity(year,month,day))
            System.out.println("Date Format is Wrong");
        else nextDate(year,month,day);
    }
}

分析:

 

这道题和上面的上面那道题极其相似,可以说是一模一样,我就不多解释。

题目集三 7-3

题目:使用类与对象编写程序对简单多项式的导函数进行求解。

我的代码:

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
/*
 *       此类用于对多项式表达式求导
 *         by@kaix2021/03/23
 */
//Derivation 求导
public class Main  {

    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        String temp=in.nextLine();

        DvForString dv=new DvForString();
        dv.setPolynthic(temp);//预处理输入的字符串
        dv.print(); //根据合法性作出不同输出
//        System.out.println();
//        dv.printEveryElement();
    }
}
class DvForString{
    private static LinkedList<String> item;//存储各项,此题并不需要
    private static String sign,number,regex,regex1,regex2,regex3,concat,concatend,specialnum,end;//各项正则
    //静态区,字符串
    static {
        //静态区只在类被加载时被执行一次
        item=new LinkedList<>();
        sign="(?:[+|-])";
        number="(?:([1-9](\\s*\\d*\\s*)*))";
        //全是常数项时的特判
        specialnum="((?:(\\s*([+|-]?[1-9]+\\s*\\d*)\\s*))*)";
        //带x项,允许系数中间内有空格
        //有不为0的系数和指数
        regex="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))";
        //x只有指数
        regex1="(?:(x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))";
        //x只有系数
        regex2="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x))";
        //x前面系数,指数都没有
        regex3="(?:x)";
        concat="("+regex+"|"+regex1+"|"+regex2+"|"+regex3+")";
        //数字和带x项或在一起构成多项式中的一项
        concatend="("+concat+"|"+number+")";
        //多项式表达式 ,首项可以无+,-  后续项必须有
        end="(?:("+"\\s*"+sign+"?\\s*"+concatend+"(?:\\s*"+sign+"\\s*"+concatend+"\\s*)*"+"))";
    }
    //Polynthic 多项式
    private String Polynthic=null;

    public LinkedList<String> getItem() {
        return item;
    }
    //无参构造
    public DvForString(){}
    //有参构造,可以直接通过字符串数组名赋值另一个相同大小和类型的数组
    public DvForString(String polynthic) {
        this.Polynthic = polynthic;
    }
    //对私有变量获取和,赋值的方法
    public String getPolynthic() {
        return Polynthic;
    }
    public void setPolynthic(String polynthic) {
        this.Polynthic = polynthic;
    }
    //合法性检验
    private boolean isLegal() {
        boolean flag=true;

        String s=Polynthic;
        //全常数检验
        if(s.matches(specialnum))
            return true;
        //复杂多项式检验
        if(!s.matches(end)) flag=false;
        return flag;
    }
    //打印合法输入中每一项
    public  void printEveryElement() {
        System.out.println();
        if(isLegal()) { for(String e: item) System.out.println(e);}
        else System.out.println("Wrong Format");
    }
    //打印
    public void print() {
        if(isLegal()) printLegal();
        else  printNotLegal();//不合法结果打印
    }
    //打印不合法输出
    private void printNotLegal() {
        System.out.println("Wrong Format");
    }
    //打印合法输出
    private  void printLegal() {
        //拷贝一份给字符串s
        String s=Polynthic;
        s=s.replace(" ","");

        char[] t=s.toCharArray();
        for(int i=0;i<s.length()-1;i++)
            if(t[i]=='^'&&t[i+1]=='-') t[i+1]='#';

        //直接通过tostring转换字符数组会出错,所以一个个字符拼接
        String s1="";
        for(int i=0;i<t.length;i++)
            s1=s1+t[i];
        //再来整体替换-
        s1=s1.replace("^+","^");
        s1=s1.replace("-","+-");

        //如果+出现在第一个位置会报异常,要用\\+
        String [] id=s1.split("\\+");

        //加号输出标记
        int flag=0;
        //无项时输出标记,标记若最终为0,最后输出0
        int lazy=0;
        //所有常数直接打印不需要特意放+,带x项,对于大于0项,前面有其他项要打印出来

        for(String e :id) {
            //切开后对每个元素进行分析处理,#要替换成-
            e=e.replace("#","-");
            if(e!=null) item.addLast(e);

            int start=e.indexOf("x");
            int mid=e.indexOf("*");
            int end=e.indexOf("^");
            //x,^都有
            if(start!=-1&&end!=-1) {
                //系数不为1,存在*
                if(mid!=-1) {
                    BigInteger pro=new BigInteger(e.substring(0,mid));
                    BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                    BigInteger xishu=pro.multiply(pos);//乘法
                    BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法

                    if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                    else {
                        if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                        else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                        else {
                            if(    xishu.compareTo(new BigInteger(0+""))>0)
                                if(flag==1) System.out.print("+");
                            System.out.print(xishu+"*x^"+zhishu);
                        }
                    }
                    lazy=1;
                    if(flag==0) flag=1;
                }
                //没有*,系数为负
                else {
                    if(e.charAt(0)=='-') {
                        BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                        BigInteger xishu=pos.multiply(new BigInteger(-1+""));//乘法
                        BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法
                        if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                        else {
                            if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                            else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                            else {
                                if(    xishu.compareTo(new BigInteger(0+""))>0)
                                    if(flag==1) System.out.print("+");
                                System.out.print(xishu+"*x^"+zhishu);
                            }
                        }
                    }
                    //没*,系数不为负
                    else {
                        BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                        BigInteger xishu=pos.multiply(new BigInteger(1+""));//乘法
                        BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法
                        if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                        else {
                            if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                            else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                            else {
                                if(    xishu.compareTo(new BigInteger(0+""))>0)
                                    if(flag==1) System.out.print("+");
                                System.out.print(xishu+"*x^"+zhishu);
                            }
                        }
                    }
                    lazy=1;
                    if(flag==0) flag=1;
                }
            }//^,x都存在的分界线
            //有x,没有指数
            else if(start!=-1) {
                //有*
                if(mid!=-1) {
                    BigInteger num=new BigInteger(e.substring(0, mid));
//                    if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+");
                    System.out.print(num);
                }
                //没有*
                else {
                    BigInteger num=null;
                    if(e.charAt(0)=='x') num=new BigInteger("1");
                    else if(e.charAt(0)=='-') num=new BigInteger("-1");
                    else
                        num=new BigInteger(e.substring(0, start));
//                    if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+");
                    System.out.print(num);
                }
                lazy=1;
                if(flag==0) flag=1;
            }
            //常数
            else
                System.out.print("");
        }//大for结尾
        if(lazy==0) System.out.println(0);
    }
}

分析:

 

这道题不愧是压轴题,难度系数很大,由于没有达到全部要求,所以这里给大家看看,集思广益,一起解决这道题。

总结:这三次作业难度系数加大的同时,老师也在要求我们做到面向对象的思想来解决问题,我也深刻意识到,面对复杂的代码,就越要做到条理清晰,面向对象就是很好的方法,这也是我们学这门课程的原因。

posted on 2021-10-15 20:37  高抬贵手  阅读(38)  评论(0编辑  收藏  举报