注意:这是针对 usaco.org 的问题 teleportation (因此奇怪的变量名称,我显然不是在制作实际的传送器)
当我尝试使用 (2, 8) 的内置最大值时,由于某种原因我得到 2。我还创建了自己的函数,如下所示:
#define min(number_1, number_2) (number_1 < number_2 ? number_1 : number_2)
#define max(number_1, number_2) (number_1 < number_2 ? number_2 : number_1)
但仍然失败。这是代码:
#include <iostream>
#include <string>
using namespace std;
#define min(number_1, number_2) (number_1 < number_2 ? number_1 : number_2)
#define max(number_1, number_2) (number_1 < number_2 ? number_2 : number_1)
int main() {
int start_position, end_position;
int teleportation_start_position, teleportation_end_position;
cin >> start_position >> end_position >> teleportation_start_position >> teleportation_end_position;
cout << start_position << " " << end_position << " " << teleportation_start_position << " " << teleportation_end_position << "\n";
start_position = min(start_position, end_position);
end_position = max(start_position, end_position);
teleportation_start_position = min(teleportation_start_position, teleportation_end_position);
teleportation_end_position = max(teleportation_start_position, teleportation_end_position);
cout << start_position << " " << end_position << " " << teleportation_start_position << " " << teleportation_end_position << "\n";
}
我得到了
的输入3 10 8 2
我明白了
3 10 8 2
3 10 2 2
为什么是2 2?
当我删除我定义的东西(#define)后仍然不起作用。 我只插入了一个代码片段。
注#2:我是 C++ 新手,主要有 Python 习惯。
错误是您正在重复使用
teleportation_start_position
变量。
teleportation_start_position = min(teleportation_start_position, teleportation_end_position);
在此行之前,程序的状态是:
teleportation_start_position
= 8teleportation_end_position
= 2但是之后,是:
teleportation_start_position
= 2teleportation_end_position
= 2所以在下一行中你正在做:
teleportation_end_position = max(teleportation_start_position, teleportation_end_position);
// teleportation_end_position = max(2, 2);