首页 > 其他分享 >总结三次题目集的知识点、题量、难度等情况

总结三次题目集的知识点、题量、难度等情况

时间:2023-03-30 22:48:53浏览次数:37  
标签:知识点 题目 题量 int System month public year day

前言:题目集1是Java入门,大至涵盖了Java的几种基本语言结构,如循环,选择,数组。与c语言大致相似,上手较容易。但是题目难度不低,7-10GPS数据处理,不仅题目长难以理解,对有难度的字符串要求掌握熟练,输出要考虑的情况也很多,题目也达12题之多。其他题目在c语言里写过近似一模一样的题,写这个题目集可以感受到两种语言的语法的不同。

题目集2依旧是熟悉Java的几种基本语言结构,选择结构占比很大,同时要求我们掌握Java特有函数数据类型比如boolean;与题目集1一样只用一个主方法就可以解决问题,题量比第一次还要少,题目也更简单一些,也许是我们第一次作业的情况非常糟糕,老师对我们手下留情。

题目集3开始需要为对象定义类,感受到Java面对对象的特性。题量很少,只有四题。最后一道题目7-4是前一道题目7-3进行了改编,要求增多,难度忽然飞跃,但因为有前一道题目打基础,不至于陷入一开始思路也不清晰的困境。

题目集3的7-3

7-3 定义日期类

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

7-3告知了类图:

 

设计和分析:类图指导代码的写作,从类图可知Main类与Date类有依赖关系,在Main类需要写一个返回为空的主方法(main(String args[]) ),Date类包含三个私有属性,全为int型变量的year,month,day,和一个公共int[]变量的属性mon_maxnum;包含两个类的构造方法,一个无参,一个有参需要传入year,month,day数据;包含year,month,day的setter和getter方法,setter方法全部返回为空,getter方法全部返回为int型;包含返回boolean数据类型,传入year数据的判断年份是否是闰年的isLeap Year方法;包含返回boolean数据类型,不用传入的判断输入日期是否合法的checkInputValidity方法;包含返回为空的求下一天的getNextDate的方法。

在写判断是否为闰年方法isLeap Year()时想清楚闰年的判断标准:被4整除同时不能被100整除或者被400整除。

在写判断输入日期是否合法方法checkInputValidity()时看清楚对年份的要求,1,3,5,7,8,10,12月最多31天,2,4,6,9,11月最多30天,特殊情况闰年二月29天。

在写getNextDate()时也考虑几种特殊的情况,比如一个月的最后一天的下一天是下一个月的第一天,闰年二月是29天,12月31日的下一天是下一年的1月1日。

用SourceMonitor生成了一个分析报告,如下:

 

从SourceMonitor报表中可以看出Avg  Complexity平均复杂度,Avg Depth平均深度和Max Complexity最大复杂度的数值都很低,不过代码也确实不复杂,它们都没有超过绿色区域,应该没什么问题。

最终我的代码如下:

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5         Scanner input = new Scanner(System.in);
 6         int year = input.nextInt();
 7         int month = input.nextInt();
 8         int day = input.nextInt();
 9         Date date1 = new Date(year,month,day);
10         
11         if(date1.checkInputValidity()){
12             date1.getNextDate();
13         }else{
14             System.out.println("Date Format is Wrong");
15         }
16     }
17 }
18 class Date{
19     private int year;
20     private int month;
21     private int day;
22     public int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
23     
24     public Date(){
25     }
26     public Date(int year,int month,int day){
27         this.year = year;
28         this.month = month;
29         this.day = day;
30     }
31     public int getYear(){
32         return year;
33     }
34     public void setYear(int year){
35         this.year = year;
36     }
37     public int getMonth(){
38         return month;
39     }
40     public void setMonth(int month){
41         this.month = month;
42     }
43     public int getDay(){
44         return day;
45     }
46     public void setDay(int day){
47         this.day = day;
48     }
49     public boolean isLeapYear(int year){
50         return((year%4==0&&year%100!=0)||(year%400==0));
51     }
52     public boolean checkInputValidity(){
53         if(this.year>=1900&&this.year<=2000&&this.month>=1&&this.month<=12){
54             if(isLeapYear(this.year)){
55             mon_maxnum[2] += 1;
56         }
57             if(this.day>mon_maxnum[this.month]||this.day<1){
58                 return false;
59             }else return true;
60         }else return false;
61     }
62     public void getNextDate(){
63         if(this.day==mon_maxnum[this.month]){
64             if(this.month!=12){
65                 this.month += 1;
66             }else{
67                 this.year += 1;
68                 this.month = 1;
69             }
70             this.day = 1;
71         }else{
72             this.day += 1;
73         }
74         System.out.println("Next day is:"+this.year+"-"+this.month+"-"+this.day);
75     }
76 }

 踩坑心得:第一次在类里面使用数组,犹豫好久,不知道是否需要用new操作符创建数组,上网搜索无果,询问大佬,告诉我直接赋值,我怀着忐忑的心将老师给的类图中的对mon_maxnum数组定义删去new int[],改成int[] mon_maxnum = {...}形式;在判断是否为闰年isLeap Year方法里使用boolean数据类型时,书上写结果返回true或者false,我不太懂是我只能先得出1或者0这样电脑可以识别的逻辑用词后在return语句里return 1或return 0,它把1或0转译为我看得懂的true或false,还是在return语句里我就可以写return true或return false。为防止出错,我选择最稳妥的第一种形式,在return语句里直接进行判断,当然,后来我发现了一个例题,boolean的用法就是如我第二个想法用的,可惜当时我着急写完题目,没有过多思考就下手了,万幸后面还是让我知道了正确用法。

