PTA作业总结

(1)前言

第四次题目集第一题就是菜单问题,看到这么长的文字当时属实是蚌住了,所以对我而言有点难,暂时没做,第一题菜单主要考查的是类与类之间的联系,他们之间方法的调用,题目没看懂,完全动不了笔,第二题用到的是  java.util.Arrays.sort(a),进行排序,可以降低时间复杂度,其实也可以用HashMap来进行判断是否有相同的数据;第三题用到的是 LinkedHashSet<String> list = new LinkedHashSet<String>(),这个是可以直接储存不重复的数据,相同的数据就不会在进行储存,比较方便使用;第四题用到了  LinkedHashSet<String> link = new LinkedHashSet<String>(),以及compareToIgnoreCase()方法来比较他们的大小顺序;第五题是关于类的方法的书写,有参无参构造方法,get,set方法的使用;第六题考察的是保留六位小数以及整除时要除以整数不能为小数;第七题考查的是LocalDate类中isAfter()、isBefore(),ChronoUnit类中DAYS、WEEKS、MONTHS,Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法,需要对这些类进行查找资料;第五次题目集一三四考查的是正则表达式的使用,第二题用到了String[] s = s1.split("");:Arrays.sort(s),将字符串进行排序,第五第六题考察的是日期类的聚合,第五题需要层层调用,第六题是直接调用;第六次题目集是菜单类的迭代,第四次迭代,第三次没有做出来,第四题做的就很难受,没有做出来,难受死了,不能很好的缕清他们之间的关系;第五次题目集是菜单题的第四版迭代,之前第三版没做出来,这个用到的也是类与类之间的关系以及他们的方法的使用,所以这个做的时间很久,最后也是以失败告终,做不出来,感觉好难啊,难的要死,但是别人能做出来,而且还有满分的,这样差距一下就出来了,这边建议老师给个源码啥的,不然不会的就是永远不会,得不到提升啊,因为别人不会给你看代码,他们怕你抄袭,所以真的很难受啊,热爱不在啊;

 

(2)设计与分析

 

第五题是日期的迭代,用到的是聚合一,和之前用到的,这次是一次一次的调用方法;

 

这里给出复杂度图:

 

 

这里给出他的类图:

 

 

 

 

 这里给出相应的代码:

 

 

 

 

 

 

 

import java.util.Scanner;
//主函数
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

 

int choice = input.nextInt();

 

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

 

DateUtil date = new DateUtil(day,month,year);

 

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

 

m = input.nextInt();

 

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

 

// System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

 

DateUtil date = new DateUtil(day,month,year);

 

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

 

n = input.nextInt();

 

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

 

// System.out.print(
// date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

 

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

 

