From 44dc7303a632425f422f6bc1bf7243d5feb8fa16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=9C=E6=AC=A2=E5=81=9A=E7=99=BD=E6=97=A5=E6=A2=A6?= <760721316@qq.com> Date: Sat, 4 Nov 2023 07:40:33 +0000 Subject: [PATCH] =?UTF-8?q?add=20210920142/chapter1/=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E7=AB=A0=E4=B8=8A=E6=9C=BA=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 喜欢做白日梦 <760721316@qq.com> --- ...3\240\344\270\212\346\234\272\351\242\230" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "210920142/chapter1/\347\254\254\344\270\200\347\253\240\344\270\212\346\234\272\351\242\230" 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 00000000..ddfd551f --- /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 -- Gitee