upgrade to python3;add some validator examples
This commit is contained in:
68
examples/range_validator.cpp
Normal file
68
examples/range_validator.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// 数值范围验证程序
|
||||
// 验证输出的数值是否在合理范围内
|
||||
// 编译: g++ -o range_validator range_validator.cpp
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 4) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ifstream input(argv[1]); // 输入数据,包含范围信息
|
||||
ifstream expected(argv[2]); // 期望答案
|
||||
ifstream result(argv[3]); // 用户输出
|
||||
|
||||
if (!input.is_open() || !expected.is_open() || !result.is_open()) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 读取输入数据中的范围信息
|
||||
int n;
|
||||
input >> n;
|
||||
vector<int> arr(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
input >> arr[i];
|
||||
}
|
||||
|
||||
int min_val = *min_element(arr.begin(), arr.end());
|
||||
int max_val = *max_element(arr.begin(), arr.end());
|
||||
|
||||
// 读取期望答案
|
||||
int exp_result;
|
||||
if (!(expected >> exp_result)) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 读取用户答案
|
||||
int user_result;
|
||||
if (!(result >> user_result)) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 验证答案是否在合理范围内
|
||||
if (user_result < min_val || user_result > max_val) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 验证答案是否正确
|
||||
if (user_result == exp_result) {
|
||||
cout << "AC" << endl;
|
||||
} else {
|
||||
cout << "WA" << endl;
|
||||
}
|
||||
|
||||
input.close();
|
||||
expected.close();
|
||||
result.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user