(1)训练集02 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){ System.out.println("Wrong Format"); } else { if(a+b<=c||a+c<=b||b+c<=a){ System.out.println("Not a triangle"); } else { if(a==b&&b==c){ System.out.println("Equilateral triangle"); } else if ((a*a+b*b)==c*c||(a*a+c*c)==b*b||(b*b+c*c)==a*a) { if((a/Math.sqrt(2)==b&&a/Math.sqrt(2)==c)||(b/Math.sqrt(2)==a&&a/Math.sqrt(2)==c)||(c/Math.sqrt(2)==a&&c/Math.sqrt(2)==b)){ System.out.println("Isosceles right-angled triangle"); } else { System.out.println("Right-angled triangle"); } } else if (a==b||a==c||b==c) { System.out.println("Isosceles triangle"); } else { System.out.println("General triangle"); } } } } }
(2)训练集03 7-3 定义日期类
我的思路:
|
|
|
|
|
|
|
|
|
|
|
我的代码:
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(); //创建类Date Date Date=new Date(); Date.setYear(year); Date.setMonth(month); Date.setDay(day); //判断是否输入合法 if(Date.checkInputValidity()){ Date.getNextDate(); System.out.println("Next day is:"+Date.getYear()+"-"+Date.getMonth()+"-"+Date.getDay()); }//输入合法 else { System.out.println("Date Format is Wrong"); }//输入不合法 } } class Date { private int year; private int month; private int day; private int[] mon_maxnum1=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//平年 private int[] mon_maxnum2=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年 public Date() { } public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } 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; } public boolean isLeapYear(int year){ return !(year % 4 == 0 && year % 100 != 0 || year % 400 == 0);//是平年返回true,不是平年返回false } public boolean checkInputValidity(){ if(year<1900||year>2000||month<1||month>12||day<1||day>31){ return false;//超出合法取值 } else if (isLeapYear(year)&&day>mon_maxnum1[month]) { return false;//平年的情况 } else return isLeapYear(year)||day<=mon_maxnum2[month]; //闰年的情况,三种情况都不满足就返回true } public void getNextDate(){ if(month==12&&day==31){ year=year+1; month=1; day=1;//一年的最后一天 }else if(isLeapYear(year)&&day==mon_maxnum1[month]){ month=month+1; day=1;//平年每月最后一天 } else if ((!isLeapYear(year))&&day==mon_maxnum2[month]) { month=month+1; day=1;//闰年每月最后一天 }else { day=day+1;//其他的日期 } } }
(3)训练集03
我的思路:
|
|
|
|
|
|
代码:主方法
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() + "-" + 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(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() + "-" + 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(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); } } }
DateUtil类
class DateUtil { private int year; private int month; private int day; private int[] mon_maxNum1=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//平年 private int[] mon_maxNum2=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年 public DateUtil() { } public DateUtil(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } 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; } //判断year是否为闰年 public boolean isLeapYear(int year){ return !(year % 4 == 0 && year % 100 != 0 || year % 400 == 0);//是平年返回true,不是平年返回false } //检测输入的年、月、日是否合法 public boolean checkInputValidity(){ if(year<1820||year>2020||month<1||month>12||day<1||day>31){ return false;//超出合法取值 } else if (isLeapYear(year)&&day>mon_maxNum1[month]) { return false;//平年的情况 } else return isLeapYear(year)||day<=mon_maxNum2[month]; //闰年的情况,三种情况都不满足就返回true } //以“year-month-day”格式返回日期值 public String showDate(){ return year+"-"+month+"-"+day; } //取得year-month-day的下n天日期 public DateUtil getNextNDays(int n){ while(n>365){ if(isLeapYear(year)){ n=n-365; year=year+1; } else{ n=n-366; year=year+1; } } for (int i = 1; i<=n; i++) { if (month == 12 && day == 31) { year = year + 1; month = 1; day = 1;//一年的最后一天 } else if (isLeapYear(year) && day == mon_maxNum1[month]) { month = month + 1; day = 1;//平年每月最后一天 } else if ((!isLeapYear(year)) && day == mon_maxNum2[month]) { month = month + 1; day = 1;//闰年每月最后一天 } else { day = day + 1;//其他的日期 } } showDate(); return new DateUtil(getYear(),getMonth(),getDay()); } //取得year-month-day的前n天日期 public DateUtil getPreviousNDays(int n){ while(n>365){ if(isLeapYear(year)){ n=n-365; year=year-1; } else{ n=n-366; year=year-1; } } for(int i=1;i<=n;i++) { if (month == 1 && day == 1) { year = year - 1; month = 12; day = 31;//一年的第一天 } else if (isLeapYear(year) && day == 1) { month = month - 1; day = mon_maxNum1[month];//平年每月第一天 } else if ((!isLeapYear(year)) && day == 1) { month = month - 1; day = mon_maxNum2[month];//闰年每月第一天 } else { day = day - 1;//其他的日期 } } showDate(); return new DateUtil(getYear(),getMonth(),getDay()); } //比较当前日期与date的大小(先后),当前日期<date返回true,反之返回false public boolean compareDates(DateUtil date){ if(year<date.getYear()){ return true; } else if (year==date.getYear()&&month<date.getMonth()) { return true; } else return year == date.getYear() && month == date.getMonth() && day < date.getDay(); } //判断两个日期是否相等,相等返回true,反之返回false public boolean equalTwoDates(DateUtil date){ return year==date.getYear()&&month==date.getMonth()&&day==date.getDay(); } //求当前日期与date之间相差的天数 public int getDaysofDates(DateUtil date){ int n=0;//相差的天数 if(equalTwoDates(date)){//相等 return n; } else if (compareDates(date)) {//当前天数<date while(!equalTwoDates(date)){ if(month==12&&day==31){ year=year+1; month=1; day=1;//一年的最后一天 }else if(isLeapYear(year)&&day==mon_maxNum1[month]){ month=month+1; day=1;//平年每月最后一天 } else if ((!isLeapYear(year))&&day==mon_maxNum2[month]) { month=month+1; day=1;//闰年每月最后一天 }else { day=day+1;//其他的日期 } n++; } return n; } else{//当前天数>date while(!equalTwoDates(date)){ if (month == 1 && day == 1) { year = year - 1; month = 12; day = 31;//一年的第一天 } else if (isLeapYear(year) && day == 1) { month = month - 1; day = mon_maxNum1[month];//平年每月第一天 } else if ((!isLeapYear(year)) && day == 1) { month = month - 1; day = mon_maxNum2[month];//闰年每月第一天 } else { day = day - 1;//其他的日期 } n++; } return n; } } }