g++的編譯器真強大

lt發表於2016-11-25
#include <cstdio>
#include <ctime>
#define NN 39000000
int c=0;

int t[NN+1]={0};
int getsteps2(long long n)
{
int nn=n;
int i=1;
for(;n!=1;i++)
{
long long temp=((n &1) ==0)?(n/2):(n*3+1); //3.510 2M
if (temp<nn)// && temp>0)
{
int len= t[temp];
if (len!=0)
{//c++;printf("%d:%d ",nn,temp);
   return (i+len);}
}
n=temp;
}
return i;
}
int main()
{
int maxstep=1;
int maxpos=1;
int n=2;
t[1]=1;
int tm=clock();
for(;n<=NN;n++)
{
int step=t[n/2]+1;
t[n]=step;
if (step>maxstep){
maxstep=step;
maxpos=n;
}
n++;
step=getsteps2(n);
t[n]=step;
if (step>maxstep){
maxstep=step;
maxpos=n;
}
}
printf("%dms\n",clock()-tm);
printf("maxstep=%d ,at %d,caches%d\n",maxstep,maxpos,c);
return 1;
}                                                       

報錯

D:\>g++ p14t2.cpp -O2
p14t2.cpp: In function 'int main()':
p14t2.cpp:42:10: warning: iteration 19499999u invokes undefined behavior [-Waggressive-loop-optimizations]
 t[n]=step;
          ^
p14t2.cpp:32:7: note: containing loop
 for(;n<=NN;n++)
       ^

按照提示把<=N改為< N就不報錯了。

相關文章