改进建议:我在PTA上提交的代码没有完全按照老师要求写,在checkInputValidity()和getNextDate()方法里传了参数year,month,day。但是要求是传参为空,我按着c语言写函数的老想法做,尽管我的测试点都过了,可是没按照要求应该是0分。现在在博客上传的代码是我把PTA上的代码改动使用了this引用后的。

设计和分析:

题目集3的7-4

7-4 日期类设计

参考题目3和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:

public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值
 

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

程序主方法如下:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         int year = 0;
 7         int month = 0;
 8         int day = 0;
 9 
10         int choice = input.nextInt();
11 
12         if (choice == 1) { // test getNextNDays method
13             int m = 0;
14             year = Integer.parseInt(input.next());
15             month = Integer.parseInt(input.next());
16             day = Integer.parseInt(input.next());
17 
18             DateUtil date = new DateUtil(year, month, day);
19 
20             if (!date.checkInputValidity()) {
21                 System.out.println("Wrong Format");
22                 System.exit(0);
23             }
24 
25             m = input.nextInt();
26 
27             if (m < 0) {
28                 System.out.println("Wrong Format");
29                 System.exit(0);
30             }
31 
32             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
33             System.out.println(date.getNextNDays(m).showDate());
34         } else if (choice == 2) { // test getPreviousNDays method
35             int n = 0;
36             year = Integer.parseInt(input.next());
37             month = Integer.parseInt(input.next());
38             day = Integer.parseInt(input.next());
39 
40             DateUtil date = new DateUtil(year, month, day);
41 
42             if (!date.checkInputValidity()) {
43                 System.out.println("Wrong Format");
44                 System.exit(0);
45             }
46 
47             n = input.nextInt();
48 
49             if (n < 0) {
50                 System.out.println("Wrong Format");
51                 System.exit(0);
52             }
53 
54             System.out.print(
55                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
56             System.out.println(date.getPreviousNDays(n).showDate());
57         } else if (choice == 3) {    //test getDaysofDates method
58             year = Integer.parseInt(input.next());
59             month = Integer.parseInt(input.next());
60             day = Integer.parseInt(input.next());
61 
62             int anotherYear = Integer.parseInt(input.next());
63             int anotherMonth = Integer.parseInt(input.next());
64             int anotherDay = Integer.parseInt(input.next());
65 
66             DateUtil fromDate = new DateUtil(year, month, day);
67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
68 
69             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
70                 System.out.println("The days between " + fromDate.showDate() + 
71                         " and " + toDate.showDate() + " are:"
72                         + fromDate.getDaysofDates(toDate));
73             } else {
74                 System.out.println("Wrong Format");
75                 System.exit(0);
76             }
77         }
78         else{
79             System.out.println("Wrong Format");
80             System.exit(0);
81         }        
82     }
83 }

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下:
    Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year1-month1-day1 next n days is:year2-month2-day2
    
  • 当第一个数字为2且输入均有效,输出格式如下:
    year1-month1-day1 previous n days is:year2-month2-day2
    
  • 当第一个数字为3且输入均有效,输出格式如下:
    The days between year1-month1-day1 and year2-month2-day2 are:值

设计和分析:这一题是前一题的改进版,类的数目没有增加的必要,还是两个,但是方法的复杂度和数目有所改变,求下n天和前n天跨年和跨二月的时候可以有好几次,要循环减去天数判断,难度比下一天要大多了,循环判断数目,也就是圈复杂度比前一题多很多。

