第四周作业

这篇博客涵盖了多项编程任务,包括计算自然对数e的近似值、圆周率的逼近、程序分类、图形输出、数学问题求解,如最大N值的寻找、百万富翁问题、九九乘法表。此外,还涉及编程解决经典问题,如'百钱百鸡'、计算数字位数及和、阶乘的多种计算方法、猴子吃苹果问题,以及字符串累加求和。博主表示部分题目参考了他人解决方案,但仍将持续深入学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:

5.求自然对数e的近似值

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	long double e=1.0;
	long double f=1;
	for(int i=1;1.0/f>=1e-6;i++);
	{
		f=f*i;
		e=e+1.0/f;
	}
	cout<<"自然对数 e 的近似值为:"<<e<<endl;
	return 0;
}


 

6.求圆周率的近似值

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	long double pi=1.0;
	double flag =-1.0;
	for(int i=3;1.0/i>=1e-6;i+=2)
	{
		pi+=flag/i;
		flag=-flag;
	}
	cout<<"圆周率pi的近似值为:"<<4*pi<<endl;

	return 0;
}

 

7,编一程序,,,分类并显示

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int number;
	cout<<"请输入一个整数:"<<endl;
	cin>>number;
	if(number<10)
		cout<<number<<"is less than 10.";
	else if(number<=99)
		cout<<number<<"is 10 to 99.";
	else if(number<=999)
		cout<<number<<"is 100 to 999.";
	else
		cout<<number<<"is more than 1000.";
	cout<<endl;
	return 0;
}



8.编一程序,输出图形

#include <iostream>
using namespace std;

int main()
{
	int i,j,k;
	for(i=0;i<=3;i++)
	{
		for(j=0;j<6-2*i;j++)
			cout<<" ";
		for(k=0;k<2*i+1;k++)
			cout<<" *";
		cout<<endl;
	}

	for(i=0;i<=2;i++)
	{
		for(j=0;j<2+2*i;j++)
			cout<<" ";
		for(k=0;k<5-2*i;k++)
			cout<<" *";
		cout<<endl;
	}
	return 0;
}


 

9编一程序,求满足条件的最大N值

#include <iostream>
using namespace std;

int main()
{
	int sum=0,n;
	for(n=1;;n++)
	{
		sum=sum+n*n;
		if(sum>=1000)
			break;
	}
	sum=sum-n*n;
	cout<<"满足\"1*1+2*2+3*3+...+n*n<=1000\"的最大n值为"<<n-1<<endl
		<<"其和为:"<<sum<<endl;

	return 0;
}


10百万富翁

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	double day =0.01,a=0.0,b=10.0;
	//a表示陌生人得到的钱,b表示百万富翁得到的钱
	int i = 1;
	while(i<=30)
	{
		a=a+day;
		day=day*2;
		i++;
	}
	a=a/10000.0;
	b=b*30;
	cout<<"一个月陌生人给百万富翁的钱"<<b<<"万元"<<endl
		<<"百万富翁给陌生人的钱"<<a<<"万元"<<endl;
	return 0;
}


 

11.九九乘法表

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int n,i,j,sum;
	for(i=1;i<10;i++)
	{
		for(j=1;j<=i;j++)
		{
			sum=i*j;
			cout<<j<<"*"<<i<<"="<<sum<<"\t";
		}
		cout<<endl;
	}
	return 0;
}

 

 

二:编程求“百钱百鸡”问题。

 

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
using namespace std;

 int main() 
{ 
	 int rooster,hen,chick,n=0;
	for(rooster=1;rooster<=20;rooster++) 
	for(hen=1;hen<=33;hen++) 
{ 
	chick=100-hen-rooster; 
	if((chick%3==0)&&(5*rooster+3*hen+chick/3==100)) 
	cout<< "方法" <<++n<<" rooster="<<rooster<<" hen="<<hen<<" chick="<<chick<<endl;
} 
	return 0;
}


 

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

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
using namespace std;

 int main() 
{
	 long sum=0,num,j,k,i=0;       //i存放它是几位数字,sum存放各个位数相加之和,num获取数字
	 cout<<"请输入一个整数"<<endl;
	 cin>>num;
	 k=num;
	 while(num!=0)                //计算它是几位数字
	 {
		 num=num/10;
		 i++;
	 }
	 cout<<"它总共由"<<i<<"位数字组成"<<endl;
	 for(j=0;j<i;j++)			//计算各个位数相加之和
	 {
			sum+=k%10;
			k=k/10;
	 }
	 cout<<"各个位数相加之和为"<<sum<<endl;
	 return 0;
}


 

 

