【发布时间】:2025-12-27 03:55:06
【问题描述】:
我想知道 codechef 编译器是如何工作的。实际上,不仅 codechef 编译器,其他竞争平台的编译器如何在后端工作。因为我过去在本地编译器上获得的输出有时与专门在 codechef 上的在线编译器不同。
例如,在我的本地编译器上,当我运行以下代码时,我得到了正确的输出:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
int val, N, Z, count = 0, evacuate = 0;
vector<int> v;
cin >> N >> Z;
for (int i = 0; i < N; i++)
{
cin >> val;
v.push_back(val);
}
std::make_heap(v.begin(), v.end());
while (Z > 0)
{
int power = v.front();
Z = Z - power;
std::pop_heap(v.begin(), v.end());
v.pop_back();
power = power / 2;
if (power)
{
v.push_back(power);
std::push_heap(v.begin(), v.end());
}
else if(power == 0 && Z > 0){
evacuate = 1;
break;
}
if (Z < 0)
{
count++;
break;
}
count++;
}
if (evacuate)
cout << "Evacuate" << endl;
else
cout << count << endl;
}
}
当我在 codechef 编译器上运行相同的代码来解决 codechef problem 时,我得到了意外的输出(只有 0)
另外,cpp.sh 上的代码。
CodeChef 编译器如何工作以及两者的输出有何不同?可能问题也可能出在代码上,但在本地,我正在为手动提供的输入获得正确的输出。
【问题讨论】:
-
CodeChef 使用的编译器可以在这里找到:codechef.com/wiki/list-compilers
-
哦,那我应该使用与 codechef 相同的编译器而不是 Visual Studio 或其他任何编译器吗?在否决问题之前,您是否研究过完整的问题?
-
不用担心使用相同的编译器。如果您不使用任何高级语言功能,并且您的代码没有 UB,那么任何最新的编译器都应该为您提供相同的结果。
-
@HolyBlackCat 请注意 gcc 6.3 有点旧,仅部分支持 c++17。不支持
string_view、gcd或to_chars之类的内容。
标签: compiler-construction c++14