求前下n天可以采用总天数相减方法,主要步骤为:先算初始日期在该年的第几天,减去或者加上要求的天数得到数n,以该年的1月1日为开始n一年一年相减或者相加,直到n<那年的总天数,再计算n是那年的第几月第几天。

求两个日期相差的天数,主要步骤:先算两个日期在其年的第几天,相差年份的总天数,用相差年份的总天数-在前的日期在其年的第几天+在后的日期在其年的第几天。其中会用到判断两个日期是否相等的方法compareDates(date)。

类图如下:

这一题用SourceMonitor生成的分析报告如下:

从PowerDesigner的生成报表中可以看出Max Complexity超过绿色区域,Avg Complexity,Avg Depth和Avg Stmts/Method也非常接近绿色区域边缘,if-else循环用的太多,也就是代码还需要改进。

我的代码:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11 
 12         if (choice == 1) { // test getNextNDays method
 13             int m = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24 
 25             m = input.nextInt();
 26 
 27             if (m < 0) {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31 
 32             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 33             System.out.println(date.getNextNDays(m).showDate());
 34         } else if (choice == 2) { // test getPreviousNDays method
 35             int n = 0;
 36             year = Integer.parseInt(input.next());
 37             month = Integer.parseInt(input.next());
 38             day = Integer.parseInt(input.next());
 39 
 40             DateUtil date = new DateUtil(year, month, day);
 41 
 42             if (!date.checkInputValidity()) {
 43                 System.out.println("Wrong Format");
 44                 System.exit(0);
 45             }
 46 
 47             n = input.nextInt();
 48 
 49             if (n < 0) {
 50                 System.out.println("Wrong Format");
 51                 System.exit(0);
 52             }
 53 
 54             System.out.print(
 55                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 56             System.out.println(date.getPreviousNDays(n).showDate());
 57         } else if (choice == 3) {    //test getDaysofDates method
 58             year = Integer.parseInt(input.next());
 59             month = Integer.parseInt(input.next());
 60             day = Integer.parseInt(input.next());
 61 
 62             int anotherYear = Integer.parseInt(input.next());
 63             int anotherMonth = Integer.parseInt(input.next());
 64             int anotherDay = Integer.parseInt(input.next());
 65 
 66             DateUtil fromDate = new DateUtil(year, month, day);
 67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 68 
 69             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 70                 System.out.println("The days between " + fromDate.showDate() +
 71                         " and " + toDate.showDate() + " are:"
 72                         + fromDate.getDaysofDates(toDate));
 73             } else {
 74                 System.out.println("Wrong Format");
 75                 System.exit(0);
 76             }
 77         }
 78         else{
 79             System.out.println("Wrong Format");
 80             System.exit(0);
 81         }
 82     }
 83 }
 84 
 85 class DateUtil{
 86     private int year = 0;
 87     private int month = 0;
 88     private int day = 0;
 89     public int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 90 
 91     public DateUtil(){
 92     }
 93     public DateUtil(int year,int month,int day){
 94         this.year = year;
 95         this.month = month;
 96         this.day = day;
 97     }
 98     public void setYear(int year){
 99         this.year = year;
100     }
101     public int getYear(){
102         return year;
103     }
104     public void setMonth(int month){
105         this.month = month;
106     }
107     public int getMonth(){
108         return month;
109     }
110     public void setDay(int day){
111         this.day = day;
112     }
113     public int getDay(){
114         return day;
115     }
116     public boolean isLeapYear(int year){
117         return((year%4==0&&year%100!=0)||(year%400==0));
118     }
119     public boolean checkInputValidity(){
120         if(this.year>=1820&&this.year<=2020&&this.month>=1&&this.month<=12){
121             if(isLeapYear(this.year)){
122                 mon_maxnum[2] += 1;
123             }
124             if(this.day>mon_maxnum[this.month]||this.day<1){
125                 return false;
126             }else return true;
127         }else return false;
128     }
129     public DateUtil getNextNDays(int n){
130         //计算当前日期在一年里的第几天
131         int sum = 0,daysum = 0;
132         for(int i = 1;i<this.month;i++){
133             if(isLeapYear(this.year)){
134                 mon_maxnum[2] = 29;
135             }
136             sum += mon_maxnum[i];
137         }
138         sum += this.day;
139         //计算下个日期的年份
140         sum += n;
141         if(isLeapYear(this.year)){
142             daysum = 366;
143         }else{
144             daysum = 365;
145         }
146         while (sum-daysum>0){//daysum==0
147             if(isLeapYear(this.year)){
148                 daysum = 366;
149             }else{
150                 daysum = 365;
151             }
152             sum = sum-daysum;
153             this.year ++;
154             this.month = 1;
155         }
156         if(isLeapYear(this.year)){
157             mon_maxnum[2] = 29;
158         }else{
159             mon_maxnum[2] = 28;
160         }
161         //sum是月与日
162         if(sum>mon_maxnum[1]) {//计算月日
163             for (int i = 1; sum > mon_maxnum[i]; i++) {
164                 sum = sum - mon_maxnum[i];
165                 this.month = i;
166             }this.month ++;
167         }
168         day = sum;
169         return this;
170     }//取得year-month-day的下n天日期
171     public DateUtil getPreviousNDays(int n){
172         //计算当前日期在一年里的第几天
173         int sum = 0,daysum = 0;
174         for(int i = 1;i<this.month;i++){
175             if(isLeapYear(this.year)){
176                 mon_maxnum[2] = 29;
177             }
178             sum += mon_maxnum[i];
179         }
180         sum += this.day;
181         //计算下一个日期
182         sum -= n;
183         if(sum<0){//跨年了
184             if(isLeapYear(this.year-1)){
185                 daysum = 366;
186             }else{
187                 daysum = 365;
188             }//计算年份
189             while (sum+daysum<0){
190                 if(isLeapYear(this.year-1)){
191                     daysum = 366;
192                 }else{
193                     daysum = 365;
194                 }
195                 sum = sum+daysum;
196                 this.year --;
197             }//计算正数的月份
198             this.year = this.year-1;
199             if(isLeapYear(this.year)){
200                 daysum = 366;
201             }else{
202                 daysum = 365;
203             }
204             sum = daysum+sum;
205         }else {//未跨年
206             if (isLeapYear(this.year)) {
207                 daysum = 366;
208             } else {
209                 daysum = 365;
210             }
211             sum = daysum-n;
212         }
213         if(isLeapYear(this.year)){
214             mon_maxnum[2] = 29;
215         }else{
216             mon_maxnum[2] = 28;
217         }
218         //计算月份
219         if(sum>mon_maxnum[1]) {//计算月日
220             for (int i = 1; sum > mon_maxnum[i]; i++) {
221                 sum = sum - mon_maxnum[i];
222                 this.month = i;
223             }this.month ++;
224         }
225         this.day = sum;
226         return this;
227     }//取得year-month-day的前n天日期
228     public boolean compareDates(DateUtil date){
229         if(this.year-date.year>0){
230             return false;
231         }else if(this.year-date.year<0){
232             return true;
233         }else if(this.year-date.year==0){
234             if(this.month-date.month>0){
235                 return false;
236             }else if(this.month-date.month<0){
237                 return true;
238             }else if(this.month-date.month==0){
239                 if(this.day-date.day>0){
240                     return false;
241                 }else if(this.day-date.day<0){
242                     return true;
243                 }
244             }
245         }
246         return true;
247     }//比较当前日期与date的大小(先后)
248     public boolean equalTwoDates(DateUtil date){
249         if(this.year==date.year&&this.month==date.month&&this.day==date.day){
250             return true;
251         }else return false;
252     }//判断两个日期是否相等
253     public int getDaysofDates(DateUtil date){
254         int n = 0;
255         if(equalTwoDates(date)){
256             return 0;
257         }else {
258             int k = 0;
259             //保证第一个日期始终在前
260             if (compareDates(date) == false) {
261                 k = this.year;
262                 this.year = date.year;
263                 date.year = k;
264                 k = this.month;
265                 this.month = date.month;
266                 date.month = k;
267                 k = this.day;
268                 this.day = date.day;
269                 date.day = k;
270             }
271             //计算两个日期相差天数
272             //计算两个日期在一年里的位置
273             int sum1 = 0, daysum = 0, sum2 = 0;
274             for (int i = 1; i < this.month; i++) {
275                 if (isLeapYear(this.year)) {
276                     mon_maxnum[2] = 29;
277                 }
278                 else mon_maxnum[2] = 28;
279                 sum1 += mon_maxnum[i];
280             }
281             sum1 += this.day;
282 
283             for (int i = 1; i < date.month; i++) {
284                 if (isLeapYear(date.year)) {
285                     mon_maxnum[2] = 29;
286                 }
287                 else mon_maxnum[2] = 28;
288                 sum2 += mon_maxnum[i];
289             }
290             sum2 += date.day;
291             //n = sum2 - sum1;//计算一年里两个日期的差
292             //计算两个日期相差的年份
293             for (; this.year <= date.year;) {
294                 n += daysum;
295                 if (isLeapYear(this.year)) {
296                     daysum = 366;
297                 } else {
298                     daysum = 365;
299                 }
300                 this.year ++;
301             }
302             n = n - sum1 +sum2;
303         }
304         return n;
305     }//求当前日期与date之间相差的天数
306     public String showDate(){
307         return (this.year+"-"+this.month+"-"+this.day);
308     }//以“year-month-day”格式返回日期值
309 }

