upgrade to python3;add some validator examples
This commit is contained in:
69
examples/StringFormatValidator.java
Normal file
69
examples/StringFormatValidator.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 字符串格式验证程序
|
||||
* 验证输出是否符合特定格式要求
|
||||
* 编译: javac StringFormatValidator.java
|
||||
* 使用: java StringFormatValidator input.txt expected.txt result.txt
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class StringFormatValidator {
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 3) {
|
||||
System.out.println("WA");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 读取期望的行数和格式信息
|
||||
Scanner expected = new Scanner(new File(args[1]));
|
||||
Scanner result = new Scanner(new File(args[2]));
|
||||
|
||||
// 假设期望文件第一行是期望的行数
|
||||
int expectedLines = expected.nextInt();
|
||||
expected.nextLine(); // 消费换行符
|
||||
|
||||
// 读取用户输出
|
||||
List<String> userLines = new ArrayList<>();
|
||||
while (result.hasNextLine()) {
|
||||
String line = result.nextLine().trim();
|
||||
if (!line.isEmpty()) {
|
||||
userLines.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查行数
|
||||
if (userLines.size() != expectedLines) {
|
||||
System.out.println("WA");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查每行格式
|
||||
boolean formatCorrect = true;
|
||||
for (int i = 0; i < userLines.size(); i++) {
|
||||
String line = userLines.get(i);
|
||||
|
||||
// 检查是否符合 "Case #X: Y" 格式
|
||||
Pattern pattern = Pattern.compile("^Case #" + (i + 1) + ": \\d+$");
|
||||
if (!pattern.matcher(line).matches()) {
|
||||
formatCorrect = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (formatCorrect) {
|
||||
System.out.println("AC");
|
||||
} else {
|
||||
System.out.println("PE"); // 格式错误
|
||||
}
|
||||
|
||||
expected.close();
|
||||
result.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("WA");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user