四:求该点建筑物的高度。

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>  
#include <math.h>  
using namespace std;  
  
int main()  
{  
    float x,y,s;  
    cout<<"请输入一个平面坐标(x,y)计算其高度"<<endl;  
    cout<<"x=";  
    cin>>x;  
    cout<<"y=";  
    cin>>y;  
    if(x<0)  
        x=-x;  
    if(y<0)  
        y=-y;  
    s=sqrt((x-2)*(x-2)+(y-2)*(y-2));  
    if(s<=1)  
        cout<<"这个平面坐标的高度为10"<<endl;  
    else  
        cout<<"这个平面坐标的高度为0"<<endl;  
  
    return 0;  
  
} 


 

 

 

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

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
using namespace std;

 int main() 
{
	int i,n,j,k=1,sum=0;
	cout<<"请输入一个正整数n"<<endl;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=i;j++)
			k=k*j;
		sum+=k;
		k=1;
	}
	cout<<"1!+2!+3!+......n!="<<sum<<endl;
	return 0;
}


 

 

六:猴子吃苹果问题

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>    
using namespace std;    
    
int main()    
{    
    int a=1,b=1,c;    
    cout<<"猴子吃苹果问题,"<<endl;    
    cout<<"猴子每天都吃昨天吃剩的一半再加一个,"<<endl;    
    cout<<"直到第十天,猴子只剩下最后一个苹果,"<<endl;    
    cout<<"问猴子一共有多少个苹果?"<<endl;    
    while(a<=10)    
    {    
        c=(2*b)+1;    
        a++;    
        b=c;    
    }    
    cout<<endl;    
    cout<<"解:猴子一共有"<<c<<"个苹果"<<endl;    
    
    return 0;    
} 

 

七:计算s[n]=a+aa+aaa+aa...a(n个)的值。

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>  
#include<math.h>  
using namespace std;  
  
int main()  
{  
    int n,a,i,s1,S,Sn;  
    cout<<"计算s[n]=a+aa+aaa+aa...a(n个)的值"<<endl;  
    cout<<"请输入a的值"<<endl;  
    cin>>a;  
    cout<<"请输入n的值"<<endl;  
    cin>>n;  
    s1=0;  
    i=1;  
    Sn=0;  
    while(i<=n)  
    {  
        S=s1+a*pow(10,i-1);  
        Sn=S+Sn;  
        s1=S;  
        i++;  
    }  
    cout<<"S["<<n<<"]="<<Sn<<endl;  
    return 0;  
}  


 

 

  八:在前面(一的11小题中)已做

九:请编程找出3对比赛名单。

// 223.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream> 
using namespace std;

int main()
{
	cout<<"羽毛球单打参赛选手"<<endl;
	cout<<"甲队:A 张三、B 李四、C 王五"<<endl;
	cout<<"乙队:D 陈六、E 赵七、F 宋八"<<endl;
	cout<<"比赛名单"<<endl;
	char i,j,k;
	for(i='D';i<='F';i++)
    for(j='D';j<='F';j++)
	for(k='D';k<='F';k++)
	if(i!=j&&i!=k&&j!=k)//三人对手各不相同,这句排除相同的情况
	if(i!='D'&&k!='D'&&k!='E')//张三说他不和陈六打,王五说他不和陈六和宋八打 
					{
						cout<<"A VS "<<i<<endl; 
                        cout<<"B VS "<<j<<endl;
                        cout<<"C VS "<<k<<endl; 
					}

	return 0;
}


 

 

 

十。老师,有的不会做,参考了别人的额,敬请原谅,我会继续好好琢磨的。

 

 

 

 

 

 


 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值