DateUtil fromDate = new DateUtil(day,month,year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

 

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println( fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
//Year类
class Year{
private int value;
Year(){

}
Year(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
boolean isLeapYear() {
if(value%4==0&&value%100!=0||value%400==0)
return true;
else
return false;
}
boolean validate() {
if(value>=1900&&value<=2050)
return true;
else
return false;
}
void yearIncrement() {
value++;
}
void yearReduction() {
value--;
}
}
//Month类
class Month{
private int value;
private Year year;
Month(){

}
Month(int yearValue,int monthValue){
this.value = monthValue;
this.year = new Year(yearValue);
}
int getValue() {
return this.value ;
}
void setValue(int value) {
this.value = value;
}
Year getYear() {
return year;
}
void setYear(Year year) {
this.year = year;
}
void resetMin(){
this.value = 1;
}
void resetMax() {
this.value = 12;
}
boolean validate() {
if(value>=1&&value<=12)
return true;
else
return false;
}
void monthIncrement() {
if(value>=1&&value<12)
value++;
else {
value = 1;
year.yearIncrement();
}
}
void monthReduction() {
if(value>1&&value<=12)
value--;
else {
value = 12;
year.yearReduction();
}
}
}
//Day类
class Day{
private int value;
private Month month;
private int[] monmaxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
Day(){

}
Day(int yearValue,int monthValue,int dayValue){
this.value = dayValue;
month = new Month(yearValue,monthValue);
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
Month getMonth() {
return month;
}
void setMonth(Month value) {
this.month = value;
}
void resetMin() {
value = 1;
}
void resetMax() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
value = monmaxnum[month.getValue()-1];
}
boolean validate() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value>=1&&value<=monmaxnum[month.getValue()-1]) {
return true;
}
else
return false;

}
void dayIncrement() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value==monmaxnum[month.getValue()-1]) {
value = 1;
month.monthIncrement();
}
else
value++;

}
void dayReduction() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value==1) {
month.monthReduction();
value=monmaxnum[month.getValue()-1];
}
else
value--;
}
}
//DateUtil类
class DateUtil{
Day day;
DateUtil(){
}
DateUtil(int d,int m,int y){
this.day = new Day(y, m,d);
}
Day getDay() {
return day;
}
void setDay(Day d){
this.day =d;
}
boolean checkInputValidity() {
if(day.getMonth().getYear().validate()==true&&day.getMonth().validate()==true&&day.validate()==true) {
return true;
}
else
return false;
}
boolean compareDates(DateUtil date) {
if(day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue())
return false;
else if(day.getMonth().getYear().getValue()<date.day.getMonth().getYear().getValue())
return true;
else{
if(day.getMonth().getValue()>date.day.getMonth().getValue())
return false;
else if(day.getMonth().getValue()>date.day.getMonth().getValue())
return true;
else{
if(day.getValue()>date.day.getValue())
return false;
else
return true;
}
}
}
boolean equalTwoDates(DateUtil date) {
if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&day.getValue()==date.day.getValue())
return true;
else
return false;
}
String showDate() {
String s1 = day.getMonth().getYear().getValue()+"-"+day.getMonth().getValue()+"-"+day.getValue();
return s1;
}
DateUtil getNextNDays(int n) {
for(int i = 0;i<n;i++) {
day.dayIncrement();
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
DateUtil getPreviousNDays(int n) {
for(int i = 0;i<n;i++) {
day.dayReduction();
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
int getDaysofDates(DateUtil date) {
int c = 0;
if(equalTwoDates(date)==true) {
return c;
}
else {
if(compareDates(date)==true) {
while(equalTwoDates(date)==false) {
day.dayIncrement();
c++;
}
}
else if(compareDates(date)==false) {
while(equalTwoDates(date)==false) {
day.dayReduction();
c++;
}

}
return c;
}

}
}

第六题用到的是聚合二;这次是在主方法中用year,month,day类中的方法;

这里给出复杂度图:

这里给出相应的类图:

 

 这里给出相应的代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year,month,day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year,month,day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(year,month,day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println( "The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"+fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class Year{
private int value;
Year(){

}
Year(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
boolean isLeapYear() {//判断是否为闰年
if(value%4==0&&value%100!=0||value%400==0)
return true;
else
return false;
}
boolean validate() {//判断是否合法
if(value>=1820&&value<=2020)
return true;
else
return false;
}
void yearIncrement() {
value++;
}
void yearReduction() {
value--;
}
}

class Month{
private int value;
Month(){

}
Month(int value){
this.value = value;
}
int getValue() {
return this.value ;
}
void setValue(int value) {
this.value = value;
}

void resetMin(){
this.value = 1;
}
void resetMax() {
this.value = 12;
}
boolean validate() {//判断是否合法
if(value>=1&&value<=12)
return true;
else
return false;
}
void monthIncrement() {
value++;
}
void monthReduction() {
value--;
}
}

class Day{
private int value;
Day(){

}
Day(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
void dayIncrement() {
value++;
}
void dayReduction() {
value--;
}
}
class DateUtil{
private Day day;
private Year year;
private Month month;
private int[] monmaxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
DateUtil(){
}
DateUtil(int y,int m,int d){
this.year = new Year(y);
this.month = new Month(m);
this.day = new Day(d);
}
Year getYear() {
return year;
}
void setYear(Year year) {
this.year = year;
}
Month getMonth() {
return month;
}
void setMonth(Month month) {
this.month = month;
}
Day getDay() {
return day;
}
void setDay(Day day){
this.day =day;
}
void setDayMin() {
this.day = new Day(1);
}
void setDayMax() {
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
this.day = new Day(monmaxnum[month.getValue()-1]);
}
boolean checkInputValidity() {//判断是否合法
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(year.validate()==true&&month.validate()==true&&day.getValue()>0&&day.getValue()<=monmaxnum[month.getValue()-1]) {
return true;
}
else
return false;
}
DateUtil getNextNDays(int n) {//获取下N天
for(int i=0;i<n;i++){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(month.getValue()==12&&day.getValue()==31){
this.year.setValue(year.getValue()+1);
this.month.setValue(1);
this.day.setValue(1);
}
else if(day.getValue()==monmaxnum[month.getValue()-1]){
this.month.setValue(month.getValue()+1);
this.day.setValue(1);
}
else
this.day.setValue(day.getValue()+1);
}
return new DateUtil(year.getValue(),month.getValue(),day.getValue());
}
DateUtil getPreviousNDays(int n) {//获取前N天
for(int i=0;i<n;i++){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(month.getValue()==1&&day.getValue()==1){
this.year.setValue(year.getValue()-1);
this.month.setValue(12);
this.day.setValue(31);
}
else if(day.getValue()==1){
this.month.setValue(month.getValue()-1);
this.day.setValue(monmaxnum[month.getValue()-1]);
}
else
this.day.setValue(day.getValue()-1);
}
return this;
}
boolean compareDates(DateUtil date) {//比较两个日期的大小
if(year.getValue()>date.year.getValue())
return false;
else if(year.getValue()<date.year.getValue())
return true;
else{
if(month.getValue()>date.month.getValue())
return false;
else if(month.getValue()>date.month.getValue())
return true;
else{
if(day.getValue()>date.day.getValue())
return false;
else
return true;
}
}
}
boolean equalTwoDates(DateUtil date) {//判断是否相等
if(day.getValue()==date.day.getValue()&&year.getValue()==date.year.getValue()&&month.getValue()==date.month.getValue())
return true;
else
return false;
}
String showDate() {//输出日期
String s1 = year.getValue()+"-"+month.getValue()+"-"+day.getValue();
return s1;
}
int getDaysofDates(DateUtil date) {//判断日期之间的相差天数
int druingday=0,d=0,i,y;
if(equalTwoDates(date))
return 0;
else{
if(compareDates(date)){//前面的日期比后面的小时
if(year.getValue()<date.year.getValue()){
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<month.getValue()-1;i++){
d+= monmaxnum[i];
}
d+=day.getValue();
druingday=y-d;
while(date.year.getValue()-year.getValue()>1){
year=new Year(year.getValue()+1);
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
druingday+=y;
}
if(date.year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<date.month.getValue()-1;i++)
druingday+= monmaxnum[i];
druingday+=date.day.getValue();
}
else{

if(month.getValue()<date.month.getValue()){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
druingday= monmaxnum[month.getValue()-1]-day.getValue();
for(i=month.getValue();i<date.month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=date.day.getValue();
}
else
druingday=date.day.getValue()-day.getValue();
}
}
else{//前面的日期比后面的大时
if(year.getValue()>date.year.getValue()){
if(date.year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<date.month.getValue()-1;i++){
d+=monmaxnum[i];
}
d+=date.day.getValue();
druingday=y-d;
while(year.getValue()-date.year.getValue()>1){
date.year=new Year(date.year.getValue()+1);
if(date.year.isLeapYear()){
y=366;
monmaxnum[1]=29;
}
else{
y=365;
monmaxnum[1]=28;
}
druingday+=y;
}
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=day.getValue();
}
else {
if(date.month.getValue()<month.getValue()){
if(date.year.isLeapYear()){
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
druingday= monmaxnum[date.month.getValue()-1]-date.day.getValue();
for(i=date.month.getValue();i<month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=day.getValue();
}
else
druingday=day.getValue()-date.day.getValue();
}
}
}
return druingday;
}
}

 

(3)踩坑心得

第四次题目集的第一题菜单没有做出来,只过了一个测试点,实在是难受啊;

 

 第二题的话刚开始有两个测试点没过,因为我用的是数组的方法,后来发现是因为内存超限,所以这道题我后面用了 java.util.Arrays.sort(a),将他快速排序就可以通过测试点了

 第三题我刚开始用的是数组的方法将他们排序组合,但是最后还是有测试点没过,为最后两个运行超时的测试点

 之后就用了LinkedHashSet<String> list = new LinkedHashSet<String>();的方法进行去重,就可以通过测试点了,这个Hashset的方法还是很好用的,后面第四题就用到了这种方法,比较方便操作;附上此方法的代码,方便理解哈

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
LinkedHashSet<String> list = new LinkedHashSet<String>();
int i,flag=0;
for(i=0;i<n;i++){
list.add(input.next());
}
for(String s : list){
if(flag==0){
System.out.print(s);
flag=1;
}
else
System.out.print(" "+s);
}
}
}

 第四题用到了书上的compareToIgnoreCase()的方法来比较他们之间的大小,这个是忽略大小写的,刚好和题目符合;刚开始没通过测试点是因为自己的冒泡排序没有弄好,将i和j写反了,所以得到的结果有点问题;就会有测试点没过;

 但是之后修改了就可以了,这里也用到了之前提到的hashmap方法,可以去重,将重复的单词去掉;

 正确代码:

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String[] strs = str.split("[,. ]");
LinkedHashSet<String> link = new LinkedHashSet<String>();
int i,j;
for(i = 0;i<(strs.length)-1;i++){
for(j = 0;j<(strs.length)-1-i;j++){
if(strs[j].length()<strs[j+1].length()){
StringBuffer t = new StringBuffer(strs[j]);//将String变为StringBuffer
strs[j] = strs[j+1];
strs[j+1] = t.toString();//将StringBuffer变为String
}
if(strs[j].length()==strs[j+1].length()){
if(strs[j].compareToIgnoreCase(strs[j+1])>0){//比较相同长度的大小,按升序输出
StringBuffer a = new StringBuffer(strs[j]);
strs[j] = strs[j+1];
strs[j+1] = a.toString();
}
}
}
}//冒泡排序将字符串按长度从大到小排好
for( i = 0;i<strs.length-1;i++){//去除重复字符串
link.add(strs[i]);
}
for(String s : link) {
System.out.println(s);
}
}
}

第五题直接进行有参构造和无参构造以及get,set方法的运用,所以直接通过了测试点

 第六题直接对给出的要求进行输出就好了;

第七题需要自己对Date的方法充分运用,先学习方法然后就可以比较好的使用他们;这里附上代码以便学习参考

import java.util.*;
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String date1sc = input.nextLine();
String date2sc = input.nextLine();//将日期输入
String[] date1s = date1sc.split("-");//将输入的日期按照“-”分隔开
String[] date2s = date2sc.split("-");
LocalDate date1 = LocalDate.of(Integer.parseInt(date1s[0]),Integer.parseInt(date1s[1]),Integer.parseInt(date1s[2]));//对日期直接进行赋值,便于后面的日期计算
LocalDate date2 = LocalDate.of(Integer.parseInt(date2s[0]),Integer.parseInt(date2s[1]),Integer.parseInt(date2s[2]));
// LocalDate date = new LocalDate();
if(date1.isBefore(date2)){//第一个日期大于第二个日期
System.out.println("第一个日期比第二个日期更早");
System.out.println("两个日期间隔" + ChronoUnit.DAYS.between(date1,date2)+"天");
System.out.println("两个日期间隔" + ChronoUnit.WEEKS.between(date1,date2)+"周");
}
else{//第一个日期小于第二个日期
System.out.println("第一个日期比第二个日期更晚");
System.out.println("两个日期间隔" + ChronoUnit.DAYS.between(date2,date1)+"天");
System.out.println("两个日期间隔" + ChronoUnit.WEEKS.between(date2,date1)+"周");
}

}
}

第五次题目集的第一题用到的是正则表达式,关键的表达式为:  String regex = "[1-9]\\d{4,14}";

第二题用到的是将字符串数组排序,主要用到的是Arrays.sort(s);将字符串数组排序;

第三题用到的是正则表达式,关键的表达式为:·String·regex·=·"[\\w&&[^_]]{4}";

第四题  用到的是正则表达式,关键的表达式为:String regex = "(20201[1-7]|202061|20207[1-3]|20208[1-2])(([0-3][1-9])|([1-4]0));

第五题是日期的迭代,用到的是聚合一,和之前用到的,这次是一次一次的调用方法;

 这里给出相应的代码:

 

 

 

import java.util.Scanner;
//主函数
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day,month,year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

// System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day,month,year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

// System.out.print(
// date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day,month,year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println( fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
//Year类
class Year{
private int value;
Year(){

}
Year(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
boolean isLeapYear() {
if(value%4==0&&value%100!=0||value%400==0)
return true;
else
return false;
}
boolean validate() {
if(value>=1900&&value<=2050)
return true;
else
return false;
}
void yearIncrement() {
value++;
}
void yearReduction() {
value--;
}
}
//Month类
class Month{
private int value;
private Year year;
Month(){

}
Month(int yearValue,int monthValue){
this.value = monthValue;
this.year = new Year(yearValue);
}
int getValue() {
return this.value ;
}
void setValue(int value) {
this.value = value;
}
Year getYear() {
return year;
}
void setYear(Year year) {
this.year = year;
}
void resetMin(){
this.value = 1;
}
void resetMax() {
this.value = 12;
}
boolean validate() {
if(value>=1&&value<=12)
return true;
else
return false;
}
void monthIncrement() {
if(value>=1&&value<12)
value++;
else {
value = 1;
year.yearIncrement();
}
}
void monthReduction() {
if(value>1&&value<=12)
value--;
else {
value = 12;
year.yearReduction();
}
}
}
//Day类
class Day{
private int value;
private Month month;
private int[] monmaxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
Day(){

}
Day(int yearValue,int monthValue,int dayValue){
this.value = dayValue;
month = new Month(yearValue,monthValue);
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
Month getMonth() {
return month;
}
void setMonth(Month value) {
this.month = value;
}
void resetMin() {
value = 1;
}
void resetMax() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
value = monmaxnum[month.getValue()-1];
}
boolean validate() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value>=1&&value<=monmaxnum[month.getValue()-1]) {
return true;
}
else
return false;

}
void dayIncrement() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value==monmaxnum[month.getValue()-1]) {
value = 1;
month.monthIncrement();
}
else
value++;

}
void dayReduction() {
if(month.getYear().isLeapYear()==true) {
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
if(value==1) {
month.monthReduction();
value=monmaxnum[month.getValue()-1];
}
else
value--;
}
}
//DateUtil类
class DateUtil{
Day day;
DateUtil(){
}
DateUtil(int d,int m,int y){
this.day = new Day(y, m,d);
}
Day getDay() {
return day;
}
void setDay(Day d){
this.day =d;
}
boolean checkInputValidity() {
if(day.getMonth().getYear().validate()==true&&day.getMonth().validate()==true&&day.validate()==true) {
return true;
}
else
return false;
}
boolean compareDates(DateUtil date) {
if(day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue())
return false;
else if(day.getMonth().getYear().getValue()<date.day.getMonth().getYear().getValue())
return true;
else{
if(day.getMonth().getValue()>date.day.getMonth().getValue())
return false;
else if(day.getMonth().getValue()>date.day.getMonth().getValue())
return true;
else{
if(day.getValue()>date.day.getValue())
return false;
else
return true;
}
}
}
boolean equalTwoDates(DateUtil date) {
if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&day.getValue()==date.day.getValue())
return true;
else
return false;
}
String showDate() {
String s1 = day.getMonth().getYear().getValue()+"-"+day.getMonth().getValue()+"-"+day.getValue();
return s1;
}
DateUtil getNextNDays(int n) {
for(int i = 0;i<n;i++) {
day.dayIncrement();
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
DateUtil getPreviousNDays(int n) {
for(int i = 0;i<n;i++) {
day.dayReduction();
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
int getDaysofDates(DateUtil date) {
int c = 0;
if(equalTwoDates(date)==true) {
return c;
}
else {
if(compareDates(date)==true) {
while(equalTwoDates(date)==false) {
day.dayIncrement();
c++;
}
}
else if(compareDates(date)==false) {
while(equalTwoDates(date)==false) {
day.dayReduction();
c++;
}

}
return c;
}

}
}

第六题用到的是聚合二;这次是在主方法中用year,month,day类中的方法;

 

 这里给出相应的代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year,month,day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year,month,day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(year,month,day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println( "The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"+fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class Year{
private int value;
Year(){

}
Year(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
boolean isLeapYear() {//判断是否为闰年
if(value%4==0&&value%100!=0||value%400==0)
return true;
else
return false;
}
boolean validate() {//判断是否合法
if(value>=1820&&value<=2020)
return true;
else
return false;
}
void yearIncrement() {
value++;
}
void yearReduction() {
value--;
}
}

class Month{
private int value;
Month(){

}
Month(int value){
this.value = value;
}
int getValue() {
return this.value ;
}
void setValue(int value) {
this.value = value;
}

void resetMin(){
this.value = 1;
}
void resetMax() {
this.value = 12;
}
boolean validate() {//判断是否合法
if(value>=1&&value<=12)
return true;
else
return false;
}
void monthIncrement() {
value++;
}
void monthReduction() {
value--;
}
}

class Day{
private int value;
Day(){

}
Day(int value){
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
void dayIncrement() {
value++;
}
void dayReduction() {
value--;
}
}
class DateUtil{
private Day day;
private Year year;
private Month month;
private int[] monmaxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
DateUtil(){
}
DateUtil(int y,int m,int d){
this.year = new Year(y);
this.month = new Month(m);
this.day = new Day(d);
}
Year getYear() {
return year;
}
void setYear(Year year) {
this.year = year;
}
Month getMonth() {
return month;
}
void setMonth(Month month) {
this.month = month;
}
Day getDay() {
return day;
}
void setDay(Day day){
this.day =day;
}
void setDayMin() {
this.day = new Day(1);
}
void setDayMax() {
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
this.day = new Day(monmaxnum[month.getValue()-1]);
}
boolean checkInputValidity() {//判断是否合法
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(year.validate()==true&&month.validate()==true&&day.getValue()>0&&day.getValue()<=monmaxnum[month.getValue()-1]) {
return true;
}
else
return false;
}
DateUtil getNextNDays(int n) {//获取下N天
for(int i=0;i<n;i++){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(month.getValue()==12&&day.getValue()==31){
this.year.setValue(year.getValue()+1);
this.month.setValue(1);
this.day.setValue(1);
}
else if(day.getValue()==monmaxnum[month.getValue()-1]){
this.month.setValue(month.getValue()+1);
this.day.setValue(1);
}
else
this.day.setValue(day.getValue()+1);
}
return new DateUtil(year.getValue(),month.getValue(),day.getValue());
}
DateUtil getPreviousNDays(int n) {//获取前N天
for(int i=0;i<n;i++){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
if(month.getValue()==1&&day.getValue()==1){
this.year.setValue(year.getValue()-1);
this.month.setValue(12);
this.day.setValue(31);
}
else if(day.getValue()==1){
this.month.setValue(month.getValue()-1);
this.day.setValue(monmaxnum[month.getValue()-1]);
}
else
this.day.setValue(day.getValue()-1);
}
return this;
}
boolean compareDates(DateUtil date) {//比较两个日期的大小
if(year.getValue()>date.year.getValue())
return false;
else if(year.getValue()<date.year.getValue())
return true;
else{
if(month.getValue()>date.month.getValue())
return false;
else if(month.getValue()>date.month.getValue())
return true;
else{
if(day.getValue()>date.day.getValue())
return false;
else
return true;
}
}
}
boolean equalTwoDates(DateUtil date) {//判断是否相等
if(day.getValue()==date.day.getValue()&&year.getValue()==date.year.getValue()&&month.getValue()==date.month.getValue())
return true;
else
return false;
}
String showDate() {//输出日期
String s1 = year.getValue()+"-"+month.getValue()+"-"+day.getValue();
return s1;
}
int getDaysofDates(DateUtil date) {//判断日期之间的相差天数
int druingday=0,d=0,i,y;
if(equalTwoDates(date))
return 0;
else{
if(compareDates(date)){//前面的日期比后面的小时
if(year.getValue()<date.year.getValue()){
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<month.getValue()-1;i++){
d+= monmaxnum[i];
}
d+=day.getValue();
druingday=y-d;
while(date.year.getValue()-year.getValue()>1){
year=new Year(year.getValue()+1);
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
druingday+=y;
}
if(date.year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<date.month.getValue()-1;i++)
druingday+= monmaxnum[i];
druingday+=date.day.getValue();
}
else{

if(month.getValue()<date.month.getValue()){
if(year.isLeapYear()==true)
monmaxnum[1] = 29;
else
monmaxnum[1] = 28;
druingday= monmaxnum[month.getValue()-1]-day.getValue();
for(i=month.getValue();i<date.month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=date.day.getValue();
}
else
druingday=date.day.getValue()-day.getValue();
}
}
else{//前面的日期比后面的大时
if(year.getValue()>date.year.getValue()){
if(date.year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<date.month.getValue()-1;i++){
d+=monmaxnum[i];
}
d+=date.day.getValue();
druingday=y-d;
while(year.getValue()-date.year.getValue()>1){
date.year=new Year(date.year.getValue()+1);
if(date.year.isLeapYear()){
y=366;
monmaxnum[1]=29;
}
else{
y=365;
monmaxnum[1]=28;
}
druingday+=y;
}
if(year.isLeapYear()==true) {
monmaxnum[1] = 29;
y = 366;
}
else {
monmaxnum[1] = 28;
y = 365;
}
for(i=0;i<month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=day.getValue();
}
else {
if(date.month.getValue()<month.getValue()){
if(date.year.isLeapYear()){
monmaxnum[1]=29;
}
else
monmaxnum[1]=28;
druingday= monmaxnum[date.month.getValue()-1]-date.day.getValue();
for(i=date.month.getValue();i<month.getValue()-1;i++){
druingday+=monmaxnum[i];
}
druingday+=day.getValue();
}
else
druingday=day.getValue()-date.day.getValue();
}
}
}
return druingday;
}
}

第六次题目集的菜单问题没有做出来,不太会,只混到了15分,过了这几个测试点;

 

 

 本人能力有限,做不出来这类的题目,如果有大佬做出来了,可以给个代码啊;

 

 

 

(4)改进建议

第四次题目集感觉没啥改进的,第五次题目集我之前并不是用正则表达式来写而是用比较常用的方法;后面的几个正则表达式也是用这种常用的方法,这是第一题的代码:

 

 char[] s = new char[s1.length()];
int i,flag = 0;
for(i=0;i<s1.length();i++){
s[i] = s1.charAt(i);
if(!(s[i]>='0'&&s[i]<='9')){
flag = 1;
}
}
if(s1.length()>=5&&s1.length()<=15&&s[0]!=0&&flag==0)
System.out.println("你输入的QQ号验证成功");
else
System.out.println("你输入的QQ号验证失败");

第二题之前用到的是比较常规的方法:

···char[]·s·=·new·char[s1.length()];
········int·i,j;
········for(i=0;i<s1.length();i++)
·············s[i]·=·s1.charAt(i);
········for(i=0;i<s1.length()-1;i++){
············for(j=0;j<s1.length()-1-i;j++){
················if(s[j]>s[j+1]){
···················char·t·=·s[j];
····················s[j]·=·s[j+1];
····················s[j+1]·=·t;
················}
············}
········}
········for(i=0;i<s1.length();i++){
···········System.out.print(s[i]);
········}

后来发现字符串数组也可以用Arrays.sort();所以就改进了之前的方法:

String[] s = s1.split("");//将字符串分割为单个字符串存入字符串数组
Arrays.sort(s);
for(int i=0;i<s1.length();i++)
System.out.print(s[i]);

 

 

(5)总结

通过这几次练习能够感觉自己和别人的差距,别人的菜单pta都是满分,而自己却做不出来,看得出来别人在这方面花了比较多的时间,所以自己也是要多花点时间去努力的提升自己;这几次练习主要考察的是运用类和类的方法,正则表达式的学习,hashmap的使用,arrays.sort()的方法将他排序,以及冒泡排序的使用;让自己有更强的动力去自学,自己去学习方法,而不是停留在老师讲的层面,但是在这里建议老师可以给出一些难题的正确代码,因为现在不能给其他同学看代码,所以这个问题就很棘手,每次有不会的又不知道该怎么办,同学不会告诉你怎么写,你听他们讲讲思路有时也不太会,而且有些题目他们可能也不会教你,因为他们怕和你写成一样的代码,怕查重,所以说感觉这一点不是很好,这样的话,很难得到提升,就会陷入生生的自我怀疑和不自信,精神内好自己,所以说,希望老师上课可以讲一讲这些习题,还有之前在课堂上的讲的一些小组作业,其实上课的时候老师可以讲一下,这样的话就不会有同学不会了,要不然自己写的代码,也不知道对不对哎;但是在我看来,这门课程还是要有思维要多练,多做题,思维要打开;

posted @ 2023-04-25 23:11  菜要多练丫  阅读(19)  评论(0编辑  收藏  举报