diff --git "a/210920142/chapter1/\347\254\254\344\270\200\347\253\240\344\270\212\346\234\272\351\242\230" "b/210920142/chapter1/\347\254\254\344\270\200\347\253\240\344\270\212\346\234\272\351\242\230" new file mode 100644 index 0000000000000000000000000000000000000000..ddfd551f1904946c39c3741cb6454f1f7b460896 --- /dev/null +++ "b/210920142/chapter1/\347\254\254\344\270\200\347\253\240\344\270\212\346\234\272\351\242\230" @@ -0,0 +1,50 @@ +#include +#include +#include + +long add1(long n) +{ + long i, sum = 0; + for (i = 1;i < = n;i++) + sum+=i; + return sum; +} +void AddTime1(long n) +{ + clock_t t; + long sum; + t = clock(); + sum = add1(n); + t = clock() - t; + printf("方法1:\n"); + printf(" 结果:1~%d之和:%1d\n",n,sum); + printf(" 用时: % 1f秒\n",((float)t)/CLOCKS_PER_SES); +} +long add2(long n) +{ + return n * (n+1)/2; + +} +void AddTime2(long n) +{ + clock_t t; + long sum; + t = clock(); + sum = add1(n); + t = clock() - t; + printf("方法2:\n"); + printf(" 结果:1~%d之和:%1d\n",n,sum); + printf(" 用时: % 1f秒\n",((float)t)/CLOCKS_PER_SES); +} +int main() +{ + int n; + printf("n(大于1000000):"); + scanf("% d",&n); + if(n<1000000) + return 0; + AddTime1(n); + AddTime2(n); + return 1; + +} \ No newline at end of file