#include
#include
#include
int add1(long n) {
long i, sum = 0;
for (i = 0; i <= n; i++) {
sum+=i;
}
return sum;
}
void AddTime1(long n) {
clock_t t;
t = clock();
long sum;
sum = add1(n);
t = clock() - t;
printf("方法一:\n");
printf("用時為:%lf\n", ((float)t)/CLOCKS_PER_SEC);
printf("結果為:從1-%ld的和是%ld\n", n, sum);
}
int add2(long n) {
long sum;
return n * (n + 1)/ 2;
}
void AddTime2(long n) {
clock_t t;
t = clock();
long sum;
sum = add2(n);
t = clock() - t;
printf("方法二:\n");
printf("用時為:%lf\n", ((float)t) / CLOCKS_PER_SEC);
printf("結果為:從1-%ld的和是%ld\n", n, sum);
}
int main() {
int n;
printf("n(大于1000000):");
scanf_s("%d", &n);
if (n < 1000000)return 0;
AddTime1(n);
AddTime2(n);
return 1;
}