From a4b14e02ada56cc9c0e0d8da5e39098fd6156c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Thu, 21 Sep 2023 15:21:33 +0000 Subject: [PATCH 01/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 4562e17b..dc8d5e05 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,46 @@ #include +#include +#include -int main(void) { - printf("Hello "); - - return 0; +long way1(int n) +{ + long i,sum=0; + for(i=1;i<=n;i++) + { + sum+=i; + } + return sum; } +long way2(int n) +{ + return n*(n+1)/2; +} +void assesstime1(int n) +{ + clock_t t1,t2; + t1=clock(); + way1(n); + t2=clock()-t1; + printf("n个整数数之和为:%ld\n",way1(n)); + printf("运行时间为:%lf\n",(float(t2))/CLOCKS_PER_SEC); +} +void assesstime2(int n) +{ + clock_t t1,t2; + t1=clock(); + way2(n); + t2=clock()-t1; + printf("n个整数数之和为:%ld\n",way2(n)); + printf("运行时间为:%lf\n",(float(t2))/CLOCKS_PER_SEC); +} +int main(void) +{ + long way1(int n); + long way2(int n); + void assesstime1(int n); + void assesstime2(int n); + int n; + scanf("%d",&n); + assesstime1(n); + assesstime2(n); +} \ No newline at end of file -- Gitee From a841528eb1418085f09c3e596c3de7f8a0277322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Thu, 21 Sep 2023 15:21:37 +0000 Subject: [PATCH 02/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 6db2605b9b13c58dee2275f5cc041f2cf0db8d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Thu, 21 Sep 2023 15:21:40 +0000 Subject: [PATCH 03/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 72c43d0090eb8868b644b508338016ef871aae0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Thu, 21 Sep 2023 15:21:42 +0000 Subject: [PATCH 04/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 949e5a1df4931faa84f44e6287eb4ccef583a67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 08:35:48 +0000 Subject: [PATCH 05/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020147/exp.1-1.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2224020147/exp.1-1.cpp diff --git a/2224020147/exp.1-1.cpp b/2224020147/exp.1-1.cpp new file mode 100644 index 00000000..8e0fac9f --- /dev/null +++ b/2224020147/exp.1-1.cpp @@ -0,0 +1,53 @@ +//文件名:exp1-1.cpp +#include +#include //clock_t, clock, CLOCKS_PER_SEC +#include + +//------方法1----------------------------------------------- +long add1(long n) //方法1:求1+2+...+n +{ + long i,sum=0; + for (i=1;i<=n;i++) + sum+=i; + return sum; +} + +void AddTime1(long n) //采用方法1的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add1(n); + t=clock()-t; + printf("方法1:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} + +//------方法2----------------------------------------------- +long add2(long n) //方法2:求1+2+...+n +{ + return n*(n+1)/2; +} +void AddTime2(long n) //采用方法2的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add2(n); + t=clock()-t; + printf("方法2:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} +//------------------------------------------------------------ +int main() +{ + int n; + printf("n(大于1000000):"); + scanf("%d",&n); + if (n<1000000) return 0; + AddTime1(n); + AddTime2(n); + return 1; +} -- Gitee From f4f57cbfcf88080fd40f055f193366a0557b4db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 08:35:53 +0000 Subject: [PATCH 06/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 81d937651835ef4aa0d0c0a309b81cd44eb34650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 08:35:56 +0000 Subject: [PATCH 07/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 6f8189a7365d2e725820b6caeea06faf04692b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:00:38 +0000 Subject: [PATCH 08/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020147/chapter/exp.1-1.cpp | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2224020147/chapter/exp.1-1.cpp diff --git a/2224020147/chapter/exp.1-1.cpp b/2224020147/chapter/exp.1-1.cpp new file mode 100644 index 00000000..8e0fac9f --- /dev/null +++ b/2224020147/chapter/exp.1-1.cpp @@ -0,0 +1,53 @@ +//文件名:exp1-1.cpp +#include +#include //clock_t, clock, CLOCKS_PER_SEC +#include + +//------方法1----------------------------------------------- +long add1(long n) //方法1:求1+2+...+n +{ + long i,sum=0; + for (i=1;i<=n;i++) + sum+=i; + return sum; +} + +void AddTime1(long n) //采用方法1的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add1(n); + t=clock()-t; + printf("方法1:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} + +//------方法2----------------------------------------------- +long add2(long n) //方法2:求1+2+...+n +{ + return n*(n+1)/2; +} +void AddTime2(long n) //采用方法2的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add2(n); + t=clock()-t; + printf("方法2:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} +//------------------------------------------------------------ +int main() +{ + int n; + printf("n(大于1000000):"); + scanf("%d",&n); + if (n<1000000) return 0; + AddTime1(n); + AddTime2(n); + return 1; +} -- Gitee From c4ed822677ff87937c8b5f9de640a7c57de42dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:00:47 +0000 Subject: [PATCH 09/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From f7a1508df25c5fed0fd6bbc01ddb4546f3e28d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:00:50 +0000 Subject: [PATCH 10/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From 0566fe6f94a093d80ed240183ed5904a40be92d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:14:29 +0000 Subject: [PATCH 11/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020147/chapter/exp.1_1.cpp | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2224020147/chapter/exp.1_1.cpp diff --git a/2224020147/chapter/exp.1_1.cpp b/2224020147/chapter/exp.1_1.cpp new file mode 100644 index 00000000..8e0fac9f --- /dev/null +++ b/2224020147/chapter/exp.1_1.cpp @@ -0,0 +1,53 @@ +//文件名:exp1-1.cpp +#include +#include //clock_t, clock, CLOCKS_PER_SEC +#include + +//------方法1----------------------------------------------- +long add1(long n) //方法1:求1+2+...+n +{ + long i,sum=0; + for (i=1;i<=n;i++) + sum+=i; + return sum; +} + +void AddTime1(long n) //采用方法1的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add1(n); + t=clock()-t; + printf("方法1:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} + +//------方法2----------------------------------------------- +long add2(long n) //方法2:求1+2+...+n +{ + return n*(n+1)/2; +} +void AddTime2(long n) //采用方法2的耗时统计 +{ + clock_t t; + long sum; + t=clock(); + sum=add2(n); + t=clock()-t; + printf("方法2:\n"); + printf(" 结果:1~%d之和:%ld\n",n,sum); + printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); +} +//------------------------------------------------------------ +int main() +{ + int n; + printf("n(大于1000000):"); + scanf("%d",&n); + if (n<1000000) return 0; + AddTime1(n); + AddTime2(n); + return 1; +} -- Gitee From 6ead2248efdc4eab3199ddb921ede29bed796610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E4=BC=8A=E5=A9=B7?= <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:14:35 +0000 Subject: [PATCH 12/15] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E9=97=AE=E9=A2=98=E4=B8=8D=E5=90=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E7=BB=9D=E5=AF=B9=E6=89=A7=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BD=93=E4=BC=9A=E4=B8=8D=E5=90=8C=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=8A=A3=E5=8A=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- Gitee From dd2b589b23d93561be2d16fe988686d4aaf011f7 Mon Sep 17 00:00:00 2001 From: pp <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:16:19 +0000 Subject: [PATCH 13/15] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?24020147/chapter/exp.1=5F1.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020147/chapter/exp.1_1.cpp | 53 ---------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 2224020147/chapter/exp.1_1.cpp diff --git a/2224020147/chapter/exp.1_1.cpp b/2224020147/chapter/exp.1_1.cpp deleted file mode 100644 index 8e0fac9f..00000000 --- a/2224020147/chapter/exp.1_1.cpp +++ /dev/null @@ -1,53 +0,0 @@ -//文件名:exp1-1.cpp -#include -#include //clock_t, clock, CLOCKS_PER_SEC -#include - -//------方法1----------------------------------------------- -long add1(long n) //方法1:求1+2+...+n -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} - -void AddTime1(long n) //采用方法1的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} - -//------方法2----------------------------------------------- -long add2(long n) //方法2:求1+2+...+n -{ - return n*(n+1)/2; -} -void AddTime2(long n) //采用方法2的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} -//------------------------------------------------------------ -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if (n<1000000) return 0; - AddTime1(n); - AddTime2(n); - return 1; -} -- Gitee From 20e1a1aed2f1436dba227677741d03042f3e5072 Mon Sep 17 00:00:00 2001 From: pp <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:16:37 +0000 Subject: [PATCH 14/15] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?24020147/exp.1-1.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020147/exp.1-1.cpp | 53 ------------------------------------------ 1 file changed, 53 deletions(-) delete mode 100644 2224020147/exp.1-1.cpp diff --git a/2224020147/exp.1-1.cpp b/2224020147/exp.1-1.cpp deleted file mode 100644 index 8e0fac9f..00000000 --- a/2224020147/exp.1-1.cpp +++ /dev/null @@ -1,53 +0,0 @@ -//文件名:exp1-1.cpp -#include -#include //clock_t, clock, CLOCKS_PER_SEC -#include - -//------方法1----------------------------------------------- -long add1(long n) //方法1:求1+2+...+n -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} - -void AddTime1(long n) //采用方法1的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} - -//------方法2----------------------------------------------- -long add2(long n) //方法2:求1+2+...+n -{ - return n*(n+1)/2; -} -void AddTime2(long n) //采用方法2的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} -//------------------------------------------------------------ -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if (n<1000000) return 0; - AddTime1(n); - AddTime2(n); - return 1; -} -- Gitee From 78963b5f178af2284094c16b315d2f3831041bdd Mon Sep 17 00:00:00 2001 From: pp <3278045090@qq.com> Date: Fri, 6 Oct 2023 13:16:45 +0000 Subject: [PATCH 15/15] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ma?= =?UTF-8?q?in.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 main.cpp diff --git a/main.cpp b/main.cpp deleted file mode 100644 index dc8d5e05..00000000 --- a/main.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include - -long way1(int n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - { - sum+=i; - } - return sum; -} -long way2(int n) -{ - return n*(n+1)/2; -} -void assesstime1(int n) -{ - clock_t t1,t2; - t1=clock(); - way1(n); - t2=clock()-t1; - printf("n个整数数之和为:%ld\n",way1(n)); - printf("运行时间为:%lf\n",(float(t2))/CLOCKS_PER_SEC); -} -void assesstime2(int n) -{ - clock_t t1,t2; - t1=clock(); - way2(n); - t2=clock()-t1; - printf("n个整数数之和为:%ld\n",way2(n)); - printf("运行时间为:%lf\n",(float(t2))/CLOCKS_PER_SEC); -} -int main(void) -{ - long way1(int n); - long way2(int n); - void assesstime1(int n); - void assesstime2(int n); - int n; - scanf("%d",&n); - assesstime1(n); - assesstime2(n); -} \ No newline at end of file -- Gitee