前三次PTA总结
(1)前言:这三次的pta作业,其涵盖的知识点很全面,说明出题老师非常认真,由简入繁,
逐层往上增加难度,既可以巩固基础,不至于让学生直接碰壁,增强学生信心,又可以很好的提
高学生水平,题量总体来说是合适的,较难的题目题量少,稍微简单一些的题量就更多。总而言
之,pta的作业对于我的帮助是非常大的,下面是我对前三次pta作业的一次总结。
(2)设计与分析:由于进入pta开始写作业的时间晚了两天,第一次pta的作业错过了,已经
跟老师解释过了,下面分析后两次的作业。题目集2的7-4是求下一天,其中题目给出了Main类中
必须含有的四个方法,如下图:

下面是我提交的源码:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input = new Scanner (System.in); int year,month,day; year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); /* if(year<1600||year>2100) { */ if(checkInputValidity( year,month, day)) { nextDate(year,month,day); } else { System.out.print("Wrong Format"); } } //判断year是否为闰年,返回boolean类型 public static boolean isLeapYear(int year) { boolean b; if(year%4==0&&year%100!=0||year%400==0) b=true; else b=false; return b; } //判断输入日期是否合法,返回布尔值 public static boolean checkInputValidity(int year,int month,int day) { boolean b =true; if(year<1820||year>2020||month<1||month>12||day<1) { b = false; } else if(((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31)||((month==4||month==6||month==9||month==11)&&day>30)||(month==2&&day>29)) { b = false; } else if(!isLeapYear(year)) { if(month==2&&day>28) { b = false; } } return b; } public static void nextDate(int year,int month,int day) { if(year==1819&&month==12&&day==31) { day=1; month=1; year++; System.out.print("Next date is:"+year+"-"+month+"-"+day); } else if(isLeapYear(year)) { if(month==12&&day==31) { day=1; month=1; year++; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)||((month==4||month==6||month==9||month==11)&&day==30)||(month==2&&day==29)) { day=1; month++; } else { day++; } System.out.print("Next date is:"+year+"-"+month+"-"+day); } else { if(month==12&&day==31) { day=1; month=1; year++; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)||((month==4||month==6||month==9||month==11)&&day==30)||(month==2&&day==28)) { day=1; month++; } else { day++; } System.out.print("Next date is:"+year+"-"+month+"-"+day); } } }
通过Main函数中的四个方法,实现了求下一天的功能,也符合题目的要求,首先判断输入日期是否
合法,返回布尔值,其次判断year是否为闰年,返回boolean类型,本体需要注意的是平年与润年在
2月份天数的区别,其中平年28天,润年29天,还有大小月的区别,大月31天,小月30天,以及每一
年的12月31日,需要年份加一,注意以上几点这道题就没什么问题了,总体而言难度不大,需要按照
题目给的要求来做,同时注意输出的格式即可。
运行截图如下:


