社区讨论
调交互题交互库与SPJ要调到炸了
学术版参与者 3已保存回复 20
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 20 条
- 当前快照
- 1 份
- 快照标识符
- @mkrq7pbu
- 此快照首次捕获于
- 2026/01/24 11:04 4 周前
- 此快照最后确认于
- 2026/01/24 18:28 4 周前
依旧RT。依旧未归紫衫。
我有一道题目需要一个特殊的评测机制。所以我搞了交互。但是本蒟蒻调这些代码调到炸了还是没找出为何频频爆出
wrong answer Round 1: No operation output的原因。交互库代码(请AI调了,还是没用)
CPP#include <iostream>
#include <cstdio>
// 选手函数声明
extern "C" char in_time(int a);
int main() {
// 禁用C++标准流的同步,提高性能
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int obstacle_pos;
// 循环读取SPJ发送的障碍物坐标
while (std::cin >> obstacle_pos) {
// 调用选手函数
char operation = in_time(obstacle_pos);
// 将选手的操作发送给SPJ
std::cout << operation << std::endl;
// 强制刷新输出缓冲区
// 这是关键!确保SPJ能立即收到选手的操作
std::cout.flush();
}
return 0;
}
SPJ代码(同上)
CPP#include "testlib.h"
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char* argv[]) {
// 使用registerInteraction而不是registerTestlibCmd
registerInteraction(argc, argv);
// 从输入文件中读取参数
int min_rounds = inf.readInt(); // 最小回合数
int max_rounds = inf.readInt(); // 最大回合数
int min_obstacle = inf.readInt(); // 障碍物最小坐标
int max_obstacle = inf.readInt(); // 障碍物最大坐标
// 随机生成实际回合数
int total_rounds = rnd.next(min_rounds, max_rounds);
int robot_pos = 0; // 机器人初始位置
for (int round = 1; round <= total_rounds; round++) {
// 生成随机障碍物位置
int obstacle_pos = rnd.next(min_obstacle, max_obstacle);
// 发送障碍物位置给选手程序(使用tout)
tout << obstacle_pos << endl;
tout.flush();
// 读取选手操作
char op;
while (true) {
if (ouf.eof()) {
quitf(_wa, "Round %d: No operation output", round);
}
op = ouf.readChar();
if (!isspace(op)) break;
}
// 验证操作合法性
if (op != 'L' && op != 'R' && op != 'N') {
quitf(_wa, "Round %d: Invalid operation '%c'", round, op);
}
// 更新机器人位置
if (op == 'L') {
robot_pos--;
} else if (op == 'R') {
robot_pos++;
}
// 'N' 表示不动,位置不变
// 检查碰撞
if (robot_pos == obstacle_pos) {
quitf(_wa, "Round %d: Robot hit obstacle at position %d",
round, robot_pos);
}
}
// 发送通关标志
tout << 1369800 << endl;
tout.flush();
// 读取最终操作
char final_op;
while (true) {
if (ouf.eof()) {
quitf(_wa, "End round: No operation output after end flag");
}
final_op = ouf.readChar();
if (!isspace(final_op)) break;
}
// 验证最终操作合法性
if (final_op != 'L' && final_op != 'R' && final_op != 'N') {
quitf(_wa, "End round: Invalid operation '%c'", final_op);
}
// 通关成功
quitf(_ok, "Success! Completed %d rounds.", total_rounds);
}
我个人认为没啥问题,但本地编译也是错的。求条。
回复
共 20 条回复,欢迎继续交流。
正在加载回复...