pta作业总结
输入三角形三条边,判断该三角形为什么类型的三角形。
输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。
输出格式:
(1)如果输入数据非法,则输出“Wrong Format”; (2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”; (3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”; (3)如果输入数据合法且能够成等腰直角三角形,则输出“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
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double a[] = new double[3];
int i;
for(i = 0; i < 3; i++){
a[i] = in.nextDouble();
}
for(i = 0; i < 3; i++){
if(a[i] < 1 || a[i] > 200){
System.out.println("Wrong Format");
return;
}
}
Arrays.sort(a);
if(a[0] + a[1] > a[2]){
if(a[0] == a[1] && a[1] == a[2]){
System.out.println("Equilateral triangle");
}
else if(a[0] == a[1] && a[0] * a[0] + a[1] * a[1] - a[2] * a[2] < 0.000001)
System.out.println("Isosceles right-angled triangle");
else if(a[2] == a[1] || a[2] == a[0] || a[0] == a[1])
System.out.println("Isosceles triangle");
else if(a[0] * a[0] + a[1] * a[1] - a[2] * a[2] < 0.0000010)
System.out.println("Right-angled triangle");
else{
System.out.println("General triangle");
}
}
else
System.out.println("Not a triangle");
}
运行截图:


小结:
这道题考查的是基础的for循环与if-else结构,需要理清各种情况对应的输出
输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[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) ; //求输入日期的下一天
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
- 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
输入样例1:
在这里给出一组输入。例如:
2020 3 10
输出样例1:
在这里给出相应的输出。例如:
Next date is:2020-3-11
输入样例2:
在这里给出一组输入。例如:
2025 2 10
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static boolean isLeapYear(int year) {
boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
return isLeapYear;
}
public static boolean checkInputValidity(int year,int month,int day) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0);
return checkInputValidity;
}
public static void nextDate(int year,int month,int day) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
int a = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day)) {
if(month==12) {
if(day==aa[month]) {
a = year+1;
b = 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c =day +1;
}
}
if(month<12) {
if(day==aa[month]) {
a = year;
b = month + 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c = day+1;
}
}
System.out.println("Next date is:"+a+"-"+b+"-"+c);
}
else System.out.println("Wrong Format");
}
public static void main(String[] args) {
Scanner zyh = new Scanner(System.in);
Main ZYH = new Main();
int year = zyh.nextInt();
int month = zyh.nextInt();
int day = zyh.nextInt();
ZYH.nextDate(year,month,day);
}
}
小结:关于年月日的问题一个很重要的问题就是判断是否为闰年,如果为闰年就要设置2月份为29天;另一个关键点是在某年或某月的最后一天下一天需要改变原来的基础上加一。
输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。
输入格式:
在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。
输出格式:
- 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
- 当输入数据合法时,输出“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
import java.util.Scanner;
public class Main {
public static boolean isLeapYear(int year) {
boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
return isLeapYear;
}
public static boolean checkInputValidity(int year,int month,int day,int n) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0&&Math.abs(n)<=10&&Math.abs(n)>=0);
return checkInputValidity;
}
public static void nextDate(int year,int month,int day,int n) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
int a = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day,n)) {
if (n==0) {
a = year;
b = month;
c = day;
}
if(month==12&&n>0) {
if(day-n<=aa[month]&&day-n>0) {
a = year;
b = month;
c = day-n;}
if(day-n<=aa[month]&&day-n<0) {
a = year;
b = month-1;
c = aa[b]-n+day;
}
if(day-n<=aa[month]&&day-n==0) {
a = year;
b = month-1;
c = aa[b]-n+day;
}
}
if(month==12&&n<0) {
if(day-n>aa[month]) {
a = year +1;
b = 1;
c =day - n -aa[month];
}
if(day-n<=aa[month]) {
a = year;
b = month;
c =day - n;
}
}
if(month>1&&month<12&&n>0) {
if(day-n<=aa[month]&&day-n>0) {
a = year;
b = month;
c = day-n;}
if(day-n<=aa[month]&&day-n<0) {
a = year;
b = month-1;
c = aa[b]-n+day;
}
if(day-n<=aa[month]&&day-n==0) {
a = year;
b = month-1;
c = aa[b]-n+day;
}
}
if(month<12&&month>1&&n<0) {
if(day-n>aa[month]) {
a = year;
b = month+1;
c =day - n -aa[month];
}
if(day-n<=aa[month]) {
a = year;
b = month;
c =day - n;
}
}
if(month == 1&&n>0) {
if(day-n<=aa[month]&&day-n>0) {
a = year;
b = month;
c = day-n;
}
if(day-n<=aa[month]&&day-n<0) {
a = year-1;
b = 12;
c = aa[b]-n+day;
}
if(day-n<=aa[month]&&day-n==0) {
a = year-1;
b = 12;
c = aa[b]-n+day;
}
}
if(month==1&&n<0) {
if(day-n>aa[month]) {
a = year;
b = month+1;
c =day - n -aa[month];
}
if(day-n<=aa[month]) {
a = year;
b = month;
c =day - n;
}
}
System.out.println(n+" days ago is:"+a+"-"+b+"-"+c);
}
else System.out.println("Wrong Format");
}
public static void main(String[] args) {
Scanner zyh = new Scanner(System.in);
Main ZYH = new Main();
int year = zyh.nextInt();
int month = zyh.nextInt();
int day = zyh.nextInt();
int n = zyh.nextInt();
ZYH.nextDate(year,month,day,n);
}
}
运行截图:


