第四周任务

来源:互联网 发布:网络ip计算器 编辑:程序博客网 时间:2024/05/18 01:57

第三章作业

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

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

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

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

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

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

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

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

8.打印九九乘法表。

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

10.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。


第一:课本习题

程序1:

/*******************************计算并输出自然对数的值  *************************/#include <iostream>using namespace std;int main(){double b=1,c=1,f=1;int a,d=1;while(!(b<1e-6)){for(a=1;a<=d;a++)c*=a;b=1/c;f+=b;c=1;d++;}cout<<"e="<<f<<endl;return 0;}


结果:2.71828

程序2:求圆周率

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:计算圆周率的值#include "stdafx.h"#include<iostream>#include<cmath>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b=0;double c=0,pi;for(a=1;1.0/a>=1e-8;a+=2){c+=pow(-1,b)*(1.0/a);b++;}pi=4*c;cout<<"圆周率PI的值约为:"<<pi<<endl;return 0;}
结果:圆周率PI的值约为:3.14159
程序3:编写程序,将输入的数分类显示

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:显示数据分类#include "stdafx.h"#include<iostream>#include<cmath>using namespace std;int _tmain(int argc, _TCHAR* argv[]){float a,b;int c,d=0;cout<<"please input a number"<<endl;cin>>a;for(b=10;b<a;b*=10){if(a/b>1)c=1;d+=c;}switch(d){case 0:cout<<a<<"is 0 to 10"<<endl;break;case 1:cout<<a<<"is 10 to 100"<<endl;break;case 2:cout<<a<<"is 100 to 1000"<<endl;break;default:cout<<a<<"is larger than 1000"<<endl;}return 0;}

程序4:

// l.cpp : 定义控制台应用程序的入口点。//功能:编程输出字符组成图形#include "stdafx.h"#include<iostream>#include<iomanip>using namespace std;int _tmain(int argc, _TCHAR* argv[]){cout<<setfill(' ')<<setw(7)<<"*"<<endl<<setw(9)<<"* * *"<<endl<<setw(11)<<"* * * * *"<<endl<<setw(13)<<"* * * * * * *"<<endl<<setw(11)<<"* * * * *"<<endl<<setw(9)<<"* * *"<<endl<<setw(7)<<"*"<<endl;return 0;}


 

改进后的程序

// jkfgti8uy.cpp : Defines the entry point for the console application.//gongneng:shuchulingxing#include "stdafx.h"#include<iostream>using namespace std;int main(int argc, char* argv[]){int a,b,c,i,j;cout<<"请输入一个奇数"<<endl;cin>>a;i=a;for(a;a>=1;a--){j=a-(i+1)/2;if(j>=0)j=j;elsej=-j;{for(b=2*j;b>0;b--)cout<<" ";for(c=1;c<=i-2*j;c++)cout<<"* ";cout<<endl;}}return 0;}



 

程序5:

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:习题9满足条件的最大值#include "stdafx.h"#include<iostream>#include<cmath>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a=0,b;for(b=1;b<=1000;){a++;b+=pow(a,2);}cout<<"满足条件的最大值为"<<a-1<<endl;return 0;}
结果:13

程序6:

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:习题10#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){float a=10000,b=0.01;int c;for(c=2;c<=30;c++){b+=2*b;a+=10000;}cout<<"陌生人给了富翁"<<a/10000<<"万元"<<endl;cout<<"富翁给了陌生人"<<b/10000<<"万元"<<endl;return 0;}
程序7:

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:习题11:九九乘法表#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c;for(a=1;a<=9;a++){for(b=1;b<=a;b++){c=b*a;cout<<b<<"x"<<a<<"="<<c<<"\t";}cout<<endl;}return 0;}


第二:百鸡百钱

// lesion3-2.cpp : 定义控制台应用程序的入口点。
//功能:百鸡百钱问题

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:百鸡百钱问题#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c;for(a=0;a*5<=100;a++)for(b=0;(a*5+b*3)<=100;b++){c=100-a-b;//百鸡if((a*5+b*3+c/3==100)&&(c%3==0))//百钱cout<<"公鸡有"<<a<<"只"<<"母鸡有"<<b<<"只"<<"小鸡有"<<c<<"只"<<endl;}return 0;}
相比以前写的那个,这次写的比较简洁


