【案例】表單的全選取消全選功能
題目集1~3總結性blog
1.前言
前三次題目集主要涉及到java的程式設計基礎知識和有關類的簡單知識。題目量不大,但部分題難度較高。
2.設計與分析
題目集1,2都是考察基本程式設計知識和邏輯。題目集3則致力於是我們理解不同的耦合。
(1)題目集2 :7-1
本題集中於對字串的應用,考察對字串內容的判斷,題目複雜程度適中,其中的校驗位的考察具有一定的誤導作用。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if (!str.contains("0") || str.length() < 11) {
System.out.println("null data");
return;
}
int i, count = 1, p = 0, j = 0;
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') {
if (str.length() - i < 10) {
if (count == 0) {
System.out.println("null data");
return;
} else {
return;
}
}
if (str.charAt(i + 10) != '1') {
i += 10;
System.out.printf("%d:", count++);
System.out.println("validate error");
continue;
}
for (j = i + 1, p = 0; j < i + 9; j++) {
if (str.charAt(j) == '1') {
p++;
}
}
if (str.charAt(i + 9) -48== p % 2) {
System.out.printf("%d:", count++);
System.out.println("parity check error");
i += 10;
continue;
}
System.out.printf("%d:", count++);
for (j = i + 1; j < i + 9; j++) {
System.out.print(str.charAt(j));
}
System.out.println();
i = i + 10;
}
}
}
}
(2)題目集3:7-1
題目集三是對舊題目的重新考察,強化同學們對類的理解。
題目中有兩個類,main類是做輸入資料和輸出資料之用,QuadraticEquation類是作求根運算的用處,題目中getter的設定逐漸引導程式設計轉向面向物件程式設計。本題難度總體不高。
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double a = Double.parseDouble(input.next());
double b = Double.parseDouble(input.next());
double c = Double.parseDouble(input.next());
if(a == 0){
System.out.println("Wrong Format");
System.exit(0);
}
//create a QuadraticEquation object
QuadraticEquation equation = new QuadraticEquation(a, b, c);
//get value of b * b - 4 * a * c
double discriminant = equation.getDiscriminant();
System.out.println("a=" + equation.getA() +
",b=" + equation.getB() +
",c=" + equation.getC()+":");
if (discriminant < 0) {
System.out.println("The equation has no roots.");
}
else if (discriminant == 0)
{
System.out.println("The root is " +
String.format("%.2f", equation.getRoot1()));
}
else // (discriminant >= 0)
{
System.out.println("The roots are " +
String.format("%.2f", equation.getRoot1())
+ " and " + String.format("%.2f", equation.getRoot2()));
}
}
}
class QuadraticEquation{
//your code
double a;
double b;
double c;
double discriminant;
public QuadraticEquation(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public double getDiscriminant(){
return b*b-4*a*c;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getRoot1(){
return (-b+Math.sqrt(b*b-4*a*c))/2/a;
}
public double getRoot2(){
return (-b-Math.sqrt(b*b-4*a*c))/2/a;
}
}
(3)題目集3:7-2
本題在於增進同學們對耦合的理解,在程式碼中的兩個類,main類用於輸入和輸出,DateUtil類用於提供日期計算有關的方法。
題目的難點集中於對前幾天和後幾天的計算上,需要分情況討論,注意平閏年,題目不涉及演算法,仍然屬於對基本知識的考查。
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);
}
}
}
class DateUtil {
private int year;
private int month;
private int day;
public DateUtil(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
public boolean checkInputValidity() {
if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31) {
return false;
}
if (month == 2) {
if (isLeapYear(year)) {
if (day > 29) {
return false;
}
} else {
if (day > 28) {
return false;
}
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 28) {
return false;
}
}
return true;
}
public DateUtil getNextNDays(int n) {
int sum = 0;
int y = year;
int m = month;
int d = day;
int k;
if (n >= 0) {
while (true) {
if(sum>1000000){
sum=sum-1000000;
n=n-1000000;
}
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
k = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
k = 30;
} else {
if (isLeapYear(y)) {
k = 29;
} else {
k = 28;
}
}
if (k - d >= n) {
d = d + (n-sum);
sum = sum + (n-sum);
} else {
if (sum + (k-d) > n) {
d = n - sum;
sum = sum + (n - sum);
} else {
sum = sum+k-d;
d = 0;
if (m == 12) {
m = 1;
y++;
} else {
m++;
}
}
}
if (sum == n)
break;
}
} else if (n < 0) {
while (true) {
if(sum<-1000000){
sum=sum+1000000;
n=n+1000000;
}
if (m == 1) {
m = 12;
} else {
m--;
}
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
k = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
k = 30;
} else {
if (isLeapYear(y)) {
k = 29;
} else {
k = 28;
}
}
if (sum - d < n) {
d = d + (n - sum);
sum = sum + (n - sum);
if(m==12){
m=1;
}
else{
m++;
}
} else {
sum = sum - d;
d = k;
if (m == 12) {
y--;
}
}
if (sum == n) {
break;
}
}
}
DateUtil newone = new DateUtil(y, m, d);
return newone;
}
public DateUtil getPreviousNDays(int n) {
return getNextNDays(-n);
}
public String showDate() {
String a = String.format("%d-%d-%d", year, month, day);
return a;
}
public int getDaysofDates(DateUtil toDate) {
int sum=0;
int y = year;
int m = month;
int d = day;
int k;
while(true){
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
k = 31;
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
k = 30;
} else {
if (isLeapYear(y)) {
k = 29;
} else {
k = 28;
}
}
if(y<= toDate.getYear()){
if(y == toDate.getYear() && m == toDate.getMonth()){
sum=sum+ toDate.getDay()-d;
break;
}
else{
sum=sum+k-d;
d=0;
if(m==12){
m=1;
y++;
}
else{
m++;
}
}
}
}
return sum;
}
}
(4)題目集3:7-3
本題考察同上一題相同,但要求寫更多的類,通過相同答案不同的類的寫法,同上一題比較,本題day類呼叫了month類,month類呼叫了year類,環環相扣,而上一題這些是合併在一個類中的,這兩道題的有不同寫法。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 1){
int yearValue = sc.nextInt();
int monthValue = sc.nextInt();
int dayValue = sc.nextInt();
int n = sc.nextInt();
DateUtil date = new DateUtil(yearValue,monthValue,dayValue);
if(date.checkInputValidity()){
System.out.println(date.getNextDays(n).showDate());
}
else{
System.out.println("Wrong Format");
}
}
else if(choice == 2){
int yearValue = sc.nextInt();
int monthValue = sc.nextInt();
int dayValue = sc.nextInt();
int n = sc.nextInt();
DateUtil date = new DateUtil(yearValue,monthValue,dayValue);
if(date.checkInputValidity()){
System.out.println(date.getPreviousNDays(n).showDate());
}
else{
System.out.println("Wrong Format");
}
}
else if(choice == 3){
int yearValue = sc.nextInt();
int monthValue = sc.nextInt();
int dayValue = sc.nextInt();
DateUtil date = new DateUtil(yearValue,monthValue,dayValue);
if(!date.checkInputValidity()){
System.out.println("Wrong Format");
return;
}
yearValue = sc.nextInt();
monthValue = sc.nextInt();
dayValue = sc.nextInt();
DateUtil date1 = new DateUtil(yearValue,monthValue,dayValue);
if(!date1.checkInputValidity()){
System.out.println("Wrong Format");
return;
}
System.out.println(date.getDaysofDates(date1));
}
else {
System.out.println("Wrong Format");
}
}
}
class DateUtil{
Day day;
public DateUtil(){
}
public DateUtil(int d,int m, int y){
Day day1 = new Day(d,m,y);
setDay(day1);
}
public Day getDay() {
return day;
}
public void setDay(Day day){
this.day = day;
}
public boolean checkInputValidity(){
if(day.validate()){
return true;
}
else{
return false;
}
}
public boolean compareDates(DateUtil date){
if(day.getMonth().getYear().getValue()>date.getDay().getMonth().getYear().getValue())
{
return true;
}
else if(day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()){
if(day.getMonth().getValue() > date.getDay().getMonth().getValue()){
return true;
}
else if(day.getMonth().getValue() == date.getDay().getMonth().getValue()){
if(day.getValue() > date.getDay().getValue()) {
return true;
}
}
}
return false;
}
public boolean equalTwoDates(DateUtil date){
if(day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() &&
day.getMonth().getValue() == date.getDay().getMonth().getValue() &&
day.getValue() == date.getDay().getValue()){
return true;
}
return false;
}
public String showDate(){
return String.format("%d-%d-%d",day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
public DateUtil getNextDays(int n) {
int sum = 0;
while (true) {
if (sum == n) {
break;
}
day.dayIncrement();
sum++;
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
public DateUtil getPreviousNDays(int n){
int sum = 0;
while(true){
if(sum==n){
break;
}
day.dayReduction();
sum++;
}
return new DateUtil(day.getValue(),day.getMonth().getValue(),day.getMonth().getYear().getValue());
}
public int getDaysofDates(DateUtil date){
int sum = 0;
while(true){
if(equalTwoDates(date)){
break;
}
sum++;
// if(day.getMonth().getYear().getValue() <= date.getDay().getMonth().getYear().getValue()){
// if(day.getMonth().getValue() <= date.getDay().getMonth().getValue()){
// if(day.getValue() <= date.getDay().getValue()){
// day.dayIncrement();
// }
// else{
// day.dayReduction();
// }
// }
// else{
// day.dayReduction();
// }
// }
// else{
// day.dayReduction();
// }
if(!compareDates(date)){
day.dayIncrement();
}
else{
day.dayReduction();
}
}
return sum;
}
}
class Day{
private int value;
private Month month;
private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
public Day (){
}
public Day(int yearValue,int monthValue,int dayValue){
setValue(dayValue);
Month m = new Month(yearValue,monthValue);
setMonth(m);
}
public int getValue(){
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth(){
return month;
}
public void setMonth(Month month){
this.month = month;
}
public void resetMin(){
this.value = 1;
}
public void resetMax(){
if(month.getValue() == 2){
if(month.getYear().isLeapYear()){
this.value = 29;
}
else {
value = 28;
}
}
else{
this.value = mon_maxnum[month.getValue()-1];
}
}
public boolean validate(){
if(!month.validate()){
return false;
}
if(month.getValue() == 2){
if(month.getYear().isLeapYear()){
if(value>=1 && value<=29){
return true;
}
}
else{
if(value>=1 && value <=28){
return true;
}
}
}
else{
if (value >= 1 && value <= mon_maxnum[month.getValue()-1]) {
return true;
}
}
return false;
}
public void dayIncrement(){
if(month.getValue()==2){
if(month.getYear().isLeapYear()){
if(value ==29){
resetMin();
month.monthIncrement();
}
else{
value++;
}
}
else {
if (value == 28) {
month.monthIncrement();
resetMin();
} else {
value++;
}
}
}
else{
if(value == mon_maxnum[month.getValue()-1]){
month.monthIncrement();
resetMin();
}
else{
value++;
}
}
}
public void dayReduction(){
if(value == 1){
month.monthReduction();
resetMax();
}
else{
value--;
}
}
}
class Month{
private int value;
private Year year;
public Month(){
}
public Month(int yearValue,int monthValue){
setValue(monthValue);
Year y = new Year(yearValue);
setYear(y);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year getYear(){
return year;
}
public void setYear(Year year){
this.year = year;
}
public void resetMin(){
value = 1;
}
public void resetMax(){
value = 12;
}
public boolean validate(){
if(!year.validate()){
return false;
}
if(value >= 1 && value <= 12){
return true;
}
return false;
}
public void monthIncrement(){
if(value==12){
value = 1;
year.yearIncrement();
}
else{
value++;
}
}
public void monthReduction(){
if(value==1){
value = 12;
year.yearReduction();
}
else{
value--;
}
}
}
class Year{
private int value;
public Year (){
}
public Year (int value){
setValue(value);
}
public int getValue(){
return value;
}
public void setValue(int value){
this.value = value;
}
public boolean isLeapYear(){
if(((value % 4) == 0 && (value % 100!=0)) || value % 400 == 0){
return true;
}
return false;
}
public boolean validate(){
if(value >= 1900 && value <= 2050){
return true;
}
return false;
}
public void yearIncrement(){
value++;
}
public void yearReduction(){
value--;
}
}
(3)踩坑心得
題目集中部分題目受制於浮點數精度的原因,與原程式稍有寫法不同就會出現無法通過的測試點(主要體現在需要使用單精度浮點數的場景),前三次題目集主要的難點便是以上的問題,例如題目集1中的第二題weight/=0.45359237;就是數字本身除以一個數這麼簡單的式子,因為題目硬性要求單精度浮點數輸出,而0.45359237是雙精度浮點數,寫法與上面有分毫差別輸出後就不容易通過測試點,而導致將時間浪費在無意義的其他輸出情況的判斷上。
(4)改進建議
希望自己以後的編碼風格可以更加規範,
(5)總結
前三次題目集主要還是前八章的基礎知識,有的題目也涉及了多個類,從這些題目中獲得了簡單的練習。以上所有的提到的知識點都需要進一步學習和研究,我掌握的都不太好。包括條件判斷,迴圈,涉及類的簡單知識等。希望以後單精度浮點數的場景可以標明,雙精度浮點數的表示範圍更大,而且精度問題導致的無法通過實在沒有意義。