踩坑心得:这一题我大改了一次,一开始我想的是一月一月去减或者加去求前下n天,但是循环相减加不仅要考虑月份的改动还要考虑年份的改动,对我来说实在过于复杂,绕得我头晕;后面我听取建议,改成仅考虑跨年的算法(如我的设计所说),这样思考的情况大大降低了难度,有一些逻辑上的小问题也可以通过debug一步一步查看改动。有的时候有难度的题可以变得简单,是因为思考方式不同,这题我深有感触。

题目要求需要看清楚,这题好像是前一题仅改变了问法,将求下一天变成下n天,还加上求前n天,实际上对年份的要求也改变了,PTA上我在这里提交了好几次错误答案,拉低了通过率,还差点怀疑自我。

情况考虑不全面影响整体,我有几个测试点总是不过是因为我算一年里二月最大天数时判断是闰年把28天改成29天后求下一年没有把29天改回28天。

改进建议:在for循环括号里我会写这样的赋值,i = this.month,这样做会造成一个严重的后果,就是跳出循环后this.month的值不会发生改变,但是我需要改变后的月值,这时我在循环体里加上一条语句this.month = i。有时候它完美解决了问题,可是有时候这样改不行,大佬也说最好不要把要改变的值赋给一个变值,所以直接for(;this.month...;)就好了。在我提交的代码里有些循环条件我没有改,这些地方还需要改进。

