diff --git a/2224020147/chapter/exp.1-1.cpp b/2224020147/chapter/exp.1-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e0fac9f5299b890d3f16c867982555e50d19994 --- /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; +} diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 4562e17b782ed0a07d3274efd6e2cd31f395bce4..0000000000000000000000000000000000000000 --- a/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main(void) { - printf("Hello "); - - return 0; -}