当前位置:   article > 正文

数据结构实验(一)——绪论_编写一个程序,对于 1~n 的每个整数 n,logn

编写一个程序,对于 1~n 的每个整数 n,logn

实验一:

内容:
编写一个程序,对于给定的正整数n,求1+2+……+n,采用逐个累加和n(n+1)/2(高斯法)两种解法。对于相同的n,给出这两种解法的求和结果和求解时间,并用相关数据进行测试。

程序:

#include<iostream>

#include<ctime>

#include<math.h>

using namespace std;



long add1(long n){

long sum=0;

for(int i=0;i<=n;i++){

sum=sum+i;

}

return sum;

}

void addtime1(long n){

clock_t t;

long sum;

t=clock();

sum=add1(n);

t=clock()-t;

cout<<"方法一:"<<endl;

cout<<"结果:1~"<<n<<"之和为:"<<sum<<endl;

cout<<"用时:"<<((float)t)/CLOCKS_PER_SEC<<"秒"<<endl;

}



long add2(long n){

return n*(n+1)/2; 

}



void addtime2(long n){

clock_t t;

long sum;

t=clock();

sum=add2(n);

t=clock()-t;

cout<<"方法二:"<<endl;

cout<<"结果:1~"<<n<<"之和为:"<<sum<<endl;

cout<<"用时:"<<((float)t)/CLOCKS_PER_SEC<<"秒"<<endl;

}





int main(){

int n;

cout<<"输入n(大于1000000):";

cin>>n;

if(n<100000){

return 0;

}

addtime1(n);

addtime2(n);

return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

运行结果:
在这里插入图片描述

实验二

内容:

编写一个程序,对于1~n的每个整数n,输出log₂(n),√n,n,nlog₂(n),n²,n³,2ⁿ和n!的值。

程序:

#include<iostream>

#include<math.h>

#include<stdio.h>

using namespace std;

double log2(double n){

return log10(n)/log10(2);

}

long exponent(int n){

long s=1;

for(int i=1;i<=n;i++){

s=s*2;

}

return s;

}

long factorial(int n){

long s=1;

for(int i=1;i<=n;i++){

s=s*i;

}

return s;

}

void fun(int n){

cout<<"log2(n)sqrt(n) nnlog2(n)   n^2   n^3     2^nn!"<<endl;

cout<<"-------------------------------------------------------------------------------"<<endl;

for(int i=1;i<=n;i++){

printf("%5.2f\t",log2(double(i)));

printf("%5.2f\t",sqrt(i));

printf("%2d\t",i);

printf("%7.2f\t",i*log2(double(i)));

printf("%5d\t",i*i);

printf("%7d\t",i*i*i);

printf("%8d\t",exponent(i));

printf("%10d\n",factorial(i));

}

}



int main(){

int n=10;

fun(n);

return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

运行结果:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/328940
推荐阅读
相关标签
  

闽ICP备14008679号