PTA题目集总结2
第四次:
难度:因为前面几题没做好,本题没完成好,只知道中途遇到哪些问题,最后结果,空。
题量:这次最后一道题在上次的基础上添加了题目的种类( eg: 选择题、填空题 )等
知识点:类设计,字符串处理等基础语法,正则表达式。
————————————————————————设计与分析——————————————————————————————————
类设计思路:
1~题目类(单个题目的信息):
属性:题目编号、题目内容、标准答案-standardAnswer
主要方法:
判题方法(答案-answer:判断答案-answer是否符合标准答案-standardAnswer)
2~试卷类(一套题目的信息)
属性:题目列表(题目类的对象集合,数组)、题目数量
主要方法:
@1判题方法(题号-num、答案-answer):判断答案-answer是否符合对应题号的题目标准答案-standardAnswer。
分析:这里判题方法进一步封装,调用题目类的判题方法
@2保存题目(题号-num、题目-question):将题目保存到题目列表中,保存位置与num要能对应
3~答卷类(用于封装答题信息)
属性:试卷(试卷类的对象)、答案列表(保存每一题的考生答案)、判题列表(保存每一题的判题结果true/false)
主要方法:判题方法(题号-num):判断答案列表中第num题的结果是否符合试卷中对应题号的题目标准答案
输出方法(题号-num):按照题目的格式要求,输出题号为num的题目的内容和答题结果。
保存一个答案(题号-num,答案-answer):保存题号为num的题目的答题结果answer。
————————————————————————踩坑心得————————————————————————————————————
两大坑:
1,数组的实例化
questions=new Questions[n];
和数组元素的实例化
for (int i = 0; i < n; i++) { //!!! 实例化数组中的每个元素
questions[i] = new Questions();
2,数组元素下标访问的越界问题:
因为题目数组questions[0]存的是第一题,所以所有对题目的操作要注意下标减一。
比如 以下对num修正
public void saveQuestion(int num,String question,String standAnswer){
num-=1;
questions[num].setNum(num);
questions[num].setQuestion(question);
questions[num].setStandAnswer(standAnswer);
}
其他:
正则表达式运用不好,可以用网上在线工具实时测试表达式是否正确(如菜鸟工具https://www.jyshare.com/front-end/854/)
————————————————————————改进建议————————————————————————————————————
因为已知题目数量,所以用数组,和变量n记录。
这里分析以后更贴近真实情况应该改用arraylist更好
第五次:
难度::考察类间关系的设计。
类设计:
因为没有电阻,本题我是讲开始的220v经过每个设备都有一个 public abstract void setInputVoltage(double voltage);方法
通过输入电压计算并更新输出电压。
然后再在电路类运行每个设备的setinputvoltage方法
public void CircuitWork() {
final double initialVoltage=220;
double outputVoltage = initialVoltage;
for (Device device : devices) {
device.setInputVoltage(outputVoltage);
outputVoltage = device.getOutputVoltage(); // 从上一个设备获取输出电压作为下一个设备的输入电压
}
}
如此,对于一条串联电路,可以算出每个设备的电压差,最后用共同的方法public void printWorkState() 来输出状态
在电路类里有总的输出状态方法public void printAllWorkStates() {
for (Device device : devices) {
device.printWorkState();
}
}
这是我的设计没有用到引脚,main函数如下
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circuit circuit = new Circuit();
ArrayList
HashMap<String,Device> searchDevice = new HashMap<>();
// 匹配输入信息
Pattern p1 = Pattern.compile("((\\w)\\d)-\\d");
Pattern p2 = Pattern.compile("#((\\w)\\d)(.+)");
Pattern p3 = Pattern.compile("#((\\w)\\d):(.+)");
while (sc.hasNext()) {
String line = sc.nextLine();
if (line.equals("end")) {
break;
}
//compile("((\\w)\\d)-\\d");
Matcher m1 = p1.matcher(line);
if (m1.find()) {
char m = m1.group(2).charAt(0);
String name = m1.group(1);
switch(m) {
case 'L':if(!orderList.contains(name))
{orderList.add( name);
searchDevice.put(name,new Control_L(name));}
break;
case 'K':if(!orderList.contains(name)) {
orderList.add(name);
searchDevice.put(name,new Switch(name));}
break;
case 'F':if(!orderList.contains(name)) {
orderList.add(name);
searchDevice.put(name, new Control_F(name));}
break;
case 'B':if(!orderList.contains(name)) {
orderList.add(name);
searchDevice.put(name,new Controled_B(name));}
break;
case 'D':if(!orderList.contains(name)) {orderList.add(name);
searchDevice.put(name, new Controled_D(name));}
break;
case 'R':if(!orderList.contains(name)) {orderList.add(name);
searchDevice.put(name, new Controled_R(name));}
break;
}}
Matcher m2 = p2.matcher(line);
//"#((\\w)\\d)(.)|#((\\w)\\d)");
if(m2.matches()) {
if(m2.group(3).equals("+"))
((Control_F) searchDevice.get(m2.group(1))).upShiftGear();
else
((Control_F) searchDevice.get(m2.group(1))).downShiftGear();
}
//"#((\\w)\\d):(.+)");
Matcher m3 = p3.matcher(line);
if(m3.matches()) {
((Control_L) searchDevice.get(m3.group(1))).setRatio(Double.parseDouble(m3.group(3)));
}
Pattern p4 = Pattern.compile("#((\\w)\\d)");
Matcher m4 = p4.matcher(line);
if(m4.matches()) {
((Switch) searchDevice.get(m4.group(1))).changeState();
}}
for(int i=0;i<orderList.size();i++)
circuit.addDevice(searchDevice.get(orderList.get(i)));
circuit.CircuitWork();
circuit.printAllWorkStates();
}
}
第六次:
————————————————————————设计与分析——————————————————————————————————
难度::考察类间关系的设计。
类设计:一直在考虑物理的分压这些怎么实现
这是我的电路的设计,我的串联并联,受控,控制这些都是设备device的子类
import java.util.*;
public class Circuit extends Device{
//串联电路
private String name;
private int RofAll;
public int getRofAll() {
return RofAll;
}
public void setRofAll() {
for()
RofAll = rofAll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private List<Device> devices;
public Circuit() {
devices = new ArrayList<>();
}
public Circuit(String name ) {
devices = new ArrayList<>();
this.name = name;
}
public void addDevice(Device device) {
devices.add(device);
}
public void removeDevice(Device device) {
devices.remove(device);
}
public void CircuitWork(double I) {
for (Device device : devices) {
device.work(I);
}
}
public void printAllWorkStates() {
for (Device device : devices) {
device.printWorkState();
}
}
@Override
public void printWorkState() {
// TODO 自动生成的方法存根
}
@Override
public void work(double I) {
// TODO 自动生成的方法存根
}
}
以下是main方法import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//所有用电器集合
HashMap<String,Device> searchDevice = new HashMap<>();
//串联顺序
HashMap<String, ArrayList
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.equals("end")) {
break; }
else {
//#T1 加串联线路
Pattern p2 = Pattern.compile("#(.{2})\\[");
Matcher m2 = p2.matcher(line);
if(m2.find()) {
String T_name=m2.group(1);
if(!searchDevice.containsKey(T_name)) {
searchDevice.put(T_name, new Circuit(T_name));
}
//K1-1
ArrayList<String> order=new ArrayList<>();
Pattern p1 = Pattern.compile("(.{2})-\\d");
Matcher m1 = p1.matcher(line);
if(m1.find()) {
String name = m1.group(1);
char m = name.charAt(0);
switch(m) {
case 'L':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Control_L(name));
order.add(name);
}
break;
case 'K':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Control_Switch(name));
order.add(name);
}
break;
case 'F':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Control_F(name));
order.add(name);
}
break;
case 'B':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Controled_B(name));
order.add(name);
}
break;
case 'D':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Controled_D(name));
order.add(name);}
break;
case 'R':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Controled_R(name));
order.add(name);}
break;
case 'A':if(!searchDevice.containsKey(name)) {
searchDevice.put(name, new Controled_A(name));
order.add(name);}
break;
}
map.put(T_name, order);
}
}
//调节信息
if(!line.contains("[")) {
//#K ,#L1:1.00调节 完全匹配?
Pattern p3 = Pattern.compile("#((.)\\d):?(.*)");
Matcher m3 = p3.matcher(line);
char ch=m3.group(2).charAt(0);
String name3=m3.group(1);
String operate=m3.group(3);
if(ch=='K') {
((Control_Switch) searchDevice.get(name3)).changeState();
}
if(ch=='F') {
if(operate=="+")
((Control_F) searchDevice.get(name3)).upShiftGear();
else ((Control_F) searchDevice.get(name3)).downShiftGear();
}
if(ch=='L') {
((Control_L) searchDevice.get(name3)).setRatio(Double.parseDouble(operate));
}
}
//并联信息
if(line.startsWith("#M")) {
String[] parts1 = input.split(":");
String[] parts2 = parts1[0].split("#");
String nameOfMultipleCircuit = parts2[1];
String[] parts3 = parts1[1].split("[\\[\\]]");
String[] parts4 = parts3[0].split(" ");
MultipleCircuit newMultiple = new MultipleCircuit(nameOfMultipleCircuit);
newMultiple.add
searchDevice.put(nameOfMultipleCircuit, );
}
}
}
}
}
————————————————————————踩坑心得————————————————————————————————————
对于答题判断题:遇到选择题答案与顺序无关,比如答案ABCD输入DCBA也是对的;
对于电路题:@一开始,这里的连接管理我看不懂,我现在实现了一个个设备的电压管理和工作方法等,但是我不知道怎么放进电路中怎么连接,一开始是220v的电压我怎么知道经过一个设备后变成多少电压,我怎么知道电压差是多大,在知道连接情况下,你上面的代码思路是怎样的?
当时的回答:
设备的输入电压和输出电压:每个设备都有一个输入电压和输出电压的属性。当连接两个设备时,前一个设备的输出电压将是后一个设备的输入电压。
计算电压变化:设备之间的电压变化取决于设备本身的特性,比如开关可能会将输入电压完全传递给输出,而灯泡可能会根据输入电压产生不同的亮度。
串联连接:通过设备之间的串联连接,可以模拟整个电路的工作过程。起始设备的输出电压会传递到下一个设备的输入电压,依次类推,直到整个电路被连接完成。
@遇到问题分档调节分别是0,1,2,3档,而不是,2,3,4档;
————————————————————————改进建议————————————————————————————————————
我觉得可能第一次的电路题可以修改运用第二次的思路,和设计,因为我的第二次的考虑的情况更多。
——————————————————————————总结————————————————————————————————————————
从答题判断中多是学习到异常情况的处理,并且总在用正则表达式和格式较劲,一直在处理输入输出。
从这2次pta中学到了复杂一些的类的设计,以及明白要设计做好了要多花时间去实施
浙公网安备 33010602011771号