week_4_homework

来源:互联网 发布:mysql视频教程 百度云 编辑:程序博客网 时间:2024/06/06 19:57

第一:

5:求自然对数


#include <iostream>using namespace std;int main(){    int i=0;    double value=1,fac=1;    while (fac>=10e-6)    {        i++;        fac=fac/i;        value+=fac;    }    cout << "e = "<<value<<endl;    return 0;}


6:求圆周率:


#include <iostream>#include <cmath>using namespace std;int main(){    int i;    double pi=0,fac=1;    for(i=1;i<10e6;i+=2)    {        pi+=4*(double)1/i*fac;        fac*=-1;    }    cout << "pi约等于:"<<pi<<endl;    return 0;}

7.


#include <iostream>using namespace std;int main(){    int num;    cout << "请输入一个数:"<<endl;    cin >> num ;    if(num<10)        cout<<num<<" is less than 10."<<endl;    else if(num>=10&&num<100)        cout<<num<<" is 10 to 100."<<endl;    else if (num>=100&&num<1000)        cout<<num<<" is 100 to 1000."<<endl;    else cout << num<<" is greater than 1000."<<endl;    return 0;}

8.


#include <iostream>using namespace std;int main(){    int i,k;    for(i=0;i<4;i++)    {        for(k=0;k<6-2*i;k++)           cout << " ";        for(;k<(8+2*i);k++)        {            if(k%2==0)                cout<<"*";            else cout<<" ";        }         for(;k<13;k++)           cout << " ";    cout << endl;    }    for (i=2;i>=0;i--)    {      for(k=0;k<6-2*i;k++)           cout << " ";        for(;k<(8+2*i);k++)        {            if(k%2==0)                cout<<"*";            else cout<<" ";        }         for(;k<13;k++)           cout << " ";    cout << endl;    }    return 0;}

9.

#include <iostream>using namespace std;int main(){    int n=1,add=0;    while (add<=1000)    {        add+=n*n;        n++;    }    n=n-1;    cout<< "最大的n值是:"<<n<<endl;    return 0;}

10.

#include <iostream>#include <iomanip>using namespace std;int main(){    double poor=0,poor_m=0.01,m_bags=0;    int i;    for (i=0;i<30;i++)        m_bags+=10e4;    for(i=0;i<30;i++)    {        poor+=poor_m;        poor_m*=2;    }    cout <<fixed;    cout <<"富翁给了穷人" <<m_bags<<"元"<<endl;    cout <<"穷人给了富翁"<< poor <<"元"<<endl;    return 0;}




第二:百钱百鸡


#include <iostream>using namespace std;int main(){    int x,y,z;//鸡翁数为x,鸡母数为y,鸡雏数为z    for(x=0;x<=20;x++)        for(y=0;y<=33;y++)        for(z=0;z<=99;z+=3)    {        if(5*x+3*y+z/3==100&&x+y+z==100)            cout<<"鸡翁数为: " <<x<<"鸡母数为: "<<y<<"鸡雏数为: "<<z<<endl;    }    return 0;}//分析可知,鸡翁数为1到20之间,鸡母数为1到33之间,鸡雏数为3到99之间。//穷举出最后结果。


第三:


#include <iostream>using namespace std;int main(){    int num,i=0,add=0;//i作为计数    cout << "请输入一个数字:" << endl;    cin >> num;    while (num!=0)    {        add=add+num%10;//求模得个位上数值且相加        num=num/10;//降低一位        i++;    }    cout << "该数由"<<i<<"位组成,各个数位上数之和为:"<<add<<endl;    return 0;}

第四:

#include <iostream>using namespace std;int main(){    float x,y;//坐标    cout << "请输入该点横坐标:"<<endl;    cin >> x;    cout << "请输入该点纵坐标:"<< endl;    cin >> y;    if (((x+2)*(x+2)+(y+2)*(y+2)<=1)//计算坐标是否在四个圆中其中一个的面积内        ||((x-2)*(x-2)+(y+2)*(y+2)<=1)        ||((x+2)*(x+2)+(y-2)*(y-2)<=1)        ||((x-2)*(x-2)+(y-2)*(y-2)<=1))//四个中一个成立就成立        cout <<"该点建筑的高度为10米"<<endl;    else cout <<"该点建筑的高度为0米"<<endl;    return 0;}

第五:

法一:

#include <iostream>using namespace std;int main(){    int n,s=0,i,fac;    cout << "请输入一个整数n:"<<endl;    cin >> n;    for (;n>0;n--)    {        fac=1;        for(i=n;i>0;i--)//求N!            fac*=i;        s+=fac;//求和    }        cout <<"s=1!+2!+3!……+n!="<<s<<endl;    return 0;}

法二:

#include <iostream>using namespace std;int main(){    int n,s=0,i,fac;    cout << "请输入一个整数n:"<<endl;    cin >> n;    while(n>0)    {        i=n;        fac=1;        while(i>0)        {            fac*=i;            i--;        }        s+=fac;        n--;    }        cout <<"s=1!+2!+3!……+n!="<<s<<endl;    return 0;}

第六:


#include <iostream>using namespace std;int main(){    int n=1,day;//n为苹果数    for (day=10;day>1;day--)    {        n=(n+1)*2;    }    cout << "第一天时猴子摘了"<<n<<"个苹果。"<<endl;    return 0;}

第七:

#include <iostream>using namespace std;int main(){    int a,n,i,k,fac=0,s=0;    cout << "请输入数字 a:"<<endl;    cin >> a;    cout << "请输入a的位数n:"<<endl;    cin >> n;    for(;n>0;n--)    {        i=n;        k=a;        for(;i>0;i--)        {            fac+=k;            k=k*10;        }        s+=fac;        fac=0;    }    cout<< "s[n]=a+aa+aaa+aa...a="<<s<<endl;    return 0;}

第八:

#include <iostream>using namespace std;int main(){    int fac_1,fac_2,pro;//因子一和因子二,以及乘积。    for(fac_2=1;fac_2<=9;fac_2++)    {        for(fac_1=1;fac_1<=fac_2;fac_1++)        {            pro=(fac_1*fac_2);            cout <<fac_1<<"×"<<fac_2<<"="<<pro<<" ";        }        cout << endl;    }    return 0;}


第九:

#include <iostream>using namespace std;int main(){    char Z_three,L_four,W_five;    for(Z_three='A';Z_three<='C';Z_three++)        for(L_four='A';L_four<='C';L_four++)        for(W_five='A';W_five<='C';W_five++)        {             if(Z_three!=L_four&&Z_three!=W_five&&L_four!=W_five)             if(Z_three!='A'&&W_five!='A'&&W_five!='C')             {                 cout << "张三--"<<Z_three<<endl;                 cout << "李四--"<<L_four<<endl;                 cout << "王五--"<<W_five<<endl;             }        }    return 0;}//设陈六对应字符A,赵七对应字符B,宋八对应字符C



0 0
原创粉丝点击