小结:程序无法识别最后输入的天数所以输出的还是下一天,有待改进
定义一个类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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner zyh = new Scanner(System.in); Main ZYH = new Main(); int year = zyh.nextInt(); int month = zyh.nextInt(); int day = zyh.nextInt(); ZYH.nextDate(year, month, day); } public static void nextDate(int year,int month,int day) { int[] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear(year)==1) mon_maxnum[2] = 29; int y = 0,m = 0,d = 0; if(checkInputValidity(year,month,day)==1) { if(month==12) { if(day==31) { y = year+1; m = 1; d = 1;} else { y = year; m = month; d =day +1; } } if(month<12) { if(day==mon_maxnum[month]) { y = year; m = month + 1; d = 1;} if(day>0&&day<mon_maxnum[month]) { y = year; m = month; d = day+1; } } System.out.println("Next day is:"+y+"-"+m+"-"+d); } else System.out.println("Date Format is Wrong"); } public static int isLeapYear(int year) { int n; if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) { return 1; } else { return 0; } } public static int checkInputValidity(int year, int month, int day) { int[] mon_maxnum = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)==1) mon_maxnum[2] = 29; if (year >= 1900 && year <= 2000 && month > 0 && month <= 12 && day <= mon_maxnum[month] && day > 0) { return 1; } else{ return 0; } } }
运行截图:
![]()
![]()
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf
输入格式:
在一行内输入一个待计算导函数的表达式,以回车符结束。
输出格式:
- 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
- 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
- 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
- 当输出结果第一项系数符号为“+”时,不输出“+”;
- 当指数符号为“+”时,不输出“+”;
- 当指数值为“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
import java.math.BigInteger; import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; import java.util.regex.Pattern; import java.math.BigInteger; import java.util.regex.Pattern; public class Main { private ArrayList<Poly> polyList = new ArrayList<>(); private static final BigInteger MINUXONE = BigInteger.valueOf(-1); private void parsePoly(String[] polyArr) { for (String str : polyArr) { if (str.isEmpty()) { continue; } else if (!Pattern.matches(".*x\\^.*", str)) { polyList.add(new Poly(str, "0")); } else { String[] temp = str.split("\\*x\\^"); polyList.add(new Poly(temp[0], temp[1])); } } } private void computePoly() { for (int i = 0; i < polyList.size(); i++) { for (int j = i + 1; j < polyList.size(); j++) if (polyList.get(i).getDeg().equals(polyList.get(j).getDeg()) && !polyList.get(j).isRepeat()) { polyList.get(i).addTerm(polyList.get(j).getTerm()); polyList.get(j).setRepeat(true); } polyList.get(i).duf(); } } private void output() { BigInteger constant = BigInteger.ZERO; boolean firstOut = true; for (Poly p : polyList) { if (p.isRepeat()) { continue; } else if (p.isCons()) { constant = p.getTerm(); } else if (firstOut) { if (p.getTerm().compareTo(BigInteger.ONE) == 0) { System.out.print("x"); } else if (p.getTerm().compareTo(MINUXONE) == 0) { System.out.print("-x"); } else if (p.getTerm().compareTo(BigInteger.ZERO) > 0) { System.out.print(p.getTerm() + "*x"); } else { System.out.print(p.getTerm() + "*x"); } firstOut = false; } else if (p.getTerm().compareTo(BigInteger.ONE) == 0) { System.out.print("+x"); } else if (p.getTerm().compareTo(MINUXONE) == 0) { System.out.print("-x"); } else if (p.getTerm().compareTo(BigInteger.ZERO) > 0) { System.out.print("+" + p.getTerm() + "*x"); } else { System.out.print(p.getTerm() + "*x"); } if (p.getDeg().compareTo(BigInteger.ONE) != 0 && p.getDeg().compareTo(BigInteger.ZERO) != 0) { System.out.print("^" + p.getDeg()); } } if (constant.compareTo(BigInteger.ZERO) >= 0) { if (firstOut) { System.out.println(constant); } else if (constant.compareTo(BigInteger.ZERO) > 0) { System.out.println("+" + constant); } } else if (constant.compareTo(BigInteger.ZERO) < 0) { System.out.println(constant); } } public static void main(String[] args) { Scanner input = new Scanner(System.in); String readLine = input.nextLine(); StringChecker stringChecker = new StringChecker(readLine); String[] polyArr = stringChecker.checkPoly(); if(stringChecker.recheck(readLine)==0){ return; } Main comPoly = new Main(); comPoly.parsePoly(polyArr); comPoly.computePoly(); comPoly.output(); } public static String throwl(String str){ return str = str.trim(); } public class Poly { private BigInteger term; private BigInteger deg; private boolean isCons; private boolean isRepeat; public BigInteger getDeg() { return deg; } public void addDeg(BigInteger deg) { this.deg = this.deg.add(deg); } public BigInteger getTerm() { return term; } public void addTerm(BigInteger term) { this.term = this.term.add(term); } public boolean isCons() { return isCons; } public void setCons(boolean cons) { isCons = cons; } public boolean isRepeat() { return isRepeat; } public void setRepeat(boolean repeat) { isRepeat = repeat; } public Poly(String term, String deg) { this.term = new BigInteger(term); this.deg = new BigInteger(deg); isCons = false; isRepeat = false; } public void duf() { if (term.compareTo(BigInteger.ZERO) != 0 && deg.compareTo(BigInteger.ZERO) != 0) { term = term.multiply(deg); deg = deg.subtract(BigInteger.ONE); if (deg.compareTo(BigInteger.ZERO) == 0) { isCons = true; } else { isCons = false; } } else { term = BigInteger.ZERO; deg = BigInteger.ZERO; isCons = true; isRepeat = true; } } } public static class StringChecker { private String stringLine; public StringChecker(String input) { stringLine = input; } private boolean spaceChecker(String str) { str=throwl(str); String space1 = ".*(\\d+\\s+\\d+).*"; boolean isLegal1 = Pattern.matches(space1, str); String space2 = ".*[+\\-^]\\s*[+\\-]\\s+\\d+.*"; boolean isLegal2 = Pattern.matches(space2, str); return isLegal1 | isLegal2; } private boolean opChecker(String str) { String char1 = ".*([+\\-]{3,}|[*^]{2,}|[+\\-][*^]).*"; boolean isLegal1 = Pattern.matches(char1, str); String char2 = ".*(\\*[+\\-]|\\^[+\\-]{2,}).*"; boolean isLegal2 = Pattern.matches(char2, str); String char3 = ".*(([x0-9]*\\*[^x])|(x\\^[+\\-]*x)).*"; boolean isLegal3 = Pattern.matches(char3, str); String char4 = "(.*[+\\-*^]$)|([*^].*)"; boolean isLegal4 = Pattern.matches(char4, str); return isLegal1 | isLegal2 | isLegal3 | isLegal4; } private boolean charChecker(String str) { boolean isLegal1 = Pattern.matches(".*[^\\dx*+\\-^]+.*", str); boolean isLegal2 = Pattern.matches(".*((\\dx)|(x\\d)).*", str); boolean isLegal3 = Pattern.matches(".*(x{2,}).*", str); boolean isLegal4 = Pattern.matches(".*((\\*\\d)|(x\\*)).*", str); boolean isLegal5 = Pattern.matches(".*((\\d\\^)|(\\^x)).*", str); return isLegal1 | isLegal2 | isLegal3 | isLegal4 | isLegal5; } public String[] checkPoly() { if (spaceChecker(stringLine)) { System.out.println("Wrong Format"); System.exit(0); } stringLine = stringLine.replaceAll("[ \t]", ""); if (stringLine.isEmpty()) { System.out.println("Wrong Format"); System.exit(0); } if (opChecker(stringLine)) { System.out.println("Wrong Format"); System.exit(0); } stringLine = stringLine.replaceAll("\\+\\+|--", "+"); stringLine = stringLine.replaceAll("\\+-|-\\+", "-"); if (charChecker(stringLine)) { System.out.println("Wrong Format"); System.exit(0); } if (stringLine.charAt(0) == 'x') { stringLine = "1*x" + stringLine.substring(1); } stringLine = stringLine.replaceAll("\\+x", "+1*x"); stringLine = stringLine.replaceAll("-x", "-1*x"); stringLine = stringLine.replaceAll("x\\+", "x^1+"); stringLine = stringLine.replaceAll("x-", "x^1-"); String temp = stringLine.substring(stringLine.length() - 1); if (temp.equals("x")) { stringLine = stringLine + "^1"; } stringLine = stringLine.replace("+", " +"); stringLine = stringLine.replace("-", " -"); stringLine = stringLine.replace("^ +", "^+"); stringLine = stringLine.replace("^ -", "^-"); return stringLine.split("[\\s]"); } public int recheck(String str) { str=throwl(str); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'x' && i != 0) { if (str.charAt(i - 2) == '0') { System.out.println("Wrong Format"); return 0; } } else if(str.charAt(i) == '^' && i != 0){ if (str.charAt(i + 1) == '0') { System.out.println("Wrong Format"); return 0; } } } return 1; } } class Student1 { private String name; private String num; private String clazz; private double score; public Student1(String name, String num, String clazz, double score) { this.name = name; this.num = num; this.clazz = clazz; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getClazz() { return clazz; } public void setClazz(String clazz) { this.clazz = clazz; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String toString() { return num + "," + name; } } class Stu { private String stuNum;//学号 private String name;//姓名 private String gender;//性别 private int age;//年龄 private int score;//分数 public Stu() { super(); } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String etName() { return name; } public void setName(String name) { this.name = name; } } class Newcompartor implements Comparator<Student1> { public int compare(Student1 o1, Student1 o2) { double ret = o1.getScore() - o2.getScore(); if (ret > 0) ret = -1; else if (ret < 0) ret = 1; else { ret = Integer.parseInt(o1.getNum()) - Integer.parseInt(o2.getNum()); if (ret > 0) { ret = 1; } else if (ret < 0) { ret = -1; } } return (int) ret; } String getWhatDay(int days) { String w = null; if (days % 7 == 1) { w = "Monday"; } if (days % 7 == 2) { w = "Tuesday"; } if (days % 7 == 3) { w = "Wednesday"; } if (days % 7 == 4) { w = "Thursday"; } if (days % 7 == 5) { w = "Friday"; } if (days % 7 == 6) { w = "Saturday"; } if (days % 7 == 0) { w = "Sunday"; } return w; } } }
运行截图:

![]()
![]()
踩坑心得:经常性出现在本机的编译软件上能够运行成功,并且可以得到正确的答案,但是
一旦放到pta上就出现编译错误或者答案错误,或者运行时间超时。
编码的改进意见:例如:①题目要求输入或输出一串数字,但并未指出该串数字类型是整型还是字符串类型
导致在编写代码过程中偶尔不知道使用哪种类型
②建议给出多一点输入样例,举例一个题目 输入年月日的值(均为整型数),输出该日期的下一天。 其中:
年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31]
该题并未给出输入2020年12月31日的输入样例,导致我们在编码过程中不知道在输入2020年12月31日
后会出现什么样的结果,因此需要反复测试,重复提交,不断更改代码,很是浪费时间求前N天的问题种同样有
该缺陷
总结:在上述三个题目中,我学到了很多东西,最重要的一点就是加强了我的编码能力,以前基本遇到编程的题目
即使是一个简单的编程题目,我都需要想好久时间才能能动手去做,并且昨晚需要花费好多时间,索然这次三个题目
也花费了我获得时间,但做完后我感觉编程原来也没有我想象的那么难,只要把思路整理好,构建好基本得流程
写代码也就议会的事
还有就是本阶段的三个题目做完后,让我懂得了无论是编写代码还是做别的什么事情,都要细心不浮躁,就别入求日期的
题目中,一旦粗心大意,就很容易漏东西,导致拿不到分数
我觉得我需要进一步学习得地方有很多,有编写代码得速度需要提高,数学公式需要加强掌握,还有需要进一步跟进
老师讲课得步伐。我对教师讲课得内容与质量无异议,但希望老师能够在讲到比较i男女的地方讲慢一点,多多照顾一下
基础不够牢固得同学
:
:
:
:







浙公网安备 33010602011771号