例5
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"求自然对数E的值"<<endl;
double e=0,e0=0;
double n=0,a=1,b;
while(n<100)
{
for(b=1;b<=n;b++)
{
a*=b;
}
e0=1/a;
e+=e0;
a=1;
n++;
}
cout<<"e的近似值为:"<<setprecision(8)<<e<<endl;
return 0;
}
题6
#include<iostream> #include<iomanip> using namespace std; int main() { long int i; float j; double k=0; for(i=1;i<=5e7;i++) { j=1.0/(2*i-1); if(i/2==i/2.0) k-=j; else k+=j; } cout<<"pai="<<setprecision(8)<<k*4<<endl; return 0; } 题7
#include<iostream> #include<iomanip> using namespace std; int main() { int i=0; long int a; float N,m; while(1) { cout<<"请输入一个值:"<<endl; cin>>N; for(a=1;a<=N;a=10*a) { i++; } switch(i) { case 2: cout<<N<<"is 10 to 100."<<endl; break; case 3: cout<<N<<"is 100 to 1000."<<endl; break; default: if(i<2) cout<<N<<"小于10"<<endl; if(i>3) cout<<N<<"在1000以上。"<<endl; break; } i=0; } return 0; }
#include<iostream> using namespace std; int main() { int i,j,k; for(i=7;i>=1;i-=2) { for(k=i;k>0;k--) cout<<' '; for(j=8-i;j>0;j--) { cout<<"*"; } cout<<endl; } for(i=5;i>=1;i-=2) { for(k=8-i;k>0;k--) cout<<' '; for(j=i;j>0;j--) { cout<<"*"; } cout<<endl; } return 0; }