题目集3的7-3和7-4是老师要求要分析的题目,除此之外,我还想分析一下题目集1的7-7:

7-7 有重复的数据

在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。

输入格式:

你的程序首先会读到一个正整数n,n∈[1,100000],然后是n个整数。

输出格式:

如果这些整数中存在重复的,就输出:

YES
 

否则,就输出:

   NO

设计和分析:这道题目其实就是考察了字符串的基础应用,思路是把字符串变成字符数组,再对字符数组的每一项进行查重。为了简便,也是因为数据都是数字,先对数组进行一个排序,小一点的数放在前面,大数放在后面,再每一项都与后一项比较,发现有一样的就输出YES,跳出循环;如果全部比对后没有发现相同的数,也就是在循环结束后输出NO。

类图:

7-7就是三个步骤,非常简单,没什么好分析的,类图就只包含主类。

SourceMonitor的生成报表:

从PowerDesigner的生成报表中可以看出明明只是一个很简单的主类,但是复杂度和深度不管是平均还是最大都超出绿色范围很多,这是因为用了Java自带的排序方法java.util.Arrays.sort(),用的原因在下面的踩坑心得。

我的代码:

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5        Scanner input = new Scanner(System.in);
 6           
 7 
 8          int n = input.nextInt();
 9          int flag=0;
10 
11         if(n>=1){
12         int[] mylist = new int[n];
13         for(int i = 0;i<n;i++){
14             mylist[i] = input.nextInt();
15         }
16         
17         java.util.Arrays.sort(mylist);
18         
19         
20         for(int i = 0;i<n;i++){
21             if(i!=0){
22             if(mylist[i]==mylist[i-1]){
23                 flag=1;
24             }
25             }
26         }
27             if(flag==1){
28                 System.out.print("YES");
29             }
30             if(flag==0){
31                 System.out.print("NO");
32             }
33        
34         }
35     }
36 }

踩坑心得:这本是一个非常简单就可以写出的主类,但是在老师加了一个代码限制的前提后就很难写出。一开始我连先进行排序的想法都没有,用了一个嵌套循环,在固定一个数组元素后让它与后面的数组元素进行比较,比较完后再换下一个数组元素固定。出现的一行红字警告给了我当头一棒,告诉我事情并不简单。我只好上网搜索减少查重时间的方法,就找到了先排序的说法,可是我自己写的排序方法还是不够快,我以为这道题就这样了,写不出来了。后面有大佬找到了解决方法,那就是利用Java自带的排序方法,改后我果然通过了。学c语言的时候绝没有这种自带简便方法,在这里我体会到Java简便的说法绝对令人信服。

