#include
#include
void main ()
{
clock_t start1,finish1,start2,finish2;
double a,b;
int i,j,k,n,m,p,q;
int sum1=0,sum2=0;
scanf("%d",&n);
for (k=1;k<=n;k*=2)
{
for (j=1;j<=n;j++)
sum1++;
for(j=1;j<=k;j++)
sum2++;
}
start1=clock();
for(k=1;k<=n;k*=2)
for (j=1;j<=n;j++)
finish1=clock();
a=(double)(finish1 - start1) / CLOCKS_PER_SEC;
start2=clock();
for(k=1;k<=n;k*=2)
for(j=1;j<=k;j++)
finish2=(double)clock() / CLOCKS_PER_SEC;
printf("sum1等於:%d\n sum2等於;%d\n sum1所用時間為;%lf\n sum2所用時間為;%lf\n",sum1,sum2,a,b);
system("pause");
}
後面程序應該是
finish2=clock();
b=(double)(finish2-start2)/ CLOCKS_PER_SEC;