周四作业

一、完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现,有可能的话使用两种以上方法。

第5题

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


第6题

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


第7题  

#include <iostream>
using namespace std;
int main()
{
    int a;
    cout << "任意输入一个数a:\n";
    cin >> a;
    
	if(a<10)
            cout<<a<<" 小于 10.\n";
    
	else if(a>=10 && a<100)
            cout<<a<<" is 10 to 100.\n";
    
	else if(a>=100 && a<1000)
            cout<<a<<" is 100 to 1000.\n";
    
	else
	    cout<<a<<" 大于 1000.\n";
    
	return 0;
}


第8题

#include <iostream>
using namespace std;
int main()
{
        cout<<"         *          \n";
        cout<<"       * * *        \n";
	cout<<"     * * * * *      \n";
	cout<<"   * * * * * * *    \n";
	cout<<"     * * * * *      \n";
	cout<<"       * * *        \n";
	cout<<"         *          \n";
	return 0;
}



第9题

#include<iostream>
using namespace std;
int main()
{
	int n=1,s=0;
	while(s<=1000)
	{
		s=s+n*n;
		n++;
	}
	n=n-1;
	cout<<"满足1^2+2^2+3^2+...+n^n<=1000的最大的n值为:"<<n<<endl;
	return 0;
}



第10题

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


二、编程求“百钱百鸡”问题。鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?

#include<iostream>
using namespace std;
int main()
{
	int i=0;
	int x,y,z;
	cout<<"百钱买百鸡问题,\n";
	cout<<"鸡翁一只值五钱,\n";
	cout<<"鸡母一只值三钱,\n";
	cout<<"鸡雏三只值一钱,\n";
	cout<<"若用百钱买百鸡,\n";
	cout<<"则可买鸡翁x、鸡母y、鸡雏z各为多少只?\n";
	cout<<"\n";
	for(x=0;x<=20;x++)
		for(y=0;y<=33;y++)
		for(z=0;z<=100;z+=3)
		{
			if(5*x+3*y+z/3 == 100 && x+y+z == 100)
			{
				i=i+1;
				cout<<"第"<<i<<"种方法\n";
				cout<<"鸡翁只数x= "<<x<<endl;
				cout<<"鸡母只数y= "<<y<<endl;
				cout<<"鸡雏只数z= "<<z<<endl;
				cout<<"\n";
			}
		}
		return 0;
}



三、编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。

#include <iostream>
using namespace std;
int main()
{
    int a,b=0,c=0;
    cout<<"任意输入一个数字:\n";
    cin>>a;
    while (a!=0)
    {
        c=c+a%10;
        a=a/10;
        b++;
    }
    cout<<"该数由"<<b<<"位组成,各个数位上数之和为:"<<c<<endl;
    return 0;
}


四、在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。

五、编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

方法一:

#include <iostream>
using namespace std;
int main()
{
    int n,i,j,s=0;
    cout<<"任意输入一个整数n:\n";
    cin>>n;
    while(n>0)
    {
        i=n,j=1;
        while(i>0)
        {
            j*=i,i--;
        }
        s+=j,n--;
    }

    cout <<"s=1!+2!+3!……+n!= "<<s<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值