PTA题目集1/2/3总结
PTA题目集1
PTA的第一个题目集较为简单,所以不做过多描述:
比较坑的是题目没有说明,但要都是float类型,如果在计算中用double类型,那么最后输出时要强制转换成float类型。
7-2 长度质量计量单位换算
7-4 NCHU_房产税费计算
以上两题都是这样,不去百度我都不知道要这样搞(
PTA题目集2
7-1 成绩计算-1-类、数组的基本运用
创建学生类,包含
属性:学号(String)、姓名(String)、语文成绩(int)、数学成绩(int)、物理成绩(int)
方法:计算总分、计算平均分
输入5个学生的信息,将每个学生的信息封装在一个学生对象中。
按输入顺序依次输出5个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。
浮点数保留小数的相关知识可参考:
https://blog.csdn.net/huaishuming/article/details/17752365
注意:未用学生类对象封装数据的,本题计0分
输入格式:
5个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+语文成绩+英文空格+数学成绩+英文空格+物理成绩
例如:
22201311 张琳 80 80 80
22201312 黄昊 66 82 81
22201313 李少辰 77 76 80
22201314 袁婷 62 79 90
22201315 朱哲一 74 98 94
输出格式:
5个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+总成绩+英文空格+平均分
例如:
22201311 张琳 240 80.00
22201312 黄昊 229 76.33
22201313 李少辰 233 77.67
22201314 袁婷 231 77.00
22201315 朱哲一 266 88.67
输入样例:
在这里给出一组输入。例如:
22201311 张琳 80 80 80
22201312 黄昊 66 82 81
22201313 李少辰 77 76 80
22201314 袁婷 62 79 90
22201315 朱哲一 74 98 94
输出样例:
在这里给出相应的输出。例如:
22201311 张琳 240 80.00
22201312 黄昊 229 76.33
22201313 李少辰 233 77.67
22201314 袁婷 231 77.00
22201315 朱哲一 266 88.67
我的答案
1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 4 public class Main { 5 public static class students{ 6 String numberString; 7 String nameString; 8 int chinese; 9 int math; 10 int phy; 11 int zong=0; 12 int all() { 13 zong=chinese+math+phy; 14 return zong; 15 } 16 double average() { 17 double aver=zong/(double)3; 18 return aver; 19 } 20 } 21 22 public static void main(String[] args) { 23 24 students[] stus=new students[5]; 25 DecimalFormat df = new DecimalFormat("#.00"); 26 Scanner putinScanner=new Scanner(System.in); 27 28 //String stu1=putinScanner.nextLine(); 29 for(int i=0;i<5;i++) 30 { 31 stus[i]=new students(); 32 stus[i].numberString=putinScanner.next(); 33 stus[i].nameString=putinScanner.next(); 34 stus[i].chinese=putinScanner.nextInt(); 35 stus[i].math=putinScanner.nextInt(); 36 stus[i].phy=putinScanner.nextInt(); 37 } 38 39 for(int i=0;i<5;i++) 40 { 41 System.out.println(stus[i].numberString+" "+stus[i].nameString+" "+ 42 stus[i].all()+" "+df.format(stus[i].average())); 43 } 44 } 45 46 47 }

7-2 成绩计算-2-关联类
创建成绩类,包含:
属性:平时成绩(int)、期末成绩(int)
方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃)
创建学生类,包含:
属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类)
方法:计算总分、计算平均分
输入3个学生的信息,将每个学生的信息封装在一个学生对象中。
按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。
浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365
注意:未用学生类对象封装数据的,本题计0分
输入格式:
依次输入3个学生的每门课成绩,每个学生成绩信息格式:
学号+英文空格+姓名+英文空格+课程名+英文空格+平时成绩+英文空格+期末成绩
注:3个学生的课程顺序可能会不一致
例如:
22201311 张琳 语文 70 80
22201311 张琳 数学 85 89
22201311 张琳 物理 75 83
22201312 黄昊 语文 66 78
22201312 黄昊 数学 76 82
22201312 黄昊 物理 83 82
22201313 李少辰 语文 86 76
22201313 李少辰 数学 78 76
22201313 李少辰 物理 87 76
输出格式:
3个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+总成绩+英文空格+平均分
例如:
22201311 张琳 242 80.67
22201312 黄昊 234 78.00
22201313 李少辰 236 78.67
输入样例:
在这里给出一组输入。例如:
22201311 张琳 语文 70 80
22201311 张琳 数学 85 89
22201311 张琳 物理 75 83
22201312 黄昊 语文 66 78
22201312 黄昊 数学 76 82
22201312 黄昊 物理 83 82
22201313 李少辰 语文 86 76
22201313 李少辰 数学 78 76
22201313 李少辰 物理 87 76
输出样例:
在这里给出相应的输出。例如:
22201311 张琳 242 76.67 84.00 80.67
22201312 黄昊 234 75.00 80.67 78.00
22201313 李少辰 236 83.67 76.00 78.67
我的答案
1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 4 public class Main { 5 public static class result{ 6 int ping; 7 int mo; 8 int zong() 9 { 10 int zon=(int) (ping*0.4+mo*0.6); 11 return zon; 12 } 13 14 } 15 16 public static class students{ 17 String numberString; 18 String nameString; 19 int[] feng=new int[3]; 20 int zong=0; 21 int all() { 22 zong=feng[0]+feng[1]+feng[2]; 23 return zong; 24 } 25 double average() { 26 double aver=zong/(double)3; 27 return aver; 28 } 29 } 30 31 public static void main(String[] args) { 32 result[] tell=new result[3]; 33 students[] stus=new students[3]; 34 DecimalFormat df = new DecimalFormat("#.00"); 35 Scanner putinScanner=new Scanner(System.in); 36 double[][] jun=new double[3][2]; 37 String putin1,putin2; 38 for(int i=0;i<3;i++) 39 { 40 tell[i]=new result(); 41 stus[i]=new students(); 42 } 43 44 for(int i=0;i<3;i++) 45 { 46 47 for(int n=0;n<3;n++) 48 { 49 50 putin1=putinScanner.next(); 51 if(n==1) 52 stus[i].numberString=putin1; 53 putin1=putinScanner.next(); 54 if(n==1) 55 stus[i].nameString=putin1; 56 putin1=putinScanner.next(); 57 58 putin1=putinScanner.next(); 59 tell[n].ping=Integer.parseInt(putin1); 60 jun[i][0]+=tell[n].ping; 61 62 putin2=putinScanner.next(); 63 tell[n].mo=Integer.parseInt(putin2); 64 jun[i][1]+=tell[n].mo; 65 66 stus[i].feng[n]=tell[n].zong();; 67 68 } 69 } 70 71 72 73 74 75 for(int i=0;i<3;i++) 76 { 77 78 System.out.println(stus[i].numberString+" "+stus[i].nameString+" "+ 79 stus[i].all()+" "+df.format(jun[i][0]/(double)3)+" "+ 80 df.format(jun[i][1]/(double)3)+" "+ 81 df.format(stus[i].average())); 82 } 83 } 84 85 }

后面倒数两题对我来讲就是上难度了,倒数第二题7-7题目给了类,所以花时间磨一磨还是能做出来的
7-7 菜单计价程序-1
某饭店提供4种菜,每种菜品的基础价格如下:
西红柿炒蛋 15
清炒土豆丝 12
麻婆豆腐 12
油淋生菜 9
设计点菜计价程序,根据输入的订单,计算并输出总价格。
订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束
每条点菜记录包含:菜名、份额两个信息。
份额可选项包括:1、2、3,分别代表小、中、大份)
不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格2。
如果计算出现小数,按四舍五入的规则进行处理。
参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
}
点菜记录类:保存订单上的一道菜品记录
Record {
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}
订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(String dishName,int portion)
//添加一条菜品信息到订单中。
}
输入格式:
每条点菜记录的格式:
菜名+空格(英文)+份额
注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
最后一条记录以“end”结束。
输出格式:
订单上所有菜品的总价(整数数值),每份菜
如果订单中包含不能识别的菜名,则在总价之前输出“** does not exist”,**是不能识别的菜名
输入样例:
在这里给出一组输入。例如:
麻婆豆腐 2
西红柿炒蛋 3
end
输出样例:
在这里给出相应的输出。例如:
48
输入样例1:
订单中包含不存在的菜品记录。例如:
麻婆豆腐 2
炒脆肚 2
西红柿炒蛋 3
end
输出样例1:
在这里给出相应的输出。例如:
炒脆肚 does not exist
48
我的答案
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 6 static class Dish { 7 String name;//菜品名称 8 int unit_price; //单价 9 int getPrice(int portion){ 10 int price=0; 11 if(portion==1) 12 {price =unit_price;} 13 else if(portion==2) 14 {price=Math.round((float)(unit_price*1.5));} 15 else if(portion==3) 16 {price=Math.round((float)(unit_price*2));} 17 18 return price; 19 }//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) 20 } 21 static class Menu { 22 23 static Dish dishs[]=new Dish[4];//菜品数组,保存所有菜品信息 24 void Menu2() { 25 dishs[0]=new Dish(); 26 dishs[0].name="西红柿炒蛋"; 27 dishs[0].unit_price=15; 28 dishs[1]=new Dish(); 29 dishs[1].name="清炒土豆丝"; 30 dishs[1].unit_price=12; 31 dishs[2]=new Dish(); 32 dishs[2].name="麻婆豆腐"; 33 dishs[2].unit_price=12; 34 dishs[3]=new Dish(); 35 dishs[3].name="油淋生菜"; 36 dishs[3].unit_price=9; 37 } 38 39 40 static Dish searthDish(String dishName){ 41 for(int i=0;i<4;i++) 42 { 43 if(dishs[i].name.equals(dishName)==true) 44 { 45 return dishs[i]; 46 } 47 } 48 return null; 49 } 50 //根据菜名在菜谱中查找菜品信息,返回Dish对象。 51 } 52 static class Record { 53 Dish d;//菜品 54 int portion;//份额(1/2/3代表小/中/大份) 55 int getPrice(){ 56 57 int price=d.getPrice(portion); 58 return price; 59 }//计价,计算本条记录的价格 60 } 61 static class Order { 62 Record[] records=new Record[10];//保存订单上每一道的记录 63 int n=-1,all=0; 64 int getTotalPrice(){ 65 for(int i=0;i<=n;i++) 66 { 67 all+=records[i].getPrice(); 68 } 69 return all; 70 }//计算订单的总价 71 void addARecord(String dishName,int portion) { 72 /*if(n==0) 73 { 74 records[n]=new Record(); 75 records[n].d=Menu.searthDish(dishName); 76 records[n].portion=portion; 77 78 }else {*/ 79 records[n+=1]=new Record(); 80 records[n].d=Menu.searthDish(dishName); 81 records[n].portion=portion; 82 83 84 } 85 //添加一条菜品信息到订单中。 86 } 87 88 public static void main(String[] args) { 89 // TODO 自动生成的方法存根 90 Menu menu1=new Menu(); 91 menu1.Menu2(); 92 //Record jilu=new Record(); 93 Order dingdangOrder=new Order(); 94 Scanner inputScanner=new Scanner(System.in); 95 ArrayList<String> list = new ArrayList<String>(); 96 String iString; 97 while(!"end".equals(iString=inputScanner.next())) 98 { 99 list.add(iString); 100 } 101 for (int i = 0; i < list.size(); i+=2) { 102 if(menu1.searthDish(list.get(i))==null) 103 { 104 System.out.println(list.get(i)+" does not exist"); 105 106 }else { 107 dingdangOrder.addARecord(list.get(i), Integer.parseInt(list.get(i+1))); 108 } 109 110 } 111 System.out.println(dingdangOrder.getTotalPrice()); 112 113 } 114 115 }

最后一题7-8坐大牢(,一开始想用蠢方法一个一个用scannner输入,但去百度后发现java提供了许多关于时间的类,所以我花了一晚上的时间去了解Localdate类。
其实Localdate类的操作看懂后关于日期的计算也还好,主要一开始的输入我不知道怎么限定就没去管(这里给我自己挖坑了!),利用Localdate就能轻松的计算是否闰年,该日期是当年第几天、当月第几天、当周第几天。判定日期是否合法且结束日期是否早于起始日期。如果均合法,输出结束日期与起始日期之间的相差的天数、月数、年数。
不过我不懂为什么月数要是单纯的做减法?人家Localdate类都是按时间算差的月数(老师你喜欢就好),经过一晚上的折腾终于觉得搞好的时候!Pta最后的两个测试点一直过不去,折磨我到凌晨两点想放弃了,结果听隔壁说是输入判定没有考虑,结果果然如此!如果没有按给定的格式输入就输出日期不合法,最后折磨我的竟然是开头的输入判定!
最后时间到了才知道这件事!我裂开(
7-8 jmu-java-日期类的基本使用
- 给定一个日期,判定是否为合法日期。如果合法,判断该年是否闰年,该日期是当年第几天、当月第几天、当周第几天、。
- 给定起始日期与结束日期,判定日期是否合法且结束日期是否早于起始日期。如果均合法,输出结束日期与起始日期之间的相差的天数、月数、念书。
输入格式:
第一行输入一个日期字符串,格式为"YYYY-MM-dd"
第二行输入两个日期字符串,中间使用空格隔开。分别代表开始日期与结束日期。
输出格式:
如果第一行日期字符串非法,输出自定义的错误信息。
如果第一行日期有效,输出相关信息,如果是闰年要输出是闰年。
如果第二行两个日期,只要有一个无效。就输出相关错误信息。
如果第二行两个日期有效且结束日期不早于开始日期,输出相关信息。
输入样例1:
第一行日期非法、第二行有日期非法
2020-02-30
2020-02-30 2020-01-02
输出样例1:
2020-02-30无效!
2020-02-30或2020-01-02中有不合法的日期.
输入样例2:
均有效且合法
2021-02-28
2019-08-01 2020-01-02
输出样例2:
2021-02-28是当年第59天,当月第28天,当周第7天.
2020-01-02与2019-08-01之间相差154天,所在月份相差-7,所在年份相差1.
输入样例3:
日期均有效,但结束日期早于开始日期
2020-02-28
2020-02-02 2020-02-01
输出样例3:
2020-02-28是闰年.
2020-02-28是当年第59天,当月第28天,当周第5天.
2020-02-01早于2020-02-02,不合法!
我的答案
1 import java.time.LocalDate; 2 import java.time.Month; 3 import java.time.format.DateTimeFormatter; 4 import java.time.format.DateTimeParseException; 5 import java.time.format.ResolverStyle; 6 import java.time.temporal.ChronoUnit; 7 import java.util.Locale; 8 import java.util.Scanner; 9 import java.util.regex.Matcher; 10 import java.util.regex.Pattern; 11 12 public class Main { 13 14 public interface DateValidator { 15 boolean isValid(String dateStr); 16 } 17 18 public static class DateValidatorUsingDateTimeFormatter implements DateValidator { 19 private final DateTimeFormatter dateFormatter; 20 21 public DateValidatorUsingDateTimeFormatter(DateTimeFormatter dateFormatter) { 22 this.dateFormatter = dateFormatter; 23 } 24 25 //@Override 26 public boolean isValid(String dateStr) { 27 try { 28 this.dateFormatter.parse(dateStr); 29 } catch (DateTimeParseException e) { 30 return false; 31 } 32 return true; 33 } 34 } 35 36 37 38 public static void main(String[] args) { 39 // TODO 自动生成的方法存根 40 final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.CHINA); 41 final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 42 final DateTimeFormatter dateFormatter = DATE_FORMATTER.withResolverStyle(ResolverStyle.STRICT); 43 DateValidatorUsingDateTimeFormatter niuniUsingDateTimeFormatter= 44 new DateValidatorUsingDateTimeFormatter(dateFormatter); 45 46 Scanner inpScanner=new Scanner(System.in); 47 String ru1=inpScanner.nextLine(); 48 String ru2=inpScanner.next(); 49 String ru3=inpScanner.nextLine(); 50 ru1=ru1.trim(); 51 ru2=ru2.trim(); 52 ru3=ru3.trim(); 53 54 String pattern = "[0-9]{4}\\-(?:(01|03|05|07|09|11)(\\-0[1-9]|\\-[1-2]\\d|\\-3[0-1])|(02|04|06|08|10|12)(\\-0[1-9]|\\-[1-2]\\d|\\-30))"; 55 Pattern r1 = Pattern.compile(pattern); 56 Pattern r2 = Pattern.compile(pattern); 57 Pattern r3 = Pattern.compile(pattern); 58 Matcher m1 = r1.matcher(ru1); 59 Matcher m2 = r2.matcher(ru2); 60 Matcher m3 = r3.matcher(ru3); 61 62 63 if(ru1.length()!=10|m1.matches()==false) 64 {System.out.println(ru1+"无效!"); 65 } 66 else { 67 68 LocalDate date = LocalDate.parse(ru1,fmt); 69 if(niuniUsingDateTimeFormatter.isValid(ru1)==false) { 70 System.out.println(ru1+"无效!"); 71 } 72 else { 73 if(date.isLeapYear()) 74 System.out.println(date+"是闰年."); 75 System.out.println(date+"是当年第"+date.getDayOfYear()+ 76 "天,当月第"+date.getDayOfMonth()+"天,当周第"+date.getDayOfWeek().getValue() 77 +"天."); 78 } 79 } 80 if(ru2.length()!=10|m2.matches()==false|ru3.length()!=10|m3.matches()==false| 81 niuniUsingDateTimeFormatter.isValid(ru2)==false|niuniUsingDateTimeFormatter.isValid(ru3)==false) 82 {System.out.println(ru2+"或"+ru3+"中有不合法的日期."); 83 return;} 84 85 LocalDate chi1=LocalDate.parse(ru2,fmt); 86 LocalDate chi2=LocalDate.parse(ru3,fmt); 87 88 if(niuniUsingDateTimeFormatter.isValid(ru2)&niuniUsingDateTimeFormatter.isValid(ru3)) { 89 90 if(chi1.isBefore(chi2)) 91 92 {int year = chi2.getYear()-chi1.getYear(); 93 int month = chi2.getMonthValue()-chi1.getMonthValue(); 94 //int months = chi2.getMonthValue(); 95 long days = chi1.until(chi2, ChronoUnit.DAYS); 96 97 System.out.println(ru3+"与"+ru2+"之间相差"+days+ 98 "天,所在月份相差"+month+",所在年份相差"+year+"."); 99 } 100 else { 101 System.out.println(ru3+"早于"+ru2+",不合法!"); 102 } 103 104 } 105 106 107 108 //LocalDate newnewDate= 109 110 } 111 112 }

PTA题目集3
这里临近国庆中秋假期,所以老师只布置了4道题,其中3题是做过的,只有一道是新的,但这个新的好难,光代码要写到400行,而且超纲了,要求使用继承,还没学就去搞,我假期花了两天时间去做这道题,最后还是搞不定,也没有使用题目提供的参考类图去创作类,所以我坐大牢了(
7-2 课程成绩统计程序-1
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图:

输入样例1:
仅有课程。例如:
java 必修 考试
数据结构 选修 考试
形式与政治 选修 考察
end
输出样例1:
在这里给出相应的输出。例如:
java has no grades yet
数据结构 has no grades yet
形式与政治 has no grades yet
输入样例2:
单门考试课程 单个学生。例如:
java 必修 考试
20201103 张三 java 20 40
end
输出样例2:
在这里给出相应的输出。例如:
20201103 张三 34
java 20 40 34
202011 34
输入样例3:
单门考察课程 单个学生。例如:
java 选修 考察
20201103 张三 java 40
end
输出样例3:
在这里给出相应的输出。例如:
20201103 张三 40
java 40 40
202011 40
输入样例4:
考试课程 单个学生 不匹配的考核方式。例如:
java 必修 考试
20201103 张三 java 20
end
输出样例4:
在这里给出相应的输出。例如:
20201103 张三 : access mode mismatch
20201103 张三 did not take any exams
java has no grades yet
202011 has no grades yet
输入样例5:
单门课程,单个学生,课程类型与考核类型不匹配。例如:
java 必修 考察
20201103 张三 java 40
end
输出样例5:
在这里给出相应的输出。例如:
java : course type & access mode mismatch
java does not exist
20201103 张三 did not take any exams
202011 has no grades yet
输入样例6:
单门课程,多个学生。例如:
java 选修 考察
20201103 李四 java 60
20201104 王五 java 60
20201101 张三 java 40
end
输出样例6:
在这里给出相应的输出。例如:
20201101 张三 40
20201103 李四 60
20201104 王五 60
java 53 53
202011 53
输入样例7:
单门课程,单个学生,课程类型与考核类型不匹配。例如:
形式与政治 必修 考试
数据库 选修 考试
java 选修 考察
数据结构 选修 考察
20201103 李四 数据结构 70
20201103 李四 形式与政治 80 90
20201103 李四 java 60
20201103 李四 数据库 70 78
end
输出样例7:
在这里给出相应的输出。例如:
20201103 李四 73
java 60 60
数据结构 70 70
数据库 70 78 75
形式与政治 80 90 87
202011 73
输入样例8:
单门课程,单个学生,成绩越界。例如:
数据结构 选修 考察
20201103 李四 数据结构 101
end
输出样例8:
在这里给出相应的输出。例如:
wrong format
数据结构 has no grades yet
输入样例9:
多门课程,多个学生,多个成绩。例如:
形式与政治 必修 考试
数据库 选修 考试
java 选修 考察
数据结构 选修 考察
20201205 李四 数据结构 70
20201103 李四 形式与政治 80 90
20201102 王五 java 60
20201211 张三 数据库 70 78
end
输出样例9:
在这里给出相应的输出。例如:
20201102 王五 60
20201103 李四 87
20201205 李四 70
20201211 张三 75
java 60 60
数据结构 70 70
数据库 70 78 75
形式与政治 80 90 87
202011 73
202012 72
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class Main { 7 /* 8 * 课程类里实现输入规范 9 * 存入课程名字,考试还是考察 10 * 使用封装 11 */ 12 static class ke{ 13 //private String pattern = "[\\u4E00-\\u9FA5A-Za-z]{1,10} (必修( 考试)?|选修( 考察| 考试))"; 14 //private Pattern r = Pattern.compile(pattern); 15 private ArrayList<String> ru=new ArrayList<>(); 16 private int num=0,peacetime=0,finalexam=0,total=0; 17 void setnum() { 18 this.num+=1; 19 } 20 int getnum() { 21 return num; 22 } 23 void setpeacetime(int j) { 24 this.peacetime+=j; 25 } 26 void setpeacetimeplus(int j) { 27 this.peacetime=j; 28 } 29 int getpeacetime() { 30 return peacetime; 31 } 32 void setfinalexam(int j) { 33 this.finalexam+=j; 34 } 35 void setfinalexamplus(int j) { 36 this.finalexam=j; 37 } 38 int getfinalexam() { 39 return finalexam; 40 } 41 int getsize() { 42 return ru.size(); 43 } 44 void settotal() { 45 if(ru.get(2).equals("考试")||ru.get(1).equals("必修")) { 46 total=(int) ((double)0.3*peacetime+(double)0.7*finalexam);} 47 else { 48 total=finalexam; 49 } 50 } 51 int gettotal() { 52 return total; 53 } 54 55 void match(String str) { 56 57 for(String retval: str.split(" ")) { 58 ru.add(retval); 59 } 60 if(ru.get(1).equals("必修")&&ru.get(2).equals("考察")) { 61 ru.add("error"); 62 ru.set(1,"123"); 63 ru.set(2,"asd"); 64 System.out.println(ru.get(0)+" : course type & access mode mismatch"); 65 } 66 67 //return true; 68 } 69 70 String getcoursename() { 71 return ru.get(0); 72 } 73 String getway() { 74 if(ru.get(1).equals("必修")) 75 return "考试"; 76 else { 77 return ru.get(2); 78 } 79 } 80 } 81 /* 82 * 学生类输入规范 83 * 存入学生名字,学号,成绩 84 */ 85 public static class student{ 86 //private String pattern = "\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} (?:([0-9][0-9]?|100) ([0-9][0-9]?|100)|([0-9][0-9]?|100))\\b"; 87 //private Pattern r = Pattern.compile(pattern); 88 private ArrayList<String> ru=new ArrayList<>(); 89 private int num=0,stuexam=0; 90 String getnumber() { 91 return ru.get(0); 92 } 93 String getname() { 94 return ru.get(1); 95 } 96 void setnum() { 97 this.num+=1; 98 } 99 int getnum() { 100 return num; 101 } 102 void setstuexam(int j) { 103 this.stuexam+=j; 104 } 105 void setstuexamplus(int j) { 106 this.stuexam=j; 107 } 108 int getstuexam() { 109 return stuexam; 110 } 111 int getsize() { 112 return ru.size(); 113 } 114 void match(String str) { 115 for(String retval: str.split(" ")) { 116 ru.add(retval); 117 } 118 119 } 120 void err(String str) { 121 for(String retval: str.split(" ",3)) { 122 ru.add(retval); 123 } 124 } 125 126 String getcoursename() { 127 return ru.get(2); 128 } 129 int getgrade1() { 130 return Integer.parseInt(ru.get(3)); 131 } 132 int getgrade2() { 133 return Integer.parseInt(ru.get(4)); 134 } 135 } 136 137 public static class grade{ 138 139 static void stugrade(student stu,ke lesson) { 140 if(stu.getcoursename().equals(lesson.getcoursename())) { 141 if(lesson.getway().equals("考试")) { 142 if(stu.getsize()!=5) { 143 System.out.println(stu.getnumber()+" "+stu.getname()+" : access mode mismatch"); 144 //System.out.println(stu.getnumber()+" "+stu.getname()+" did not take any exams"); 145 return; 146 } 147 stu.setstuexam((int) ((double)0.3*stu.getgrade1()+(double)0.7*stu.getgrade2())); 148 lesson.setpeacetime(stu.getgrade1()); 149 lesson.setfinalexam(stu.getgrade2()); 150 stu.setnum();lesson.setnum(); 151 } 152 else if(lesson.getway().equals("考察")){ 153 if(stu.getsize()!=4) { 154 System.out.println(stu.getnumber()+" "+stu.getname()+" : access mode mismatch"); 155 156 return; 157 } 158 159 stu.setstuexam(stu.getgrade1()); 160 lesson.setfinalexam(stu.getgrade1()); 161 stu.setnum();lesson.setnum(); 162 } 163 else { 164 System.out.println(lesson.getcoursename()+" does not exist"); 165 } 166 } 167 168 } 169 170 } 171 public static class count{ 172 static void cal(int n,int m,student[] stu,ke[] lesson) { 173 for(int i=0;i<m;i++) { 174 for(int j=0;j<n;j++) { 175 176 grade.stugrade(stu[i],lesson[j]); 177 178 } 179 } 180 for(int i=0;i<n;i++) { 181 if(lesson[i].getnum()==0) 182 continue; 183 lesson[i].setfinalexamplus(lesson[i].getfinalexam()/lesson[i].getnum()); 184 lesson[i].setpeacetimeplus(lesson[i].getpeacetime()/lesson[i].getnum()); 185 lesson[i].settotal(); 186 187 } 188 189 } 190 } 191 192 public static class quchong{ 193 static void qu1(int m,student[] stu) { 194 String numberString=stu[0].getnumber(); 195 String nameString=stu[0].getname(); 196 int ins=0; 197 if(stu[0].getnum()!=0) 198 ins=stu[0].getstuexam()/stu[0].getnum(); 199 200 int num=0; 201 for(int i=1;i<m;i++) { 202 if(numberString.equals(stu[i].getnumber())) { 203 ins+=stu[i].getstuexam()/stu[i].getnum(); 204 num++; 205 206 }else { 207 if(ins==0) { 208 System.out.println(stu[i].getnumber()+" "+stu[i].getname()+" did not take any exams"); 209 }else { 210 System.out.println(numberString+" "+nameString+" "+ins/(num+1)); 211 } 212 num=0; 213 ins=stu[i].getstuexam()/stu[i].getnum(); 214 numberString=stu[i].getnumber(); 215 nameString=stu[i].getname(); 216 } 217 }if(num!=0||m==1||num==0) { 218 if(ins==0) { 219 System.out.println(numberString+" "+nameString+" did not take any exams"); 220 }else { 221 System.out.println(numberString+" "+nameString+" "+ins/(num+1)); 222 } 223 224 } 225 226 227 } 228 } 229 230 public static class xu{ 231 static void xu1(int m,student[] stu) { 232 for(int i=0;i<m;i++) { 233 for(int j=0;j<m;j++) { 234 if(Integer.parseInt(stu[i].getnumber())<Integer.parseInt(stu[j].getnumber())) { 235 student temp=new student(); 236 temp=stu[i]; 237 stu[i]=stu[j]; 238 stu[j]=temp; 239 } 240 } 241 } 242 quchong.qu1(m, stu); 243 } 244 245 static void xu2(int n,ke[] lesson) { 246 247 for(int i=0;i<n;i++) { 248 for(int j=0;j<n;j++) { 249 if(lesson[j].gettotal()==0&&lesson[j].getsize()==3) { 250 System.out.println(lesson[j].getcoursename()+" has no grades yet"); 251 continue; 252 } 253 if(lesson[i].gettotal()<lesson[j].gettotal()) { 254 ke temp1=new ke(); 255 temp1=lesson[i]; 256 lesson[i]=lesson[j]; 257 lesson[j]=temp1; 258 } 259 } 260 } 261 for(int i=0;i<n;i++) { 262 if(lesson[i].getway().equals("考试")&&lesson[i].gettotal()!=0) { 263 264 System.out.println(lesson[i].getcoursename()+" "+lesson[i].getpeacetime()+" "+lesson[i].getfinalexam()+" "+lesson[i].gettotal()); 265 266 } 267 else if(lesson[i].gettotal()!=0){ 268 System.out.println(lesson[i].getcoursename()+" "+lesson[i].getfinalexam()+" "+lesson[i].gettotal()); 269 } 270 } 271 } 272 } 273 274 public static class chu{ 275 static void niuzi(int m,student[] stu) { 276 String numberString=stu[0].getnumber().substring(0,6); 277 int ins=0; 278 if(stu[0].getnum()!=0) 279 ins=stu[0].getstuexam()/stu[0].getnum(); 280 281 int num=0; 282 for(int i=1;i<m;i++) { 283 if(numberString.equals(stu[i].getnumber().substring(0,6))) { 284 ins+=stu[i].getstuexam()/stu[i].getnum(); 285 num++; 286 }else { 287 if(ins==0) { 288 System.out.println(numberString+" has no grades yet"); 289 }else { 290 System.out.println(numberString+" "+(ins/(num+1))); 291 } 292 num=0; 293 ins=stu[i].getstuexam()/stu[i].getnum(); 294 numberString=stu[i].getnumber().substring(0,6); 295 } 296 } 297 if(num!=0||m==1||num==0) { 298 if(ins==0) { 299 System.out.println(numberString+" has no grades yet"); 300 }else { 301 System.out.println(numberString+" "+(ins/(num+1))); 302 } 303 304 } 305 306 } 307 } 308 309 310 311 public static void main(String[] args) { 312 Scanner inScanner=new Scanner(System.in); 313 ArrayList<String> ru=new ArrayList<>(); 314 int n=0,m=0; 315 student[] stu=new student[50]; 316 ke[] lesson=new ke[50]; 317 String str=""; 318 319 do { 320 str=inScanner.nextLine(); 321 ru.add(str); 322 }while(!str.equals("end")); 323 String pattern1 = "\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} ([0-9][0-9]?|100)( [0-9][0-9]?| 100)?\\b"; 324 String pattern2 = "[\\u4E00-\\u9FA5A-Za-z]{1,10} (必修( 考察| 考试)?|选修( 考察| 考试))"; 325 Pattern r1 = Pattern.compile(pattern1); 326 Pattern r2 = Pattern.compile(pattern2); 327 for(int i=0;i<ru.size();i++) { 328 329 Matcher pi1 = r1.matcher(ru.get(i)); 330 Matcher pi2 = r2.matcher(ru.get(i)); 331 if(pi2.matches()) { 332 lesson[n]=new ke(); 333 lesson[n].match(ru.get(i)); 334 n++; 335 } 336 if(pi1.matches()) { 337 stu[m]=new student(); 338 stu[m].match(ru.get(i)); 339 m++; 340 } 341 else if(ru.get(i).equals("end")){ 342 break; 343 }else if(!pi1.matches()&&!pi2.matches()){ 344 //stu[m]=new student(); 345 //stu[m].err(ru.get(i)); 346 //m++; 347 System.out.println("wrong format"); 348 349 } 350 351 } 352 if(m==0) { 353 for(int i=0;i<n;i++) { 354 System.out.println(lesson[i].getcoursename()+" "+"has no grades yet"); 355 } 356 }else { 357 count.cal(n,m,stu,lesson); 358 xu.xu1(m,stu); 359 xu.xu2(n,lesson); 360 chu.niuzi(m, stu); 361 } 362 363 364 //System.out.println(); 365 //System.out.println(grade.finalgrade1/grade.kao); 366 } 367 368 369 }

PTA题目集1/2总结
浙公网安备 33010602011771号