CodePudding user response:
This is not a O (N) is ok?int main () {
Int I, n, sum=0, FLG=1;
The scanf (" % d ", & amp; N);
For (I=1; I<=n; I++) {
The sum +=FLG * I;
FLG *=1;
}
Printf (" % d \ n ", sum);
return 0;
}
CodePudding user response:
Obviously every two to 1, until N is N/2 time complexity O (1)CodePudding user response:
Of course, if you use the formula directly, O (1) canint main () {
Int I, n, sum=0, FLG=1;
The scanf (" % d ", & amp; N);
Sum=(1) * (n/2) + (n % 2==0? 0, n);//by skills, each is the sum of the two - 1, whether n is even, if it is an even number is 1 * n/2, otherwise again + n
Printf (" % d \ n ", sum);
return 0;
}
CodePudding user response:
The O (1) direct formula1-2=1;
3-4=1;
,,,,,,
If N is an even number is equal to 1 * N/2
An odd number N + (N/2) * (1)
CodePudding user response:
Direct general term formula resultsTime complexity O (1)
finished