此次題目集是初學Java的題目,前兩次只是涉及Java的一些基本語法和格式,第三次要建立類,有點面向對象的意思了。題目量適中,有足夠的時間去寫。前兩次題目集題目難度還行,有C語言的基礎,循環和判斷語句用法和C語言差很少,容易上手。第三次題目集建立類是Java的新東西,通過幾個小時學習,對類有了大概的瞭解,完成了第一第二題,第三題要用正則表達式,感受蠻難的。java
判斷三角形類型,這裏應該是採用了最笨的方法,就一種一種狀況判斷,最後根據判斷狀況輸出相應內容。首先判斷是否能組成一個三角形,再判斷等腰、直角、等腰直角、等邊、普通。正則表達式
我想的是用一個變量的值來記錄三角形的判斷狀況,最後根據變量的值來判斷三角形類型。由於等腰直角既是等腰優點直角,若是判斷後直接輸出會致使輸出3個結果。學習
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); float a = input.nextFloat(); float b = input.nextFloat(); float c = input.nextFloat(); int x = 5; if(a >200 || b > 200 || c >200 || a < 1 || b < 1 || c < 1){ x = 6; System.out.println("Wrong Format"); } else{ if(a >= b + c || b >= a + c || c >= a + b) //不構成三角形 x = 0; if(-0.01 < a*a + c*c - b*b && a*a + b*b - c*c < 0.01||
-0.01 <a*a + c*c - b*b && b*b + c*c - a*a < 0.01 ||
-0.01 < a*a + c*c - b*b && a*a + c*c - b*b < 0.01 ) //直角 x = 1; if(a == b || b == c || a == c)//等腰 x = 2; if((a == b && -0.01 < a*a + c*c - b*b && a*a + b*b - c*c < 0.01 )||
( b == c && -0.01 <a*a + c*c - b*b && b*b + c*c - a*a < 0.01 )||
( a == c && -0.01 < a*a + c*c - b*b && a*a + c*c - b*b < 0.01) ) //等腰直角 x = 3; if(a == b && b == c)//等邊 x = 4; } if(x == 0 ) System.out.println("Not a triangle"); if(x == 1) System.out.println("Right-angled triangle"); if(x == 2) System.out.println("Isosceles triangle"); if(x == 3) System.out.println("Isosceles right-angled triangle"); if(x == 4) System.out.println("Equilateral triangle"); if(x == 5) System.out.println("General triangle"); } }
這個代碼雖然經過了,但仍是有許多待改進的地方。判斷方法能夠更簡潔些,判斷等腰直角能夠放在判斷等腰和直角一塊兒裏面。測試
首先要判斷輸入日期的年份是不是閏年、月份的一個月天數,閏年二月有29天而平年二月只有28天,有的月份一個月30天有的月份一個月31天。而後根據狀況給出相應結果。ui
import java.util.Scanner; public class Main{ public static boolean checkInputValidity(int year,int month,int day,boolean r){ boolean p; p = true; if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){ p = false; } if(!r && day > 28){ p = false; } return p; } public static void nextDate(int year,int month,int day,boolean r){ boolean p; int i = month,j = day; p = true; if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){ if(i == 12 && j == 31){ day = 1; year = year + 1; month = 1; } if(j == 31 && i != 12){ month = month + 1; day = 1; } if(j != 31){ day = day + 1; } } if(i == 4 || i == 6 || i == 9 || i == 11){ if(j == 30){ month = month + 1; day = 1; } if(j != 30){ day = day + 1; } } if(i == 2 && r){ if(j == 29){ month = 3; day = 1; } else{ day = day + 1; } } if(i == 2 && !r){ if(j == 28){ month = 3; day = 1; } else{ day = day + 1; } } System.out.printf("Next date is:%d-%d-%d",year,month,day); } public static void main(String[] args){ Scanner input = new Scanner(System.in); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if(checkInputValidity(year,month,day,r)){ nextDate(year,month,day,r); } else{ System.out.println("Wrong Format"); } } }
跟上一題差很少,首先也是要判斷日期,根據N來判斷斷是否要跨月,再根據前一個月或後一個月的天數來輸出相應日期。spa
import java.util.Scanner; public class Main{ public static boolean checkInputValidity(int year,int month,int day,boolean r){ boolean p; p = true; if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){ p = false; } if(!r && day > 28){ p = false; } return p; } public static void nextDate(int year,int month,int day,boolean r,int n){ int i = month,j = day; day = j + n; if(r&&i==2){ if(j + n>29){ day = j + n-29; month++; } } if(!r&&i==2){ if(j + n>28){ day = j + n-28; month++; } } if(r&&i==3){ if(j + n<=0){ day =29+j+n; month--; } } if(!r&&i==3){ if(j + n<=0){ day = j + n + 28; month--; } } if(n + j <=0){ if(i == 1 ){ year--; day = 31 + (n + j); month = 12; } if(i == 2 || i == 4 || i == 6 || i == 9 || i == 8 || i == 11){ day = 31 + (n + j); month--; } if(i == 5 || i == 7 || i == 10 || i == 12 ){ year--; day = 30 + (n + j); month = 12; } } if(n + j >30){ if(i == 12){ if(n+j > 31){ day =(n+j)-31; year++; month=1; } else day = 31; } if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10){ if(n+j > 31){ day =(n+j)-31; month++; } else day = 31; } if(i == 4 || i==6 || i==9 || i==11){ day =(n+j)-30; month++; } } System.out.printf("%d days ago is:%d-%d-%d",-n,year,month,day); } public static void main(String[] args){ Scanner input = new Scanner(System.in); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); int n = input.nextInt(); boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if(checkInputValidity(year,month,day,r)){ nextDate(year,month,day,r,-n); } else{ System.out.println("Wrong Format"); } } }
這裏就有點Java面相對象的意思了,建立一個Data類,包含私有屬性年月日,以及各類方法。只用在主類中建立一個該類對象就能夠經過調用該對象本身的方法讓它本身「說」說出下一天的日期。設計
一元多項式求導首先要使用正則表達式進行匹配,查看輸入是否正確,而後進行求導。此代碼只能判斷輸入是否正確及對某種特定狀況求導,50滿分只得了20分,仍是靠投機取巧得的,感受此題太複雜了,求導過程當中不一樣的狀況太對,不知道如何下手。code
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; class Equation{ String string; String totalRegex = "([-+]?([1-9]+[0-9]*(\\s)*(\\*)?)?x?(\\^[+-]?[1-9]+[0-9]*)?(\\s)*)+"; String sbuRegex = "[-+]?[1-9]+[0-9]*"; Equation(String input){ string = input; } boolean judge(){ if(string.matches("[-+]?[1-9]?[\\d]*")){ return string.matches("[-+]?[1-9]+[\\d]*"); } else return string.matches(totalRegex); } void derivation(String input){ Pattern pattern = Pattern.compile(sbuRegex); Matcher matcher=pattern.matcher(input); int s = 0,j = 0; if(input.matches("[-+]?[1-9]?[\\d]*")) { System.out.println("0"); } else { while(matcher.find()) { String tmp=matcher.group(); s++; int i = Integer.parseInt(tmp); if(s%2 == 0){ if(i>0) System.out.print("+"+i*j+"*x^"+(i-1)); else System.out.print(i*j+"*x^"+(i-1)); } j = i; int end=matcher.end(); if(end==input.length()) break; } // System.out.println("1"); } } } public class Main{ public static void main(String[] args){ Scanner p = new Scanner(System.in); String input = p.nextLine(); Equation a = new Equation(input); if(a.judge()) { a.derivation(input); } else System.out.println("Wrong Format"); } }
在判斷三角形類型的那題中一開始直接 a*a == b*b+c*c ,而後發現一直過不了。而後偶然想起判斷兩個浮點數是否相等儘可能不要用 == 來判斷,要用這兩個數的差值來判斷,因而就改爲了 Math.abs(a*a - b*b - c*c) < 0.01 這樣就對了。由於人算時根號數平方能夠獲得一我的整數但這題只能用小數來代替帶根號的數,平方後會產生偏差,因此只能用兩個數的插值來判斷。orm
例如對象
public class Main{ public static void main(String[] args){ System.out.println(Math.abs(Math.pow(Math.sqrt(2.0), 2)-2) < 0.001); //true } } public class Main{ public static void main(String[] args){ System.out.println(Math.pow(Math.sqrt(2.0), 2) == 2); //flase } }
在求前N天的題目中,一開始忽略了N爲負數的狀況。兩個算日期的題也沒什麼坑,就是要作好判斷理清關係。
題集三那題只寫出了能判斷多項式格式是否正確,和某種特殊狀況(指數不爲1,沒有常數項)下的求導。一開始寫的正則表達式忽略了項之間能夠存在空格的狀況,解決方法很簡單,在能夠已存在空格的地方加上(\\s)*就行。
沒有對代碼太多的改,基本就是寫完了交了對了就無論了,之後要多加註意代碼的改進問題。
初步學習感覺到了Java的面向對象含義,瞭解了正則表達式。做業涉及內容超過課上教學,爲了寫做業自學完了,上課就不知道幹什麼。