Home > Back-end >  A C language problem
A C language problem

Time:10-05

The code is as follows:
 
#include
#include
#include
Long add1 (n)
{
Long I, a=0;
for(i=1; i<=n; I++)
A=a + I;
Return a;
}
Void AddTime1 (n)
{
Clock_t t;
Long sum=0;
T=clock ();
Sum=add1 (n);
T=clock () - t;
Methods: printf (" \ n ");
Printf (" results: the sum of 1 ~ % d: % ld \ n ", n, sum);
Printf (" available: lf % s \ n ", (t) (float)/CLOCKS_PER_SEC);
}

Long add2 (n)
{
Return n * (n + 1)/2;
}
Void AddTime2 (n)
{
Clock_t t;
Long sum;
T=clock ();
Sum=add2 (n);
T=clock () - t;
Method 2: printf (" \ n ");
Printf (" results: the sum of 1 ~ % d: % ld \ n ", n, sum);
Printf (" available: lf % s \ n ", (t) (float)/CLOCKS_PER_SEC);
}

Int main ()
{
int n;
Printf (" n (greater than 1000000) : ");
The scanf (" % d ", & amp; N);
If (n<1000000) return 0;
AddTime1 (n);
AddTime2 (n);
return 1;
}


Why does it have to be input method is one of the 9999999 and negative
Input method 2 1000001 and is negative
The input 99999999 two methods are to


CodePudding user response:

You see your editor long type and the scope of type int, generally as int and long range, you try with the following statement,

Printf (" % d \ n ", __INT_MAX__);
Printf (" % ld \ n ", __LONG_MAX__);
Printf (" % LLD \ n ", __LONG_LONG_MAX__);

Two methods from the perspective of the output of your apparent results than the int the maximum range, long long should instead,

CodePudding user response:

reference 1/f, 5250 response:
you see your editor long type and the scope of type int, generally int and long range when the same, you try with the following statement,

Printf (" % d \ n ", __INT_MAX__);
Printf (" % ld \ n ", __LONG_MAX__);
Printf (" % LLD \ n ", __LONG_LONG_MAX__);

From the point of the output of your apparent when the results of the two methods is more than the int the largest scope, long long should be ok, instead of
well, after I watched it again, is the long range is too small to overflow, for long long
  • Related