第四周作业

一.5.求自然对数e的近似值,要求误差小于10-6,。

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int i;
	double sum,fact;
	sum=1;fact=1;
	for(i=1;fact<=1/10e-7;i++)
	{	fact*=i;                   //等价于fact=fact*i
    	sum=sum+1/fact;
	}

	cout<<"e="<<setprecision(7)<<sum<<endl;

	return 0;
}

6.求圆周率π的近似值。

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int i,fact;
	double pi;
	fact=1,pi=0;
	for(i=1;i<10e6;i=i+2)
	{
		pi=pi+4*(double)1/i*fact;
		fact=fact*-1;
	}
	cout<<"π约等于:"<<setprecision(7)<<pi<<endl;

	return 0;
}

7.数据分来输出

#include<iostream>
using namespace std;

int main()
{
	int n;
	cout<<"清输入一个数:";
	cin>>n;
	if(n<10)
		cout<<n<<" is less than 10"<<endl;
	else if(n>=10&&n<100)
		cout<<n<<" is 10 to 100"<<endl;
	else if(n>=100&&n<1000)
		cout<<n<<" is 100 to 1000"<<endl;
	else 
		cout<<n<<" is more than 1000"<<endl;

	return 0;
}

8.编辑图形

#include<iostream>  
using namespace std;  
  
int main()
{  
    cout<<"      *      "<<endl;
	cout<<"    * * *    "<<endl;
	cout<<"  * * * * *  "<<endl;
	cout<<"* * * * * * *"<<endl;
	cout<<"  * * * * *  "<<endl;
	cout<<"    * * *    "<<endl;
	cout<<"      *      "<<endl;

	return 0;
}

9.求n的最大值

#include<iostream>  
using namespace std;  
  
 void main()  
{  
    double a,b,count;  
    b=0;  
    for(a=1;b<=1000;a++)  
    {  
        b+=a*a;  
        count=a;  
    }  
      
    cout<<count<<endl;  
}

10.富人给多少钱陌生人

#include<iostream>  
#include<iomanip>  
using namespace std;  
  
int main()  
{  
    int a;  
    double b,money_1,money_2;  
    money_1=0;  
    b=0.01;  
    money_2=0;  
    for(a=1;a<=30;a++)  
    {  
        money_1+=10e4;  
        b*=2;  
        money_2+=b;  
      
    }  
      
    cout<<"富人给了穷人"<<fixed<<money_2<<"元"<<endl;  
    cout<<"穷人给了富人"<<fixed<<money_1<<"元"<<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;  
}


三.求一个数各个位上的数字之和.

#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>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float x,y;  
    cout<<"请输入横坐标:";  
    cin>>x;  
    cout<<"请输入纵坐标:";  
    cin>>y;  
    cout<<"您输入的坐标为:("<<x<<","<<y<<")"<<'\n';  
    x=fabs(x);  
    y=fabs(y);  
    if((x-2)*(x-2)+(y-2)*(y-2)<=1)  
        cout<<"该建筑物高度为10米!"<<endl;  
    else  
        cout<<"该建筑物高度为0米!"<<endl;  
  
    return 0;  
}


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

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

6.猴子吃苹果问题:猴子第一天摘了若干个苹果,当时吃了一半,还不过隐,又多吃了一个。第二天,又吃掉余下的一半,又多吃一个。以后每一天,都是吃掉前一天余下的一半零一个。到第10天,只有一个苹果了。问猴子第一天共摘了多少个苹果?

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

7.计算s[n]=a+aa+aaa+aa...a(n个)的值。其中a是一个数字,n表示a的位数。例如,当a=1,n=5时,则要计算的表达式为

    s[5]=1+11+111+1111+11111

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

8.打印九九乘法表。

/****************************************** 
****    功能:九九乘法表    *************** 
******************************************/  
#include<iostream>  
using namespace std;  

int main()  
{  
    int i,j,k;  
    cout<<"九九乘法表"<<endl;  
    for(i=1;i<10;i++)  
    {  
        for(j=1;j<=i;j++)  
        {  
            k=i*j;  
            cout<<j<<"*"<<i<<"="<<k<<' ';  
        }  
        cout<<endl;  
    }
	return 0;
}

9.两个羽毛队进行单打比赛,各出3个人。甲队为张三、李四、王五3个队员,已队为陈六、赵七、宋八3个队员。现已经抽签决定比赛名单,有人向队员打听比赛名单,张三说他不和陈六打,王五说他不和陈六和宋八打。请编程找出3对比赛名单。

/************************************************
********* 求羽毛球比赛名单 *********************/
#include <iostream>  
using namespace std;  
 
int main()  
{  
    char Z_three,L_four,W_five;  
    for(Z_three='A';Z_three<='C';Z_three++) //A代表陈六,B代表赵七,C代表宋八
        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;  
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值