第三:整数位

// lesion3-2.cpp : 定义控制台应用程序的入口点。//功能:分辨一个数是几位数,并求每一位数之和#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,d=0;cout<<"please input a number"<<endl;cin>>a;for(b=10;b<=a;b*=10){if(a/b>1)c=1;d+=c;            }cout<<"这是一个"<<d+1<<"位数"<<endl;d=0;for(b=b/10;b>=1;b=b/10){c=a/b;d+=c;a%=b;}cout<<"各位数之和等于:"<<d<<endl;return 0;}

第四:塔内建筑

// l.cpp : 定义控制台应用程序的入口点。//功能:分辨一个数是几位数,并求每一位数之和#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){float x,y;cout<<"请输入点坐标,坐标值用空格键隔开"<<endl;cin>>x>>y;if(x<0)//由于每个区域是对称的,所以可以将所有坐标转换到第一象限x=-x;//这样就可以简化判断条件if(y<0)//y=-y;//if((x-2)*(x-2)+(y-2)*(y-2)<=1)cout<<"该点建筑高度为10米"<<endl;elsecout<<"该点建筑高度为0米"<<endl;return 0;}


第五:级数n项和

方法1:for循环

// l.cpp : 定义控制台应用程序的入口点。//功能:求级数s=1!+2!+...+n!#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,d,s=0;cout<<"你想计算到第几项?"<<endl;cin>>c;for(a=c;a>0;a--){d=1;for(b=a;b>=1;b--)d*=b;s+=d;}cout<<"级数前"<<c<<"项之和为:"<<s<<endl;return 0;}

方法2:while循环

// l.cpp : 定义控制台应用程序的入口点。//功能:求级数s=1!+2!+...+n!#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,d,s=0;cout<<"你想计算到第几项?"<<endl;cin>>a;b=a;while(b>0){c=b;d=1;while(c>0){d*=c;c--;}s+=d;b--;}cout<<"级数前"<<a<<"项之和为:"<<s<<endl;return 0;}


第六:猴子

// l.cpp : 定义控制台应用程序的入口点。//功能:猴子吃苹果#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a=10,b=1,c=0;for(a;a>1;a--){c=2*(b+1);b=c;}cout<<"猴子第一天共摘了"<<c<<"个苹果"<<endl;return 0;}

结果:1534
第七:求数列的和

// l.cpp : 定义控制台应用程序的入口点。//功能:计算s[n]=a+aa+aaa+...的值#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,n,c,s;cout<<"请输入一个数(0~9):"<<endl;cin>>a;cout<<"请输入要计算的位数n"<<endl;cin>>n;c=s=a;for(n;n>1;n--){a*=10;c+=a;//c=aa,c=aaa,c=aaaa...s+=c;}cout<<"s[n]="<<s<<endl;return 0;}

第八:乘法表

同第一题程序7


第九:比赛名单

// l.cpp : 定义控制台应用程序的入口点。//功能:编程输出字符组成图形#include "stdafx.h"#include<iostream>using namespace std;struct yidui{char name[20];};int _tmain(int argc, _TCHAR* argv[]){yidui nam[4]={{"ChenLiu"},{"ZhaoQi"},{"SongBa"}};int z3,l4,w5;for(z3=2;z3>=0;z3--)for(l4=2;l4>=0;l4--)for(w5=0;w5<=2;w5++)if(z3!=l4&&l4!=w5&&w5!=z3)//对手不能相同if(z3!=0&&w5!=0&&w5!=2) //cout<<"ZhangSan and "<<nam[z3].name<<endl<<"LiSi and "<<nam[l4].name<<endl<<"WangWu and "<<nam[w5].name<<endl;return 0;}


对于名字的输出,可以使用switch语句进行选择,但会使程序变得冗长;

穷举法能解决很多问题,但很显然在此题中很多计算是毫无意义的,我试图把计算规则添加进去以避开不必要的计算,提升程序执行效率,很可惜,这样通常得不偿失,因为添加规则也属于添加算法,并且还会使得程序冗长,真的没有两全之策吗?


0 0
原创粉丝点击