PTA题目集1-3总结归纳

前言:

题目知识点:定义Java类、if else的嵌套、私有类型的封装...

题量难度适中  

 

目录:(java编写)

    1.PTA题目集1的7-8 (判断三角形类型)

    2.PTA题目集2的7-4 (求下一天)

    3.PTA题目集2的7-5 (求前N天)

    4.PTA题目集3的7-2 (定义日期类)

    5.PTA题目集3的7-3 (一元多项式求导(类设计))

 


1.7-8 判断三角形类型

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

输入格式:在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”;

(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;

(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;

(4)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;

(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;

(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;

(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

 

输入样例1:

 

在这里给出一组输入。例如:

 

50 50 50.0

 

输出样例1:

 

在这里给出相应的输出。例如:

 

Equilateral triangle

 

输入样例2:

 

在这里给出一组输入。例如:

 

60.2 60.2 80.56

 

输出样例2:

 

在这里给出相应的输出。例如:

 

Isosceles triangle

 

输入样例3:

 

在这里给出一组输入。例如:

 

0.5 20.5 80

 

输出样例3:

 

在这里给出相应的输出。例如:

 

Wrong Format


分析:
本题要求用户输入三个数,其分别对应三角形的三条边,根据三条边的长度来判断这三条边能不能构成一个三角形。当判断为可以构成三角形时就可以进行下一步判断:三角形是什么形状。判断完毕后进行输出。
使用if else嵌套来实现对三角形的判断并按题目要求进行相应输出,相应思路如下:
首先用户输入三个数,需要判断数据的合理性,不合法则输出Wrong Format,合法继续判断;
数据合法后,判断是否能构成三角形,不能构成三角形输出Not a triangle,能构成三角形则继续判断;
将一般三角形General triangle,等腰三角形Isosceles triangle,直角三角形Right-angled triangle并列判断;
如果为等腰三角形Isosceles triangle则需要进一步判断,等边三角形Equilateral triangle为特殊的等腰三角形,等腰直角三角形Isosceles right-angled triangle又比等腰三角形多了一个直角条件。

代码展示:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        double d = a.nextDouble();
        double e = a.nextDouble();
        double f = a.nextDouble();
        if(d<=0||e<=0||f<=0) {
            System.out.println("Wrong Format");
        }
        else if((d+e<f)||(d+f<e)||(e+f<d)){
            System.out.println("Not a triangle");
        }
        else if(d==e&&e==f){
            System.out.println("Equilateral triangle");
        }
        else if((d==e&&d*d+e*e==f*f)||(f==e&&f*f+e*e==d*d)||(d==f&&d*d+f*f==e*e)){
            System.out.println("Isosceles right-angled triangle");
        }
        else if((d==e&&d*d+e*e!=f*f)||(f==e&&f*f+e*e!=d*d)||(d==f&&d*d+f*f!=e*e)){
            System.out.println("Isosceles triangle");
        }
        else if((d!=e&&d*d+e*e==f*f)||(f!=e&&f*f+e*e==d*d)||(d!=f&&d*d+f*f==e*e)){
            System.out.println("Right-angled triangle");
        }
        else
            System.out.println("General triangle");
    }
}

2.7-4 求下一天

题目要求:输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法

public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型

public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值

public static void nextDate(int year,int month,int day) ;//求输入日期的下一天

输入格式:在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  1. 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
  2. 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日

输入样例1:

在这里给出一组输入。例如:

2020 3 10

输出样例1:

在这里给出相应的输出。例如:

Next date is:2020-3-11

输入样例2:

在这里给出一组输入。例如:

2025 2 10

输出样例2:

在这里给出相应的输出。例如:

Wrong Format


分析:
这是一道时间推算题,刚开始看到这一题的时候,我想到又有闰年又有12个月这些每年月都有许多不同求下一天一定非常麻烦,但当我把它一点一点拆分出来的时候,就感觉比较简单啦。
首先,对输入的年份进行判断,当其为闰年时,计算2月时28号的后一天就是2月29了,其他的和平年一样计算;对于平年而言,可以将月份分为三类:28天、30天、31天,如果是2月28号之前,
把天数加一就行,如果是28号,将天数改为1,月份加一;其余的以此类推即可。与此同时,要注意:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31]
有一个点就是当输入有误时输出
Wrong Format,还有一个点就是当输入的年份为平年时却有2月29时,也要输出Wrong Format,这个是我输入了好多遍以后才发现的,所以让我印象深刻。

代码展示:
import java.util.*;
public class Main{
   static class Date{
        int year;
        int month;
        int day;
        int[] mon= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        void input(){
            Scanner sc=new Scanner(System.in);
            year=sc.nextInt();
            month=sc.nextInt();
            day=sc.nextInt();
        }
        int isLeapYear(){
            int flag=0;
            if((year%400==0&&year%100!=0)||year%4==0)
                flag=1;
            return flag;
    }
         String checkInputValidity(){
            if((year<1900||year>2000)||(month<1||month>12)||(day<1||day>31))
            return "Date Format is Wrong";
            return null;
        }
           void nextDate(int flag){
            int a=year; int b=month; int c=day;
            if(month!=2&&month!=12){
                if(day<mon[month]){
                    c++;
                }
                    else{
                    b++;c=1;
                }
            }
            if(month==2){
                if(flag==1){
                    if(day<29)
                        c++;
                    else {
                        b = 3;
                        c = 1;
                    }
                }
                else
                    if(day<28)
                        c++;
                    else{
                        b = 3;
                        c = 1;
                    }
            }
            if(month==12){
                if(day<31)
                    c++;
                else{
                    a++;
                    b=1;c=1;
                }
            }
        System.out.println("Next day is:"+a+"-"+b+"-"+c);
        }
    }
    public static   void main(String[] args){
        Date a=new Date();
        a.input();
        a.isLeapYear();
        String b=a.checkInputValidity();
        if(b!=null)
        {
            System.out.println("Date Format is Wrong");
            System.exit(0);
        }
        int c=a.isLeapYear();
        a.nextDate(c);
    }
}

 

3.7-5求前N天

题目要求:

  输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
  其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
  注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:

  在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:

1.当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
2.当输入数据合法时,输出“n days ago is:年-月-日”

输入样例1:

在这里给出一组输入。例如:

2018  6 19 8 

输出样例1:

在这里给出相应的输出。例如:

8 days ago is:2018-6-11

输入样例2:

在这里给出一组输入。例如:

2018  6 19 -8 

输出样例2:

在这里给出相应的输出。例如:

-8 days ago is:2018-6-27


分析:
这一题和上一题有着很多的共同之处,数据合理的判断,判断闰年等,基本结构和上一题区别不大。两题的区别在于这时候需要用户输入n来计算出前几天或者后几天的日期。并且在跨年附近也有一些区别,找准这两题的区别
再下手写,难度不大,这里我就不多说了,附上代码:

代码展示:
import java.util.Scanner;
public class Main {
    public static void main(String[] arg) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int n = input.nextInt();
        boolean d = checkInputValidity(a, b, c, n);
        String e = String.valueOf(d);
        int h = 0, i = 0;
        if (e == "false") {
            System.out.println("Wrong Format");
        } else {
            boolean f = isLeapYear(a);
            String g = String.valueOf(f);
            if (n <= 0) {
                i = 29;
                if (g == "false" && b == 2) {
                    i = 28;
                }
                if (b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10||b==12) {
                    i = 31;
                }
                if (b == 4 || b == 6 || b == 9 || b == 11) {
                    i = 30;
                }
                c = c - n;
                if (c > i) {
                    b = b + 1;
                    c = c - i;
                }
                if (b > 12) {
                    a = a + 1;
                    b = 1;
                }
            } 
            if(n>0){
                    i = 29;
                    if (g == "false" && b == 3) {
                    i = 28;
                };
                if (b == 2 || b == 4 || b == 6 || b == 8 || b == 9 || b == 11 || b == 1) {
                    i = 31;
                }
                if (b == 5 || b == 7 || b == 10 || b == 12) {
                    i = 30;
                }
                c=c-n;
                if (c <= 0) {
                    b = b - 1;
                    c = c + i;
                }
                if (b <= 0) {
                    a = a - 1;
                    b = 12;
                }
            }
            System.out.println(n+" days ago is:" + a + "-" + b + "-" + c);
        }
    }

    public static boolean checkInputValidity(int a,int b,int c,int n) {
        boolean checkInputValidity = true;
        int year=a,month=b,day=c;
        if(n<-10||n>10) {
            checkInputValidity = false;
        }
        if(year<1820||year>2020||month<1||month>12||day<1||day>31) {
            checkInputValidity = false;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
            if(day<1||day>31) {
                checkInputValidity = false;
            }
        }
        else if(month==2) {
            if(year%4==0&&year%100!=0) {
                if(day<1||day>29) {
                    checkInputValidity = false;
                }
            }else {
                if(day<1||day>28) {
                    checkInputValidity = false;
                }
            }
        }
        else if(month==4||month==6||month==9||month==11&&(day<1||day>30)) {
            if(day<1||day>31) {
                checkInputValidity = false;
            }
        }
        return checkInputValidity;
    }


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

4.7-2定义日期类

题目要求:

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

  注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

  Date类结构如下图所示:

 

 

 

 

 输入格式:

  在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:
当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

输入样例1:

在这里给出一组输入。例如:

1912 12 25

输出样例1:

在这里给出相应的输出。例如:

Next day is:1912-12-26

输入样例2:

在这里给出一组输入。例如:

2001 2 30

输出样例2:

在这里给出相应的输出。例如:

Date Format is Wrong

分析:
本题同第二题可以说非常相似,但是这里强调了对年、月、日使用私有属性进行封装,其他地方和第二题没有什么大的区别,此处我就不进行过多解释了

代码展示:
import java.util.*;
public class Main {
    public static void main(String[] args){
        Date a=new Date();
        Scanner input=new Scanner(System.in);
        //System.out.println("请输入日期:");
        int year= input.nextInt();
        a.setYear(year);
        int month= input.nextInt();
        a.setMonth(month);
        int day= input.nextInt();
        a.setDay(day);


        if (a.checkInputValidity(year,month,day)){
             if(!a.isLeapYear(year)&&(a.getMonth()==2&& a.getDay()==29))
                System.out.println("Date Format is Wrong");
            else {
                a.getNextDate();
                System.out.println("Next day is:" + a.getYear() + "-" + a.getMonth() + "-" + a.getDay());
            }
        }
        else
            System.out.println("Date Format is Wrong");

    }
}

 class Date {
    private int year,month,day;
    int[] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};


    Date(){
        int year,month,day;
        int[] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    }

    Date(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;

    }

    void setYear(int year){
        this.year=year;
    }


    void setMonth(int month){
        this.month=month;
    }


    void setDay(int day){
        this.day=day;
    }

    //判断闰年
    boolean isLeapYear(int year){
        if(year%400==0||(year%4==0&&year%100!=0))
            return true;
        else
            return false;
    }
    //判断输入数据是否有效
    boolean checkInputValidity(int year,int month,int day){
        if((year>=1900&&year<=2000)&&(month>=1&&month<=12)&&(day>=1&&day<=31))
            return true;
        else
            return false;
    }

    void getNextDate(){
            if (month==1||month==3||month==5||month==7||month==8||month==10) {
                if (++day <31)
                    this.day = day++;
                else {
                    this.month = ++month;
                    this.day = 1;
                }
            }
            else
                if (month==4||month==6||month==9||month==11||month==8) {
                    if (++day < 30)
                        this.day = day++;
                    else {
                        this.month = ++month;
                        this.day = 1;
                    }
                }
                else
                    if(month==12){
                            if(++day<=31)
                                this.day=++day;
                            else{
                                this.year=++year;
                                this.month=1;
                                this.day=1;
                            }
                        }
                  if(isLeapYear(year)){
                      if(month==2){
                          if(++day<=29)
                              this.day=++day;
                          else{
                              this.month=++month;
                              this.day=1;
                          }
                      }
                  }
                  else
                      if(month==2){
                          if (++day<=28){
                              this.day=++day;
                          }
                          else
                          {
                              this.month=++month;
                              this.day=1;
                          }
                      }
    }
    int getYear(){
        return year;
    }
    int getMonth(){
        return month;
    }
    int getDay(){
        return day;
    }

}

6.7-3 一元多项式求导(类设计)

题目要求:

  编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 

   https://images.ptausercontent.com/a6146210-4a98-4a95-9e7b-a5df3ee89ee4.pdf

输入格式:

  在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:

1.如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
2.如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
  (1)  当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
  (2)  当输出结果第一项系数符号为“+”时,不输出“+”;
  (3)  当指数符号为“+”时,不输出“+”;
  (4)  当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
输出格式见输入输出示例。

输入样例1:

在这里给出一组输入。例如:

-2*     x^-2+  5*x^12-4*x+       12

输出样例1:

在这里给出相应的输出。例如:

4*x^-3+60*x^11-4

输入样例2:

在这里给出一组输入。例如:

2*x^6-0*x^7+5

输出样例2:

在这里给出相应的输出。例如:

Wrong Format
  分析:
本题对于刚入门的我来说确实存在一些难度,能够看到这里,真的非常感谢你的支持,我会继续努力,争取在不久的将来回到这里把这道现在觉得有难度的题画上一个圆满的句号。

 

 

总结:

  首先非常荣幸可以加入博客园这个大家庭,希望在这里可以和大家一起学习、共同进步,提升自己的水平。这里我将对自己做的一些题目进行一些分析总结,如果有的地方写的不好大家可以指出,我会根据大家的意见进行相应改正。初学Java的我发现每一道题都非常有意思,也很值得我们去思考。让我们一起努力吧!

最后我想对你说:字多了点,感谢您能耐心读完!

posted @ 2021-04-04 21:32  苏客  阅读(125)  评论(1)    收藏  举报