gpt4 book ai didi

c++ - 减少以下代码中的时间

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:46 26 4
gpt4 key购买 nike

我在 codechef 上针对问题代码给出了这个解决方案:FCTRL。

我看到其他人使用相同的语言c(我使用的是c++ gcc 4.8.1)的编译时间有点少,我的是0.46s,他们的是0.23

如果可能,有人可以帮我缩短时间吗?

#include<iostream>
using namespace std;
int main()
{
long int t,i,temp;
cin>>t;
long int n[t],a[t];
for(i=0;i<t;i++)
{
temp=1;
a[i]=0;
cin>>n[i];
while(temp)
{
temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;
}
}
for(i=0;i<t;i++)
cout<<a[i]<<"\n";

return(0);
}

最佳答案

根据您的描述,您使用的是 C++ 而他们使用的是 C,这可能是由于编译器如何处理每条指令。

你也可以尝试更换

temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;

通过

temp=n[i]/5;
a[i]+=temp;
n[i]=temp; //why compute the value again

看看有没有时间减少

关于c++ - 减少以下代码中的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18370520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com