全局变量在 C++ 中是否显着提高了编译速度?

Does global variable increase speed of compilation significantly in c++?

本文关键字:编译 速度 C++ 是否 全局变量      更新时间:2023-10-16

对于一个问题

https://codeforces.com/problemset/problem/760/B

当我提交在 int main(( 中具有声明的解决方案时,它显示 TLE,但是当声明高于 int main(( 时,它在 C++ 中被接受。

所以我的问题是全局声明是否显着影响编译速度或我错过了什么?

这是公认的:

#include<bits/stdc++.h>
using namespace std;
int n,k,m,a=1,c=1;
int main()
{
cin>>n>>m>>k;
m -= n;
while (m>0){
if (k+a<=n) c++;
if (k-a>=1) c++;
m -=c;
a++;
}
cout<<a;
return 0;
}

这是TLE一:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k,m,a=1,c=1;
cin>>n>>m>>k;
m -= n;
while (m>0){
if (k+a<=n) c++;
if (k-a>=1) c++;
m -=c;
a++;
}
cout<<a;
return 0;
}

应该没有区别。 如果您的样本量很小(您提交了一两次解决方案(,也许您处于超出时间限制与未超出时间限制的边界,有时只是很幸运。