题目集2的7-5是求前N天,由于n的取值可以为负数,所以还要求能够求得后N天,这道题得难度
比上一道更大,代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner (System.in); int year,month,day,n; year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); n=input.nextInt(); if(checkInputValidity( year,month,day)&&(n>=-10||n<=10)) { nextDate(year,month,day,n); } else { System.out.print("Wrong Format"); } } //判断year是否为闰年,返回boolean类型 public static boolean isLeapYear(int year) { boolean b; if(year%4==0&&year%100!=0||year%400==0) b=true; else b=false; return b; } //判断输入日期是否合法,返回布尔值 public static boolean checkInputValidity(int year,int month,int day) { boolean b =true; if(year==1819&&month==12&&day==31) { b = true; } else if(year<1820||year>2020||month<1||month>12) { b = false; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day>31)||((month==4||month==6||month==9||month==11)&&day>30)||(month==2&&day>29)) { b = false; } else if(!isLeapYear(year)) { if(month==2&&day>28) { b = false; } } return b; } public static void nextDate(int year,int month,int day,int n) { if(isLeapYear(year)) { if(n>0&&month==3&&day-n<1){ day=29+day-n; month--; } if(month==12&&day==31) { if(n<0){ day=-n; month=1; year++; } } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)||((month==4||month==6||month==9||month==11)&&day==30)||(month==2&&day==29)) { day=-n; month++; } else if((day-n>31)&&(month==1||month==3||month==5||month==8||month==10)){ day=day-n-31; month++; } else if((day-n>30)&&(month==4||month==6||month==9||month==11)) { day=day-n-30; month++; } else if((day-n>29)&&(month==2)) { day=day-n-29; month++; } else { day=day-n; } System.out.println(n+" "+"days ago is:"+year+"-"+month+"-"+day); } else { if(month==1&&day-n<1) { if(n>0) { day=31+day-n; month=12; year--; } } if(n<0&&!isLeapYear(year)) { if((day-n>28)&&(month==2)) { day=day-n-28; month++; } //System.out.println(n+" "+"days ago is:"+year+"-"+month+"-"+day); } else if((month==2||month==4||month==6||month==8||month==9||month==11)&&day==1) { day=31-n; month--; } else if((month==5||month==7||month==10||month==12)&&day==1) { day=30-n; month--; } else if((day-n<1)&&(month==2||month==4||month==6||month==8||month==9||month==11)){ day=31+day-n; month--; } else if((day-n<1)&&(month==5||month==7||month==10||month==12)){ day=30+day-n; month--; } else if(month==3&&day-n<1){ day=28+day-n; month--; } else { day=day-n; } System.out.print(n+" "+"days ago is:"+year+"-"+month+"-"+day); } } }
这道题我是在上一题的基础上进行修改的,同样是先判断输入是否合法,然后判断是否为润年,
最后进行求值,我对于这道题的思路是除了前面说过的平闰年2月份的问题以及大小月、每年的
最后一天以外,还有从大月到小月或者小月到大月的问题,总之,需要注意很多细节,同学跟
我说是否可以尝试求前一天然后循环n次,我觉得可行,就是还没有进行过尝试,有兴趣得话可
以试试。
下面是题目集3得7-2,题目为定义日期类,话不多说,代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int year,month,day; year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); if(checkInputValidity( year,month,day)) { nextDate(year,month,day); } else { System.out.print("Date Format is Wrong"); } } //判断year是否为闰年,返回boolean类型 public static boolean isLeapYear(int year) { boolean b; if(year%4==0&&year%100!=0||year%400==0) b=true; else b=false; return b; } //判断输入日期是否合法,返回布尔值 public static boolean checkInputValidity(int year,int month,int day) { boolean b =true; if(year<1900||year>2000||month<1||month>12||day<1||day>31) { b = false; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day>31)||((month==4||month==6||month==9||month==11)&&day>30)||(month==2&&day>29)) { b = false; } else if(!isLeapYear(year)) { if(month==2&&day>28) { b = false; } } return b; } public static void nextDate(int year,int month,int day) { if(isLeapYear(year)) { if(month==12&&day==31) { day=1; month=1; year++; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)||((month==4||month==6||month==9||month==11)&&day==30)||(month==2&&day==29)) { day=1; month++; } else { day++; } System.out.print("Next day is:"+year+"-"+month+"-"+day); } else { if(month==12&&day==31) { day=1; month=1; year++; } else if(((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)||((month==4||month==6||month==9||month==11)&&day==30)||(month==2&&day==28)) { day=1; month++; } else { day++; } System.out.print("Next day is:"+year+"-"+month+"-"+day); } } }
这道题应该没什么好说的,除了年份范围与第一题不同外其他都基本一样,所以思路也是相同的,
一句话,同上即可。
最后一个是7-3,一元多项式求导,这道题可以好好说说,首先做这道题我们要知道多项式要如
何求导,就是常数变为0,一次项变为1,其它多次项指数减1,同时还要乘前面的系数,这道题是
做pta作业以来最难的一道题了,主要是使用正则表达式来做,检验输入的多项式是否合法,做出
筛选,然后进行求导,根据题目要求,如下图:

