习题5
#include <iostream>
using namespace std;
int main()
{
int i=1,sum=1;
long double e=1.0;
for( i=1;1.0/sum>=1e-6;i++)
{
sum=sum*i;
e=e+1.0/sum;
}
cout<<"自然对数 e 的近似值为:"<<e<<endl;
return 0;
}
习题6
#include<iostream>
using namespace std;
int main()
{
int i;
double sum=0, j;
for(i=1;j>=1e-6;i++)
{
j=1/(2*i-1);
if(i/2==i/2.0)
sum-=j;
else
sum+=j;
}
cout<<"π="<<sum*4<<endl;
return 0;
}
习题7
#include <iostream>
using namespace std;
int main()
{
int t;
cout<<"请输入一个整数:"<<endl;
cin>>t;
if(t<10)
cout<<t<<"is less than 10.";
else if(t<=99)
cout<<t<<"is 10 to 99.";
else if(t<=999)
cout<<t<<"is 100 to 999.";
else
cout<<t<<"is more than 1000.";
cout<<endl;
return 0;
}
习题8.。。。。。不会高级的方法
#include<iostream>
using namespace std;
int main()
{
cout<<" *\endl"
<<" * * *\endl"
<<" * * * * *\endl"
<<"* * * * * * *\endl"
<<" * * * * *\endl"
<<" * * *\endl"
<<" *\endl";
return 0;
}
习题9
#include<iostream>
using namespace std;
int main()
{
int n=0,s=0;
while(s<=1000)
{
n++;
s+=n*n;
}
cout<<"满足条件的最大值";
cout<<"n="<<n-1<<endl;
return 0;
}
百钱百鸡
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
int i=0;
cout<<"鸡翁一值钱五,\n鸡母一值钱三,\n鸡雏三值钱一。\n百钱买百鸡,\n问鸡翁、鸡母、鸡雏各几何?\n\n";
for(a=0;a<=20;a++)
for(b=0;b<=33;b++)
for(c=0;c<=100;c+=3)
{
if(5*a+3*b+c/3==100 && a+b+c==100)
{
i++;
cout<<"\n第"<<i<<"种方案\n\n";
cout<<"..............................\n";
cout<<" 公鸡的只数 :"<<a<<"只\n"<<" 母鸡的只数 :"<<b<<"只\n"<<" 小鸡的只数 :"<<c<<"只\n";
}
}
return 0;
}
乘法表
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int i,j=0;
for(i=0;i<=9;i++)
for(j=1;j<=i;j++)
{
Sleep(300);
cout<<j<<'*'<<i<<'='<<i*j<<" \a";
if(j==i)
cout<<"\n\n"<<' ';
}
return 0;
}