第三次博客作业
第三次博客作业
一、 前言
又经过一段时间的学习,我在Java这门课程上又多学到了一些知识点。难度方面,或许是为了减轻一些我们在Java这门课程上的压力,最近三次的PTA作业简单了一些。基本上只要投入了足够的时间就可以完成,并且这三次作业也是迭代的。题量方面,这三次作业题量都很少,只有两三道题目。大概是因为网课上得太久了,做PTA的心情有点懈怠,好在我还是及时地完成了。这三次作业情况如下:
1. 题目集08
知识点主要有:容器,集合的应用,接口的实现,继承等。
2. 题目集09
知识点主要有:有序集合,迭代器以及题目集08中的知识点。
3. 题目集10
知识点主要有:正则表达式的应用,字符串的处理,多态的应用以及上一个题目集中的知识点。
二、 设计与分析
下面我将要对这三次的题目集进行详细分析,重点是对源码的分析,解释和完成题目后的心得。分析如下:(以下均是我个人的理解,可能与标准说法不同,请见谅)
1. 题目集08
7-1 电信计费系列1-座机计费
本题思路是先将输入的不同信息储存至不同的数组中,同时判断是否有重复的信息,再进行实例化:如果输入的是开户的信息,那么先存入一个字符串数组,判断无重复信息后,对于每条开户信息创建一个User对象,储存至一个只存入User对象的容器中;如果输入的是通讯记录,同上,存入不同的数组和容器中。之后,对每条通讯记录进行详细操作。在判断通讯记录的格式时,所用的正则表达式非常繁琐,主要是后面日期格式的判断不容易实现。
源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | import java.util.Date; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) throws ParseException { Scanner in = new Scanner(System.in); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" ); DecimalFormat d = new DecimalFormat( "0.0" ); ArrayList<User> Users = new ArrayList<User>(); String[] str = new String[ 100 ]; String[] users = new String[ 100 ]; String[] userRecords = new String[ 100 ]; int c= 0 ,k= 0 ,m= 0 ; for ( int i= 0 ;;i++) { str[i]=in.nextLine(); c++; if (str[i].contains( "end" )) break ; } for ( int i= 0 ;i<c- 1 ;i++) { if (str[i].matches( "u-0791[\\d]{7,8} 0" )) { boolean exist = false ; for ( int j= 0 ;j<k;j++) { if (users[j].equals(str[i])) exist = true ; } if (!exist) { users[k] = str[i]; k++; } } else if (str[i].matches( "t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ userRecords[m] = str[i]; m++; } } for ( int i= 0 ;i<k;i++) { Users.add( new User(users[i])); for ( int j= 0 ;j<m;j++) { String[] sub = userRecords[j].split( " " ); if (sub[ 0 ].substring( 2 ,sub[ 0 ].length()).matches(Users.get(i).getNumber())) { if (sub[ 1 ].substring( 0 , 4 ).matches( "0791" )) Users.get(i).userRecords.addCallInCity( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]))); else if (sub[ 1 ].substring( 0 , 4 ).matches( "(079\\d)|(0701)" )) Users.get(i).userRecords.addCallInProvince( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]))); else Users.get(i).userRecords.addCallInLand( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]))); } } } Collections.sort(Users); for (User e:Users) { System.out.println(e.getNumber()+ " " +d.format(e.calCost())+ " " +d.format(e.calBalance())); } } } class User implements Comparable<User>{ UserRecord userRecords = new UserRecord(); private double balance = 100 ; private ChargeMode chargeMode; private String number; public User(String str) { this .number = str.substring( 2 ,str.length()- 2 ); if (str.charAt(str.length()- 1 ) == '0' ) this .setChargeMode( new LandPhoneCharge()); } public double calBalance() { return balance-chargeMode.getMonthlyRent()-calCost(); } public double calCost() { double cost1 = 0 ,cost2 = 0 ,cost3 = 0 ; for ( int i= 0 ;i<userRecords.getCallInCity().size();i++) { long time = userRecords.getCallInCity().get(i).getEnd().getTime()-userRecords.getCallInCity().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost1 += 0.1 *(time/( 1000 * 60 )); else cost1 += 0.1 *(time/( 1000 * 60 )+ 1 ); } for ( int i= 0 ;i<userRecords.getCallInProvince().size();i++) { long time = userRecords.getCallInProvince().get(i).getEnd().getTime()-userRecords.getCallInProvince().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost2 += 0.3 *(time/( 1000 * 60 )); else cost2 += 0.3 *(time/( 1000 * 60 )+ 1 ); } for ( int i= 0 ;i<userRecords.getCallInLand().size();i++) { long time = userRecords.getCallInLand().get(i).getEnd().getTime()-userRecords.getCallInLand().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost3 += 0.6 *(time/( 1000 * 60 )); else cost3 += 0.6 *(time/( 1000 * 60 )+ 1 ); } return cost1+cost2+cost3; } public UserRecord getUserRecords() { return userRecords; } public void setUserRecords(UserRecord ur) { userRecords = ur; } public double getBalance() { return balance; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode cm) { chargeMode = cm; } public String getNumber() { return number; } public void setNumber(String n) { number = n; } @Override public int compareTo(User o) { return this .getNumber().compareTo(o.getNumber()); } } class UserRecord{ private ArrayList<CallRecord> callInCity = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callInProvince = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callInLand = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCity = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvince = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLand = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessage = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessage = new ArrayList<MessageRecord>(); public void addCallInCity(CallRecord cr) { callInCity.add(cr); } public void addCallInProvince(CallRecord cr) { callInProvince.add(cr); } public void addCallInLand(CallRecord cr) { callInLand.add(cr); } public void addAnswerInCity(CallRecord cr) { answerInCity.add(cr); } public void addAnswerInProvince(CallRecord cr) { answerInProvince.add(cr); } public void addAnswerInLand(CallRecord cr) { answerInLand.add(cr); } public void addSendMessage(MessageRecord mr) { sendMessage.add(mr); } public void addReceiveMessage(MessageRecord mr) { receiveMessage.add(mr); } public ArrayList<CallRecord> getCallInCity() { return callInCity; } public ArrayList<CallRecord> getCallInProvince() { return callInProvince; } public ArrayList<CallRecord> getCallInLand() { return callInLand; } public ArrayList<CallRecord> getAnswerInCity() { return answerInCity; } public ArrayList<CallRecord> getAnswerInProvince() { return answerInProvince; } public ArrayList<CallRecord> getAnswerInLand() { return answerInLand; } public ArrayList<MessageRecord> getSendMessage() { return sendMessage; } public ArrayList<MessageRecord> getReceiveMessage() { return receiveMessage; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules(){ return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> cr) { chargeRules = cr; } public double calCost(UserRecord ur) { return 0 ; } public double getMonthlyRent() { return 20 ; } } class LandPhoneCharge extends ChargeMode{ private double monthlyRent = 20 ; @Override public double calCost(UserRecord ur) { return 0 ; } @Override public double getMonthlyRent() { return monthlyRent; } } abstract class ChargeRule{ } abstract class CallChangeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> cr) { return 0 ; } } class CityRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } class ProvinceRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } class LandRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } abstract class CommunicationRecord{ String callNumber; String answerNumber; public String getCallNumber() { return callNumber; } public void setCallNumber(String cn) { callNumber = cn; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String an) { answerNumber = an; } } class CallRecord extends CommunicationRecord{ private Date start; private Date end; private String callCode; private String answerCode; public CallRecord(Date start,Date end) { this .start = start; this .end = end; } public Date getStart() { return start; } public void setStart(Date start) { this .start = start; } public Date getEnd() { return end; } public void setEnd(Date end) { this .end = end; } public String getCallCode() { return callCode; } public void setCallCode(String callCode) { this .callCode = callCode; } public String getAnswerCode() { return answerCode; } public void setAnswerCode(String answerCode) { this .answerCode = answerCode; } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } } |
2. 题目集09
7-1 电信计费系列2-手机+座机计费
本题是在题目集08第一题的基础上进行改进,增加了手机用户。在处理每条有手机用户的通讯记录时,同时需要判断手机用户所在的地区。总而言之,通讯记录的判断和处理更加详细,也更加繁琐了。需要创建手机用户的计费方式,之后在主函数中增加和修改亿点点就可以完成了。因为当时有点懈怠,做的时间比较晚,所以只得了97分,差3分,有点可惜。源码如下(部分):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | public class Main { public static void main(String[] args) throws ParseException { Scanner in = new Scanner(System.in); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" ); DecimalFormat d = new DecimalFormat( "0.0" ); ArrayList<User> Users = new ArrayList<>(); String[] str = new String[ 100 ]; String[] users = new String[ 100 ]; String[] userRecords = new String[ 100 ]; int c= 0 ,k= 0 ,m= 0 ; for ( int i= 0 ;;i++) { str[i]=in.nextLine(); c++; if (str[i].contains( "end" )) break ; } for ( int i= 0 ;i<c- 1 ;i++) { if (str[i].matches( "u-0791[\\d]{7,8} 0" )||str[i].matches( "u-1[\\d]{10} 1" )) { boolean exist = false ; for ( int j= 0 ;j<k;j++) { if (users[j].equals(str[i])) exist = true ; } if (!exist) { users[k] = str[i]; k++; } } else if (str[i].matches( "t-0(\\d){9,11}\\s0(\\d){9,11}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ userRecords[m] = str[i]; m++; } else if (str[i].matches( "t-1(\\d){10}\\s0(\\d){2,3}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ userRecords[m] = str[i]; m++; } else if (str[i].matches( "t-0(\\d){9,11}\\s1(\\d){10}\\s(\\d){3,4}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ userRecords[m] = str[i]; m++; } else if (str[i].matches( "t-1(\\d){10}\\s0(\\d){2,3}\\s1(\\d){10}\\s0(\\d){2,3}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ userRecords[m] = str[i]; m++; } } for ( int i= 0 ;i<k;i++) { Users.add( new User(users[i])); } for ( int i= 0 ;i<k;i++) { for ( int j= 0 ;j<m;j++) { String[] sub = userRecords[j].split( " " ); if (sub[ 0 ].substring( 2 ,sub[ 0 ].length()).matches(Users.get(i).getNumber())) { if (sub[ 1 ].matches( "0791[\\d]{7,8}" )) Users.get(i).userRecords.addCallInCity( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]),sub[ 0 ],sub[ 1 ])); else if (sub[ 1 ].matches( "((079[\\d])|(0701))[\\d]{7,8}" )) Users.get(i).userRecords.addCallInProvince( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]),sub[ 0 ],sub[ 1 ])); else if (sub[ 1 ].matches( "0(\\d){9,11}" )) Users.get(i).userRecords.addCallInLand( new CallRecord(sdf.parse(sub[ 2 ]+ " " +sub[ 3 ]), sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]),sub[ 0 ],sub[ 1 ])); else if (sub[ 2 ].matches( "0791[\\d]{7,8}" )) Users.get(i).userRecords.addCallInCity( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); else if (sub[ 2 ].matches( "((079[\\d])|(0701))[\\d]{7,8}" )) Users.get(i).userRecords.addCallInProvince( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); else if (sub[ 2 ].matches( "0(\\d){9,11}" )) Users.get(i).userRecords.addCallInLand( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); else if (sub[ 2 ].matches( "0791" )) { Users.get(i).userRecords.addCallInCity( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ],sub[ 1 ])); } else if (sub[ 2 ].matches( "(079[\\d])|(0701)" )) { Users.get(i).userRecords.addCallInProvince( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ],sub[ 1 ])); } else if (sub[ 2 ].matches( "0(\\d){2,3}" )) { Users.get(i).userRecords.addCallInLand( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ],sub[ 1 ])); } else if (sub[ 3 ].matches( "0791" )) { Users.get(i).userRecords.addCallInCity( new CallRecord(sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]), sdf.parse(sub[ 6 ]+ " " +sub[ 7 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); } else if (sub[ 3 ].matches( "(079[\\d])|(0701)" )) { Users.get(i).userRecords.addCallInProvince( new CallRecord(sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]), sdf.parse(sub[ 6 ]+ " " +sub[ 7 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); } else if (sub[ 3 ].matches( "0(\\d){2,3}" )) { Users.get(i).userRecords.addCallInLand( new CallRecord(sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]), sdf.parse(sub[ 6 ]+ " " +sub[ 7 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); } } else if (sub[ 1 ].matches(Users.get(i).getNumber())) { if (sub[ 0 ].substring( 2 , 6 ).matches( "0791" )) continue ; else if (sub[ 0 ].substring( 2 , 6 ).matches( "(079[\\d])|(0701)" )) continue ; else if (sub[ 0 ].substring( 2 , 6 ).matches( "0(\\d){3}" )) { Users.get(i).userRecords.addAnswerInLand( new CallRecord(sdf.parse(sub[ 3 ]+ " " +sub[ 4 ]), sdf.parse(sub[ 5 ]+ " " +sub[ 6 ]),sub[ 0 ],sub[ 2 ])); } } else if (sub[ 2 ].matches(Users.get(i).getNumber())) { if (sub[ 1 ].matches( "0791" )) continue ; else if (sub[ 1 ].matches( "(079[\\d])|(0701)" )) continue ; else if (sub[ 1 ].matches( "0(\\d){2,3}" )) { Users.get(i).userRecords.addAnswerInLand( new CallRecord(sdf.parse(sub[ 4 ]+ " " +sub[ 5 ]), sdf.parse(sub[ 6 ]+ " " +sub[ 7 ]),sub[ 0 ]+ " " +sub[ 1 ],sub[ 2 ])); } } } } Collections.sort(Users); for (User e:Users) { System.out.println(e.getNumber()+ " " +d.format(e.calCost())+ " " +d.format(e.calBalance())); } } } 手机用户的计费方式如下: class MovePhoneCharge extends ChargeMode{ private double monthlyRent = 15 ; @Override public double calCost(UserRecord userRecords) { double cost1 = 0 ,cost2 = 0 ,cost3 = 0 ,cost4 = 0 ; double rate1 = 0 ,rate2 = 0 ,rate3 = 0 ; for ( int i= 0 ;i<userRecords.getCallInCity().size();i++) { if (userRecords.getCallInCity().get(i).getCallCode().split( " " )[ 1 ].matches( "0791" )) rate1 = 0.1 ; else if (userRecords.getCallInCity().get(i).getCallCode().split( " " )[ 1 ].matches( "(079\\d)|(0710)" )) rate1 = 0.3 ; else rate1 = 0.6 ; long time = userRecords.getCallInCity().get(i).getEnd().getTime()-userRecords.getCallInCity().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost1 += rate1*(time/( 1000 * 60 )); else cost1 += rate1*(time/( 1000 * 60 )+ 1 ); } for ( int i= 0 ;i<userRecords.getCallInProvince().size();i++) { if (userRecords.getCallInProvince().get(i).getCallCode().split( " " )[ 1 ].matches( "0791" )) rate2 = 0.2 ; else if (userRecords.getCallInProvince().get(i).getCallCode().split( " " )[ 1 ].matches( "(079\\d)|(0710)" )) rate2 = 0.3 ; else rate2 = 0.6 ; long time = userRecords.getCallInProvince().get(i).getEnd().getTime()-userRecords.getCallInProvince().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost2 += rate2*(time/( 1000 * 60 )); else cost2 += rate2*(time/( 1000 * 60 )+ 1 ); } for ( int i= 0 ;i<userRecords.getCallInLand().size();i++) { if (userRecords.getCallInLand().get(i).getCallCode().split( " " )[ 1 ].matches( "0791" )) rate3 = 0.3 ; else if (userRecords.getCallInLand().get(i).getCallCode().split( " " )[ 1 ].matches( "(079\\d)|(0710)" )) rate3 = 0.3 ; else rate3 = 0.6 ; long time = userRecords.getCallInLand().get(i).getEnd().getTime()-userRecords.getCallInLand().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost3 += rate3*(time/( 1000 * 60 )); else cost3 += rate3*(time/( 1000 * 60 )+ 1 ); } for ( int i= 0 ;i<userRecords.getAnswerInLand().size();i++) { long time = userRecords.getAnswerInLand().get(i).getEnd().getTime()-userRecords.getAnswerInLand().get(i).getStart().getTime(); if (time%( 1000 * 60 )== 0 ) cost4 += 0.3 *(time/( 1000 * 60 )); else cost4 += 0.3 *(time/( 1000 * 60 )+ 1 ); } return cost1+cost2+cost3+cost4; } @Override public double getMonthlyRent() { return monthlyRent; } } |
3. 题目集10
7-1 电信计费系列3-短信计费
本题只需要考虑开户信息和短信,在题目集09的基础上进行一些删改就可以完成了。可以说,这次题目集是这三次作业中最轻松的一次了,因为之前都把基础做好了。源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | import java.util.Date; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) throws ParseException { Scanner in = new Scanner(System.in); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" ); DecimalFormat d = new DecimalFormat( "0.0" ); ArrayList<User> Users = new ArrayList<>(); String[] str = new String[ 100 ]; String[] users = new String[ 100 ]; String[] userRecords = new String[ 100 ]; int c= 0 ,k= 0 ,m= 0 ; for ( int i= 0 ;;i++) { str[i]=in.nextLine(); c++; if (str[i].contains( "end" )) break ; } for ( int i= 0 ;i<c- 1 ;i++) { if (str[i].matches( "u-1\\d{10} 3" )) { boolean exist = false ; for ( int j= 0 ;j<k;j++) { if (users[j].equals(str[i])) exist = true ; } if (!exist) { users[k] = str[i]; k++; } } else if (str[i].matches( "m-1\\d{10} 1\\d{10} ([\\d]|[A-Z]|[a-z]| |\\.|\\,)*" )) { userRecords[m] = str[i]; m++; } } for ( int i= 0 ;i<k;i++) { Users.add( new User(users[i])); for ( int j= 0 ;j<m;j++) { if (userRecords[j].substring( 2 , 13 ).matches(Users.get(i).getNumber())) { Users.get(i).userRecords.addSendMessage( new MessageRecord(userRecords[j].substring( 26 ))); } } } Collections.sort(Users); for (User e:Users) { System.out.println(e.getNumber()+ " " +d.format(e.calCost())+ " " +d.format(e.calBalance())); } } } class User implements Comparable<User>{ UserRecord userRecords = new UserRecord(); private double balance = 100 ; private ChargeMode chargeMode; private String number; public User(String str) { this .number = str.substring( 2 ,str.length()- 2 ); if (str.charAt(str.length()- 1 ) == '3' ) this .setChargeMode( new MovePhoneMessageCharge()); } public double calBalance() { return balance-chargeMode.getMonthlyRent()-calCost(); } public double calCost() { return chargeMode.calCost(userRecords); } public UserRecord getUserRecords() { return userRecords; } public void setUserRecords(UserRecord ur) { userRecords = ur; } public double getBalance() { return balance; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode cm) { chargeMode = cm; } public String getNumber() { return number; } public void setNumber(String n) { number = n; } @Override public int compareTo(User o) { return this .getNumber().compareTo(o.getNumber()); } } class UserRecord{ private ArrayList<CallRecord> callInCity = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callInProvince = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callInLand = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCity = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvince = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLand = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessage = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessage = new ArrayList<MessageRecord>(); public void addCallInCity(CallRecord cr) { callInCity.add(cr); } public void addCallInProvince(CallRecord cr) { callInProvince.add(cr); } public void addCallInLand(CallRecord cr) { callInLand.add(cr); } public void addAnswerInCity(CallRecord cr) { answerInCity.add(cr); } public void addAnswerInProvince(CallRecord cr) { answerInProvince.add(cr); } public void addAnswerInLand(CallRecord cr) { answerInLand.add(cr); } public void addSendMessage(MessageRecord mr) { sendMessage.add(mr); } public void addReceiveMessage(MessageRecord mr) { receiveMessage.add(mr); } public ArrayList<CallRecord> getCallInCity() { return callInCity; } public ArrayList<CallRecord> getCallInProvince() { return callInProvince; } public ArrayList<CallRecord> getCallInLand() { return callInLand; } public ArrayList<CallRecord> getAnswerInCity() { return answerInCity; } public ArrayList<CallRecord> getAnswerInProvince() { return answerInProvince; } public ArrayList<CallRecord> getAnswerInLand() { return answerInLand; } public ArrayList<MessageRecord> getSendMessage() { return sendMessage; } public ArrayList<MessageRecord> getReceiveMessage() { return receiveMessage; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules(){ return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> cr) { chargeRules = cr; } public double calCost(UserRecord ur) { return 0 ; } public double getMonthlyRent() { return 20 ; } } class MovePhoneMessageCharge extends ChargeMode{ private double monthlyRent = 0 ; public double calCost(UserRecord userRecords) { double cost = 0 ; int ml = 0 ; double cal = 0 ; for ( int i= 0 ;i<userRecords.getSendMessage().size();i++) { cal = userRecords.getSendMessage().get(i).getMessage().length()/ 10.0 ; if (cal% 1 != 0 ) cal = cal+ 1.0 ; ml += ( int )cal; } if (ml<= 3 ) cost = 0.1 *ml; else if (ml<= 5 ) cost = 0.3 + 0.2 *(ml- 3 ); else if (ml> 5 ) cost = 0.7 + 0.3 *(ml- 5 ); return cost; } @Override public double getMonthlyRent() { return monthlyRent; } } abstract class ChargeRule{ } abstract class CallChangeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> cr) { return 0 ; } } class CityRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } class ProvinceRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } class LandRule extends CallChangeRule{ @Override public double calCost(ArrayList<CallRecord> cr) { // TODO Auto-generated method stub return super .calCost(cr); } } abstract class CommunicationRecord{ String callNumber; String answerNumber; public String getCallNumber() { return callNumber; } public void setCallNumber(String cn) { callNumber = cn; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String an) { answerNumber = an; } } class CallRecord extends CommunicationRecord{ private Date start; private Date end; private String callCode; private String answerCode; public CallRecord(Date start,Date end ,String callCode, String answerCode) { this .start = start; this .end = end; this .callCode = callCode; this .answerCode = answerCode; } public Date getStart() { return start; } public void setStart(Date start) { this .start = start; } public Date getEnd() { return end; } public void setEnd(Date end) { this .end = end; } public String getCallCode() { return callCode; } public void setCallCode(String callCode) { this .callCode = callCode; } public String getAnswerCode() { return answerCode; } public void setAnswerCode(String answerCode) { this .answerCode = answerCode; } } class MessageRecord extends CommunicationRecord{ private String message; public MessageRecord(String m) { message = m; } public String getMessage() { return message; } } |
三、 采坑心得
在完成这3次题目集的过程中,我遇到了一些的问题,下面将对我遇到一些的问题分享一下我的心得:
首先,日期的转化问题。将字符串按格式转化为日期时,需要在转化发生的函数后面
加上throws ParseException,也就是需要抛出异常;其次,用户排序的问题。一开始我使用的是for循环来进行排序,测试用例的输出结果是正确的,但是一直过不了测试点。在和室友的排序方法进行对比后,我发现了问题所在,于是采用了Collections.sort(Users)这种方式来进行排序,顺利地通过了全部测试点。
四、 改进建议
前面说道,我判断通讯记录的正则表达式十分繁琐,这是一个急需改进的地方。SimpleDateFormat类可以对日期进行格式化,可是我对此并没有很了解,应用起来十分困难,希望之后可以熟练应用。
五、 总结
在完成这三次题目集的过程中,我遇到了很多困难,也收获了很多知识点。尽管我总是被难题虐到心累,我还是愿意继续不断的尝试,直到正确。之后,我需要学习和改进的地方还有很多,但是我会尽量使自己变得更优秀。靠自己的努力得到的满分,才是满满的成就感!
第三次博客作业
一、 前言
又经过一段时间的学习,我在Java这门课程上又多学到了一些知识点。难度方面,或许是为了减轻一些我们在Java这门课程上的压力,最近三次的PTA作业简单了一些。基本上只要投入了足够的时间就可以完成,并且这三次作业也是迭代的。题量方面,这三次作业题量都很少,只有两三道题目。大概是因为网课上得太久了,做PTA的心情有点懈怠,好在我还是及时地完成了。这三次作业情况如下:
知识点主要有:容器,集合的应用,接口的实现,继承等。
2. 题目集09
知识点主要有:有序集合,迭代器以及题目集08中的知识点。
3. 题目集10
知识点主要有:正则表达式的应用,字符串的处理,多态的应用以及上一个题目集中的知识点。
二、 设计与分析
下面我将要对这三次的题目集进行详细分析,重点是对源码的分析,解释和完成题目后的心得。分析如下:(以下均是我个人的理解,可能与标准说法不同,请见谅)
1. 题目集08
7-1 电信计费系列1-座机计费
本题思路是先将输入的不同信息储存至不同的数组中,同时判断是否有重复的信息,再进行实例化:如果输入的是开户的信息,那么先存入一个字符串数组,判断无重复信息后,对于每条开户信息创建一个User对象,储存至一个只存入User对象的容器中;如果输入的是通讯记录,同上,存入不同的数组和容器中。之后,对每条通讯记录进行详细操作。在判断通讯记录的格式时,所用的正则表达式非常繁琐,主要是后面日期格式的判断不容易实现。
源码如下:
import java.util.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
Scanner in = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
DecimalFormat d = new DecimalFormat("0.0");
ArrayList<User> Users = new ArrayList<User>();
String[] str = new String[100];
String[] users = new String[100];
String[] userRecords = new String[100];
int c=0,k=0,m=0;
for(int i=0;;i++) {
str[i]=in.nextLine();
c++;
if(str[i].contains("end"))
break;
}
for(int i=0;i<c-1;i++) {
if(str[i].matches("u-0791[\\d]{7,8} 0")) {
boolean exist = false;
for(int j=0;j<k;j++) {
if(users[j].equals(str[i]))
exist = true;
}
if(!exist) {
users[k] = str[i];
k++;
}
}else if(str[i].matches("t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
userRecords[m] = str[i];
m++;
}
}
for(int i=0;i<k;i++) {
Users.add(new User(users[i]));
for(int j=0;j<m;j++) {
String[] sub = userRecords[j].split(" ");
if(sub[0].substring(2,sub[0].length()).matches(Users.get(i).getNumber())) {
if(sub[1].substring(0, 4).matches("0791"))
Users.get(i).userRecords.addCallInCity(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5])));
else if(sub[1].substring(0, 4).matches("(079\\d)|(0701)"))
Users.get(i).userRecords.addCallInProvince(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5])));
else
Users.get(i).userRecords.addCallInLand(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5])));
}
}
}
Collections.sort(Users);
for(User e:Users) {
System.out.println(e.getNumber()+" "+d.format(e.calCost())+" "+d.format(e.calBalance()));
}
}
}
class User implements Comparable<User>{
UserRecord userRecords = new UserRecord();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public User(String str) {
this.number = str.substring(2,str.length()-2);
if(str.charAt(str.length()-1) == '0')
this.setChargeMode(new LandPhoneCharge());
}
public double calBalance() {
return balance-chargeMode.getMonthlyRent()-calCost();
}
public double calCost() {
double cost1 = 0,cost2 = 0,cost3 = 0;
for(int i=0;i<userRecords.getCallInCity().size();i++) {
long time = userRecords.getCallInCity().get(i).getEnd().getTime()-userRecords.getCallInCity().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost1 += 0.1*(time/(1000*60));
else
cost1 += 0.1*(time/(1000*60)+1);
}
for(int i=0;i<userRecords.getCallInProvince().size();i++) {
long time = userRecords.getCallInProvince().get(i).getEnd().getTime()-userRecords.getCallInProvince().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost2 += 0.3*(time/(1000*60));
else
cost2 += 0.3*(time/(1000*60)+1);
}
for(int i=0;i<userRecords.getCallInLand().size();i++) {
long time = userRecords.getCallInLand().get(i).getEnd().getTime()-userRecords.getCallInLand().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost3 += 0.6*(time/(1000*60));
else
cost3 += 0.6*(time/(1000*60)+1);
}
return cost1+cost2+cost3;
}
public UserRecord getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecord ur) {
userRecords = ur;
}
public double getBalance() {
return balance;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode cm) {
chargeMode = cm;
}
public String getNumber() {
return number;
}
public void setNumber(String n) {
number = n;
}
@Override
public int compareTo(User o) {
return this.getNumber().compareTo(o.getNumber());
}
}
class UserRecord{
private ArrayList<CallRecord> callInCity = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callInProvince = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callInLand = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInCity = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvince = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLand = new ArrayList<CallRecord>();
private ArrayList<MessageRecord> sendMessage = new ArrayList<MessageRecord>();
private ArrayList<MessageRecord> receiveMessage = new ArrayList<MessageRecord>();
public void addCallInCity(CallRecord cr) {
callInCity.add(cr);
}
public void addCallInProvince(CallRecord cr) {
callInProvince.add(cr);
}
public void addCallInLand(CallRecord cr) {
callInLand.add(cr);
}
public void addAnswerInCity(CallRecord cr) {
answerInCity.add(cr);
}
public void addAnswerInProvince(CallRecord cr) {
answerInProvince.add(cr);
}
public void addAnswerInLand(CallRecord cr) {
answerInLand.add(cr);
}
public void addSendMessage(MessageRecord mr) {
sendMessage.add(mr);
}
public void addReceiveMessage(MessageRecord mr) {
receiveMessage.add(mr);
}
public ArrayList<CallRecord> getCallInCity() {
return callInCity;
}
public ArrayList<CallRecord> getCallInProvince() {
return callInProvince;
}
public ArrayList<CallRecord> getCallInLand() {
return callInLand;
}
public ArrayList<CallRecord> getAnswerInCity() {
return answerInCity;
}
public ArrayList<CallRecord> getAnswerInProvince() {
return answerInProvince;
}
public ArrayList<CallRecord> getAnswerInLand() {
return answerInLand;
}
public ArrayList<MessageRecord> getSendMessage() {
return sendMessage;
}
public ArrayList<MessageRecord> getReceiveMessage() {
return receiveMessage;
}
}
abstract class ChargeMode{
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules(){
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> cr) {
chargeRules = cr;
}
public double calCost(UserRecord ur) {
return 0;
}
public double getMonthlyRent() {
return 20;
}
}
class LandPhoneCharge extends ChargeMode{
private double monthlyRent = 20;
@Override
public double calCost(UserRecord ur) {
return 0;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class ChargeRule{
}
abstract class CallChangeRule extends ChargeRule{
public double calCost(ArrayList<CallRecord> cr) {
return 0;
}
}
class CityRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
class ProvinceRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
class LandRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
abstract class CommunicationRecord{
String callNumber;
String answerNumber;
public String getCallNumber() {
return callNumber;
}
public void setCallNumber(String cn) {
callNumber = cn;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String an) {
answerNumber = an;
}
}
class CallRecord extends CommunicationRecord{
private Date start;
private Date end;
private String callCode;
private String answerCode;
public CallRecord(Date start,Date end) {
this.start = start;
this.end = end;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
public String getCallCode() {
return callCode;
}
public void setCallCode(String callCode) {
this.callCode = callCode;
}
public String getAnswerCode() {
return answerCode;
}
public void setAnswerCode(String answerCode) {
this.answerCode = answerCode;
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2. 题目集09
7-1 电信计费系列2-手机+座机计费
本题是在题目集08第一题的基础上进行改进,增加了手机用户。在处理每条有手机用户的通讯记录时,同时需要判断手机用户所在的地区。总而言之,通讯记录的判断和处理更加详细,也更加繁琐了。需要创建手机用户的计费方式,之后在主函数中增加和修改亿点点就可以完成了。因为当时有点懈怠,做的时间比较晚,所以只得了97分,差3分,有点可惜。源码如下(部分):
public class Main {
public static void main(String[] args) throws ParseException {
Scanner in = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
DecimalFormat d = new DecimalFormat("0.0");
ArrayList<User> Users = new ArrayList<>();
String[] str = new String[100];
String[] users = new String[100];
String[] userRecords = new String[100];
int c=0,k=0,m=0;
for(int i=0;;i++) {
str[i]=in.nextLine();
c++;
if(str[i].contains("end"))
break;
}
for(int i=0;i<c-1;i++) {
if(str[i].matches("u-0791[\\d]{7,8} 0")||str[i].matches("u-1[\\d]{10} 1")) {
boolean exist = false;
for(int j=0;j<k;j++) {
if(users[j].equals(str[i]))
exist = true;
}
if(!exist) {
users[k] = str[i];
k++;
}
}else if(str[i].matches("t-0(\\d){9,11}\\s0(\\d){9,11}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
userRecords[m] = str[i];
m++;
}else if(str[i].matches("t-1(\\d){10}\\s0(\\d){2,3}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
userRecords[m] = str[i];
m++;
}else if(str[i].matches("t-0(\\d){9,11}\\s1(\\d){10}\\s(\\d){3,4}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
userRecords[m] = str[i];
m++;
}else if(str[i].matches("t-1(\\d){10}\\s0(\\d){2,3}\\s1(\\d){10}\\s0(\\d){2,3}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
userRecords[m] = str[i];
m++;
}
}
for(int i=0;i<k;i++) {
Users.add(new User(users[i]));
}
for(int i=0;i<k;i++) {
for(int j=0;j<m;j++) {
String[] sub = userRecords[j].split(" ");
if(sub[0].substring(2,sub[0].length()).matches(Users.get(i).getNumber())) {
if(sub[1].matches("0791[\\d]{7,8}"))
Users.get(i).userRecords.addCallInCity(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5]),sub[0],sub[1]));
else if(sub[1].matches("((079[\\d])|(0701))[\\d]{7,8}"))
Users.get(i).userRecords.addCallInProvince(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5]),sub[0],sub[1]));
else if(sub[1].matches("0(\\d){9,11}"))
Users.get(i).userRecords.addCallInLand(new CallRecord(sdf.parse(sub[2]+" "+sub[3]), sdf.parse(sub[4]+" "+sub[5]),sub[0],sub[1]));
else if(sub[2].matches("0791[\\d]{7,8}"))
Users.get(i).userRecords.addCallInCity(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0]+" "+sub[1],sub[2]));
else if(sub[2].matches("((079[\\d])|(0701))[\\d]{7,8}"))
Users.get(i).userRecords.addCallInProvince(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0]+" "+sub[1],sub[2]));
else if(sub[2].matches("0(\\d){9,11}"))
Users.get(i).userRecords.addCallInLand(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0]+" "+sub[1],sub[2]));
else if(sub[2].matches("0791")) {
Users.get(i).userRecords.addCallInCity(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0],sub[1]));
}else if(sub[2].matches("(079[\\d])|(0701)")) {
Users.get(i).userRecords.addCallInProvince(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0],sub[1]));
}else if(sub[2].matches("0(\\d){2,3}")) {
Users.get(i).userRecords.addCallInLand(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0],sub[1]));
}else if(sub[3].matches("0791")) {
Users.get(i).userRecords.addCallInCity(new CallRecord(sdf.parse(sub[4]+" "+sub[5]), sdf.parse(sub[6]+" "+sub[7]),sub[0]+" "+sub[1],sub[2]));
}else if(sub[3].matches("(079[\\d])|(0701)")) {
Users.get(i).userRecords.addCallInProvince(new CallRecord(sdf.parse(sub[4]+" "+sub[5]), sdf.parse(sub[6]+" "+sub[7]),sub[0]+" "+sub[1],sub[2]));
}else if(sub[3].matches("0(\\d){2,3}")) {
Users.get(i).userRecords.addCallInLand(new CallRecord(sdf.parse(sub[4]+" "+sub[5]), sdf.parse(sub[6]+" "+sub[7]),sub[0]+" "+sub[1],sub[2]));
}
}else if(sub[1].matches(Users.get(i).getNumber())) {
if(sub[0].substring(2,6).matches("0791"))
continue;
else if(sub[0].substring(2,6).matches("(079[\\d])|(0701)"))
continue;
else if(sub[0].substring(2,6).matches("0(\\d){3}")) {
Users.get(i).userRecords.addAnswerInLand(new CallRecord(sdf.parse(sub[3]+" "+sub[4]), sdf.parse(sub[5]+" "+sub[6]),sub[0],sub[2]));
}
}else if(sub[2].matches(Users.get(i).getNumber())) {
if(sub[1].matches("0791"))
continue;
else if(sub[1].matches("(079[\\d])|(0701)"))
continue;
else if(sub[1].matches("0(\\d){2,3}")) {
Users.get(i).userRecords.addAnswerInLand(new CallRecord(sdf.parse(sub[4]+" "+sub[5]), sdf.parse(sub[6]+" "+sub[7]),sub[0]+" "+sub[1],sub[2]));
}
}
}
}
Collections.sort(Users);
for(User e:Users) {
System.out.println(e.getNumber()+" "+d.format(e.calCost())+" "+d.format(e.calBalance()));
}
}
}
手机用户的计费方式如下:
class MovePhoneCharge extends ChargeMode{
private double monthlyRent = 15;
@Override
public double calCost(UserRecord userRecords) {
double cost1 = 0,cost2 = 0,cost3 = 0,cost4 = 0;
double rate1 = 0,rate2 = 0,rate3 = 0;
for(int i=0;i<userRecords.getCallInCity().size();i++) {
if(userRecords.getCallInCity().get(i).getCallCode().split(" ")[1].matches("0791"))
rate1 = 0.1;
else if(userRecords.getCallInCity().get(i).getCallCode().split(" ")[1].matches("(079\\d)|(0710)"))
rate1 = 0.3;
else
rate1 = 0.6;
long time = userRecords.getCallInCity().get(i).getEnd().getTime()-userRecords.getCallInCity().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost1 += rate1*(time/(1000*60));
else
cost1 += rate1*(time/(1000*60)+1);
}
for(int i=0;i<userRecords.getCallInProvince().size();i++) {
if(userRecords.getCallInProvince().get(i).getCallCode().split(" ")[1].matches("0791"))
rate2 = 0.2;
else if(userRecords.getCallInProvince().get(i).getCallCode().split(" ")[1].matches("(079\\d)|(0710)"))
rate2 = 0.3;
else
rate2 = 0.6;
long time = userRecords.getCallInProvince().get(i).getEnd().getTime()-userRecords.getCallInProvince().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost2 += rate2*(time/(1000*60));
else
cost2 += rate2*(time/(1000*60)+1);
}
for(int i=0;i<userRecords.getCallInLand().size();i++) {
if(userRecords.getCallInLand().get(i).getCallCode().split(" ")[1].matches("0791"))
rate3 = 0.3;
else if(userRecords.getCallInLand().get(i).getCallCode().split(" ")[1].matches("(079\\d)|(0710)"))
rate3 = 0.3;
else
rate3 = 0.6;
long time = userRecords.getCallInLand().get(i).getEnd().getTime()-userRecords.getCallInLand().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost3 += rate3*(time/(1000*60));
else
cost3 += rate3*(time/(1000*60)+1);
}
for(int i=0;i<userRecords.getAnswerInLand().size();i++) {
long time = userRecords.getAnswerInLand().get(i).getEnd().getTime()-userRecords.getAnswerInLand().get(i).getStart().getTime();
if(time%(1000*60)==0)
cost4 += 0.3*(time/(1000*60));
else
cost4 += 0.3*(time/(1000*60)+1);
}
return cost1+cost2+cost3+cost4;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
3. 题目集10
7-1 电信计费系列3-短信计费
本题只需要考虑开户信息和短信,在题目集09的基础上进行一些删改就可以完成了。可以说,这次题目集是这三次作业中最轻松的一次了,因为之前都把基础做好了。源码如下:
import java.util.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
Scanner in = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
DecimalFormat d = new DecimalFormat("0.0");
ArrayList<User> Users = new ArrayList<>();
String[] str = new String[100];
String[] users = new String[100];
String[] userRecords = new String[100];
int c=0,k=0,m=0;
for(int i=0;;i++) {
str[i]=in.nextLine();
c++;
if(str[i].contains("end"))
break;
}
for(int i=0;i<c-1;i++) {
if(str[i].matches("u-1\\d{10} 3")) {
boolean exist = false;
for(int j=0;j<k;j++) {
if(users[j].equals(str[i]))
exist = true;
}
if(!exist) {
users[k] = str[i];
k++;
}
}else if(str[i].matches("m-1\\d{10} 1\\d{10} ([\\d]|[A-Z]|[a-z]| |\\.|\\,)*")) {
userRecords[m] = str[i];
m++;
}
}
for(int i=0;i<k;i++) {
Users.add(new User(users[i]));
for(int j=0;j<m;j++) {
if(userRecords[j].substring(2,13).matches(Users.get(i).getNumber())) {
Users.get(i).userRecords.addSendMessage(new MessageRecord(userRecords[j].substring(26)));
}
}
}
Collections.sort(Users);
for(User e:Users) {
System.out.println(e.getNumber()+" "+d.format(e.calCost())+" "+d.format(e.calBalance()));
}
}
}
class User implements Comparable<User>{
UserRecord userRecords = new UserRecord();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public User(String str) {
this.number = str.substring(2,str.length()-2);
if(str.charAt(str.length()-1) == '3')
this.setChargeMode(new MovePhoneMessageCharge());
}
public double calBalance() {
return balance-chargeMode.getMonthlyRent()-calCost();
}
public double calCost() {
return chargeMode.calCost(userRecords);
}
public UserRecord getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecord ur) {
userRecords = ur;
}
public double getBalance() {
return balance;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode cm) {
chargeMode = cm;
}
public String getNumber() {
return number;
}
public void setNumber(String n) {
number = n;
}
@Override
public int compareTo(User o) {
return this.getNumber().compareTo(o.getNumber());
}
}
class UserRecord{
private ArrayList<CallRecord> callInCity = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callInProvince = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callInLand = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInCity = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvince = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLand = new ArrayList<CallRecord>();
private ArrayList<MessageRecord> sendMessage = new ArrayList<MessageRecord>();
private ArrayList<MessageRecord> receiveMessage = new ArrayList<MessageRecord>();
public void addCallInCity(CallRecord cr) {
callInCity.add(cr);
}
public void addCallInProvince(CallRecord cr) {
callInProvince.add(cr);
}
public void addCallInLand(CallRecord cr) {
callInLand.add(cr);
}
public void addAnswerInCity(CallRecord cr) {
answerInCity.add(cr);
}
public void addAnswerInProvince(CallRecord cr) {
answerInProvince.add(cr);
}
public void addAnswerInLand(CallRecord cr) {
answerInLand.add(cr);
}
public void addSendMessage(MessageRecord mr) {
sendMessage.add(mr);
}
public void addReceiveMessage(MessageRecord mr) {
receiveMessage.add(mr);
}
public ArrayList<CallRecord> getCallInCity() {
return callInCity;
}
public ArrayList<CallRecord> getCallInProvince() {
return callInProvince;
}
public ArrayList<CallRecord> getCallInLand() {
return callInLand;
}
public ArrayList<CallRecord> getAnswerInCity() {
return answerInCity;
}
public ArrayList<CallRecord> getAnswerInProvince() {
return answerInProvince;
}
public ArrayList<CallRecord> getAnswerInLand() {
return answerInLand;
}
public ArrayList<MessageRecord> getSendMessage() {
return sendMessage;
}
public ArrayList<MessageRecord> getReceiveMessage() {
return receiveMessage;
}
}
abstract class ChargeMode{
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules(){
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> cr) {
chargeRules = cr;
}
public double calCost(UserRecord ur) {
return 0;
}
public double getMonthlyRent() {
return 20;
}
}
class MovePhoneMessageCharge extends ChargeMode{
private double monthlyRent = 0;
public double calCost(UserRecord userRecords) {
double cost = 0;
int ml = 0;
double cal = 0;
for(int i=0;i<userRecords.getSendMessage().size();i++) {
cal = userRecords.getSendMessage().get(i).getMessage().length()/10.0;
if(cal%1!=0)
cal = cal+1.0;
ml += (int)cal;
}
if(ml<=3)
cost = 0.1*ml;
else if(ml<=5)
cost = 0.3+0.2*(ml-3);
else if(ml>5)
cost = 0.7+0.3*(ml-5);
return cost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class ChargeRule{
}
abstract class CallChangeRule extends ChargeRule{
public double calCost(ArrayList<CallRecord> cr) {
return 0;
}
}
class CityRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
class ProvinceRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
class LandRule extends CallChangeRule{
@Override
public double calCost(ArrayList<CallRecord> cr) {
// TODO Auto-generated method stub
return super.calCost(cr);
}
}
abstract class CommunicationRecord{
String callNumber;
String answerNumber;
public String getCallNumber() {
return callNumber;
}
public void setCallNumber(String cn) {
callNumber = cn;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String an) {
answerNumber = an;
}
}
class CallRecord extends CommunicationRecord{
private Date start;
private Date end;
private String callCode;
private String answerCode;
public CallRecord(Date start,Date end ,String callCode, String answerCode) {
this.start = start;
this.end = end;
this.callCode = callCode;
this.answerCode = answerCode;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
public String getCallCode() {
return callCode;
}
public void setCallCode(String callCode) {
this.callCode = callCode;
}
public String getAnswerCode() {
return answerCode;
}
public void setAnswerCode(String answerCode) {
this.answerCode = answerCode;
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public MessageRecord(String m) {
message = m;
}
public String getMessage() {
return message;
}
}
三、 采坑心得
在完成这3次题目集的过程中,我遇到了一些的问题,下面将对我遇到一些的问题分享一下我的心得:
首先,日期的转化问题。将字符串按格式转化为日期时,需要在转化发生的函数后面
加上throws ParseException,也就是需要抛出异常;其次,用户排序的问题。一开始我使用的是for循环来进行排序,测试用例的输出结果是正确的,但是一直过不了测试点。在和室友的排序方法进行对比后,我发现了问题所在,于是采用了Collections.sort(Users)这种方式来进行排序,顺利地通过了全部测试点。
四、 改进建议
前面说道,我判断通讯记录的正则表达式十分繁琐,这是一个急需改进的地方。SimpleDateFormat类可以对日期进行格式化,可是我对此并没有很了解,应用起来十分困难,希望之后可以熟练应用。
五、 总结
在完成这三次题目集的过程中,我遇到了很多困难,也收获了很多知识点。尽管我总是被难题虐到心累,我还是愿意继续不断的尝试,直到正确。之后,我需要学习和改进的地方还有很多,但是我会尽量使自己变得更优秀。靠自己的努力得到的满分,才是满满的成就感!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?