代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input= new Scanner(System.in); String str = input.nextLine();// str = str.replaceAll(" ", ""); if(!str.matches("((\\+|\\-)*([1-9][\\d]*)*\\**(x|x\\^\\-*[1-9]+)*|(\\+|\\-)*[\\d])*")) System.out.println("Wrong Format"); else { String first=str.charAt(0)+""; if(str.charAt(0)!='x'&&!(str.charAt(0)>='0'&&str.charAt(0)<='9'))//第一位是+或- str=str.substring(1, str.length()); char fuhao[]=new char[100]; int f_index=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='+'||(str.charAt(i)=='-'&&str.charAt(i-1)!='^')) {//将符号存入数组中 fuhao[f_index]=str.charAt(i); f_index++; } } String expression[] = new String[100]; int e_len=0; for(int i=1,e_index=0,j=0;i<str.length();i++) { if(str.charAt(i)=='+'||(str.charAt(i)=='-'&&str.charAt(i-1)!='^')) { if(e_index==0) expression[e_index++]=str.substring(0, i); for(j=i+1;j<str.length();j++) if(str.charAt(j)=='+'||str.charAt(j)=='-') { expression[e_index]=str.substring(i+1, j); e_index++; break; } if(j==str.length()) expression[e_index]=str.substring(i+1, j); } e_len=e_index+1; } /*for(String s:expression) System.out.println(s);*/ for(int i=0;i<e_len;i++) { if(expression[i].matches("[\\d]{1,}")) expression[i]="";//System.out.println("纯数字"); else if(expression[i].matches("[\\d]{1,}[*]x")) {//System.out.println("数字+x"); expression[i] = expression[i].replace("x", ""); expression[i] = expression[i].replace("*", ""); } else if(expression[i].matches("[\\d]{1,}[*]x\\^([\\d]{1,}|[-][\\d]{1,})"))//数字+x的次方 { expression[i] = expression[i].replace("*", ""); expression[i] = expression[i].replace("^", ""); for(int j=0;j<expression[i].length();j++) { if(expression[i].charAt(j)=='x') { int old_xishu=Integer.parseInt(expression[i].substring(0, j)); int cifang=Integer.parseInt(expression[i].substring(j+1, expression[i].length())); int new_xishu=old_xishu*cifang; expression[i] = String.valueOf(new_xishu)+"*x^"+String.valueOf(cifang-1); break; } } } else if(expression[i].matches("x")) expression[i]="1";//System.out.println("x"); else if(expression[i].matches("x\\^([\\d]{1,}|[-][\\d])")) {//System.out.println("x的次方"); expression[i] = expression[i].replace("x", ""); expression[i] = expression[i].replace("^", ""); int xishu=Integer.parseInt(expression[i]); expression[i]=String.valueOf(xishu)+"x^"+String.valueOf(xishu-1); } } String result=""; for(int j=0;j<f_index;j++) { if(expression[j]!="") result+=expression[j]+fuhao[j]; if(j+1==f_index) { if(expression[j+1]!="") result+=expression[j+1]; else { if(result.charAt(result.length()-1)=='-'||result.charAt(result.length()-1)=='+') result=result.substring(0, result.length()-1); } } } if(first.equals("-")&&result.charAt(0)=='-') { result=result.substring(1,result.length()); } else if(first.equals("-")&&result.charAt(0)!='-') { result=first+result; } if(result=="") result="0"; System.out.println(result); } } }
这道题怎么说呢,其实我也不太会,得分并不高,因为输出格式经常错误,可能以我现在的水平
还不足以拿到高分,说明自己还有很大的进步空间,需要好好学习,再接再厉!对于题目的分析
就到这里了,下面是我的一些总结。
pta是个非常好的平台,对于许多同学来说,能独立地把pta上面的每道题写好,做对,说明你
的学习效果还可以了,如果想要进一步提高,那就需要别的方式了,有很多,比如牛客什么的,非
常多的刷题软件,这些都是非常不错的,通过这三次作业,让我更加了解了自己的水平,还差很多
很多,同时也学习到了很多java的基础知识,它跟C语言其实是有很多相似之处的,并且没有C中比
较复杂的链表,也不是没有,只是换了一种方式存在。这是门非常好的语言,我们需要好好学,提
高自己的水平。老师在上课中经常与同学互动,要同学回答问题,同时上课的时候经常讲例子,非
常的直观,也让我们听的更加明白。
浙公网安备 33010602011771号