为了表现出这是三个题目集的总结报告,我再选了题目集2的7-8

7-8 判断三角形类型

输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:

在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[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”。

类图:

因为这里也还用不着定义类,结果类图就和上面一道题一样了。

SourceMonitor的分析报表:

这一题的报表情况是最糟糕的,可能是因为这道题就是用if-else组成的,造成代码冗余。

踩坑心得:如果不是老师讲的话,我根本不知道原来直角三角形的判定是像求Π一样,看近似值,所以a*a+b*b==c*c不是判定标准,而只要a*a+b*b-c*c小于一个很小的数就可以了。

改进建议(总):写代码需要加上必要的注释,增加代码的可读性,方便纠错,也方便不乱思路;代码的格式要求得要严格按照阿里巴巴Java开发手册终极版来,现在改正早比以后改正轻松。

总结:要善于利用Java已有的方便的方法,比如排序方法,比如字符串的各种利用方法;一些比较特殊与认知不符合的题,比如最后一题,应该要和同学多沟通,这样才不至于闭门造车,消息阻塞;写难题的时候先找到简便方法很重要,不然会造成浪费时间和精力的情况,不过这种类似直觉的感觉需要多写题目,拥有经验才能培养,现新手阶段可以通过先设计分析排除一些根本不可行的方法;要善于利用debug,这是找到自己的逻辑错误的最好方法;Java基础知识老师不教,自主学习的话不管是上网课还是看书都最好还是连续式学习,点式学习知识面容易不全。

标签:知识点,题目,题量,int,System,month,public,year,day
From: https://www.cnblogs.com/wuyuxuansitong/p/17256088.html

相关文章

  • 小小知识点-tag
    1、trunk是带着tag标识转发的,除了默认vlan可以不带tag标识2、access转发出去不带tag标识SW1:<sw1>displaycurrent-configuration #sysnamesw1#vlanbatch1020#clusterenablentdpenablendpenable#dropillegal-macalarm#diffservdomaindefault#drop-profil......
  • LabVIEW | 知识点:移位寄存器
    移位寄存器循环结构里(while、for)常常用到移位寄存器,作为暂存数据的一种很有效的方式(移位寄存器可以存任何类型的数据)。如下图,上面循环对移位寄存器进行过初始化,下面的循环未......
  • 【计算机网络】物理层与数据链路层知识点
    引自CSDN博主「原来如此呀」的原创文章,原文链接计算机网络的正式定义:利用通信线路和通信设备,将地理位置不同的、功能独立的多台计算机互联起来,以功能完善的网络软件来实现......
  • 2021 年蓝桥杯第一次省赛题目全解答
    做题链接:A组B组C组填空题卡片直接模拟。展开代码#include<bits/stdc++.h>usingll=longlong;intmain(){std::cin.tie(nullptr)->sync_with_stdio(f......
  • Vcenter6.7相关知识点
    什么操作是只有vcenter才能做的: 1:VM模板   2:RBAC基于角色的访问控制  3:更细的颗粒度的限制  4:Vmotion    5:动态资源分配   6:HA   7:FT ......
  • C语言之PTA刷题(基础编程题目集_函数题)
    本题要求实现一个函数,对给定的正整数N,打印从1到N的全部正整数。#include<stdio.h>voidPrintN(intN);intmain(){intN;scanf("%d",&N);Pr......
  • 题目集1~3的总结性blog
    一.前言学期伊始,面向对象的程序设计课程的老师就利用PTA平台陆续发布了三次训练集。这三次训练集所涉及知识点与课上所学知识点有关,具体知识点如下:训练集01:此训练集所......
  • 题目(一)
    这道超级简单的题目没有任何输入。你只需要把这句很重要的话—— ILoveGPLT ——竖着输出就可以了。所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字......
  • oop题目集01-03小结
    oop训练集一*计算年利率*身体质量指数测算*九九乘法表(双循环)*快递运费*去掉重复字符*统计一个子串在整串中出现的次数*有重复数据*从一个字符串中移除包含......
  • PTA题目总结
    (1)前言:第一次题目集主要考察JAVA的一些语法知识,比如,控制台的输入,输出时保留两位小数,数组的使用,第十题有点难度,当时没写出来,现在想想 也还好,就是读懂题目有点费劲,第一次题......