已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第6周:生成N个随机数(1-100),排序后输出。要求1、使用数组实现。2、使用vector实现。#includeusing namespace std;int main()const int N=100;vector vec;srand(time(0);for(int i=0;iN;+i)vec.push_back(rand()100+1);void print(vector);print(vec);coutendl;for(anto i=0;i=vec.size()-2;+i)int k=I;for(int j=i+1;jvecj) k=j;int temp=veck;veck=veci;veci=temp;第7周 :1生成N个随机数( 0-99),统计落在区间10*i,10*i+9(i:0-9)内的整数个数。#include#includeusing namespace std;int main()const int N=10000;int cnt=0;bool isPrime(int);for(int i=0;i!=N;+i)int temp=rand()%10000+1;if(isPrime(temp)+cnt;cout(double)cnt/N*100%endl;return 0;bool isPrime(int n)if(n=1)return false;for(int i=2;isqrt();+i)if(n%i=0)return false;return true;2生成N个随机整数(范围:1-10000), 统计其中质数个数。判定质数要求写成一个函数。#include#include#includeusing namespace std;int main()const int N=10000;static bool aN+1;for(auto&val:a) val=!val;al=false;for(int i=2;i=sqrt(N);i+)for(int j=2*i;J=N&ai;j+=i) aj=false;int cnt=0;for(int i=1;i=N;+i)if(ai)couti,;+cnt;if(cnt%10=0) coutendl;coutendlcntendl;return 0;3.利用筛选法十个一行输出10000内的质数。筛选法概念:请百度。#includeusing namespace std;int main()const int N=100;vector vec;srand(time(0);for(int i=0;iN;+i)vec.push_back(rand()100+1);void print(vector);print(vec);coutendl;for(anto i=0;i=vec.size()-2;+i)int k=I;for(int j=i+1;jvecj) k=j;int temp=veck;veck=veci;veci=temp;第11周1. 实现一个Point类并测试;(要求Point类至少有一个或多个构造函数,一个求两点之间的距离函数,和一个求该点到原点距离的函数)#includeusing namespace std;int main() Point p1(1,2); Point p2(3,4); coutp1.distendl; coutp1.dist(p2)endl; return 0;double dist() return sqrt(x*x+y*y);double dist(Point p2) return sqrt(p2.x-x)*(p2.x-x)+(p2.y-y)*(p2.y-y);2. 实现一个三角形类并测试;(要求三角形类至少有一个或多个构造函数,一个求面积的函数area和一个求周长的函数circumference)#include#includeusing namespace std;class Pointpublic:Point(int x,int y)this-x=x;this-y=y; double dist(Point p) return sqrt(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y); private:int x;int y; class Trianglepublic:Triangle(Point p1,Point p2,Point p3)a=p1.dist(p2);b=p1.dist(p3);c=p2.dist(p3);double circumference()return (a+b+c);double area()double p=(a+b+c)/2.0;return sqrt(p*(p-a)*(p-b)*(p-c);private:double a;double b;double c;int main()Point p1(1,1);Point p2(1,5);Point p3(4,1);Triangle T(p1,p2,p3);coutcircumference is T.circumference()endl;coutarea is T.area()endl;第8周:1. 设计一复数类 Complex,实现复数的加减运算。(参考百度,教程)#includeusing namespace std;class Complexpublic: Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex add(Complex c2);Complex sub(Complex c2);double real;double imag;int main()Complex c1;c1.real=1.1;c1.imag=1.2;coutc1.real+c1.imagi;coutc1.real-c1.imagi;Complex add(Complex c1,Complex c2)Complex temp;temp.real=c1.real+c2.real;temp.real=c1.imag+c2.imag;return temp;Complex sub(Complex c1,Complex c2)Complex temp;temp.real=c1.real-c2.real;temp.real=c1.imag-c2.imag;return temp;2. 用筛选法输出10000以内的质数。(同第7周题3)第13周:玛雅历#include #include #include using namespace std; string haab19=pop,no,zip,zotz,tzec,xul,yoxkin,mol,chen,yax,zac,ceh,mac,kankin,muan,pax,koyab,cumhu,uayet; string tzolkin20=imix,ik,akbal,kan,chicchan,cimi,manik,lamat,muluk,ok,chuen,eb,ben,ix,mem,cib,caban,eznab,canac,ahau; int main() int turns; int day,year; string month; char dot; cinturns; coutturns0) cinday; cindot; cinmonth; cinyear; int i; for(i=0;i19;i+) if(haabi=month) break; int date; date=day+20*i+year*365; year=date/260; date=date%260; int mth=date%20; day=date%13; coutday+1 tzolkinmth yearendl; return 0; 第14周:已知今天(2014年12月8日)是星期一,输入一个日子(如2046年12月23日),输出该日的星期,输出该月的星期呢?(思考)#include using namespace std;int leapYear(int year);int main()cout 请先输入年份,再输入月份,最后输入日期 a b c;int i, result = 0;for(i=1;i=a-1;i+)result+=(leapYear(i)+365)%7;for(i=1;i=b-1;i+)if(i=1|i=3|i=5|i=7|i=8|i=10|i=12)result+=31%7;result%=7;else if(i=4|i=6|i=9|i=11)result+=30%7;result%=7;else if(i=2)result+=(28+leapYear(a)%7;result%7;result+=c%7;result%=7;if(result=0)result = 7;cout 今儿是星期 result endl;return 0;/判断是否为闰年,若为闰年返回1(366天),反之,返回0(365天).int leapYear(int year)if(year%400=0 | year%100!=0 & year%4=0)return 1;elsereturn 0;第16周:1. 定义一个基类Person,数据成员包含name用于保存姓名,函数成员包括默认构造函数、带参构造函数用于初始化数据成员和输出姓名的成员函数PrintName()。从Person类派生出Student类,增加长整型数据成员Number用于保存学号,函数成员包括带参构造函数用于初始化数据成员和输出学生信息的成员函数PrintInfo()。要求:在函数PrintInfo()中需要调用基类的成员函数PrintName()。测试程序:int main()Student b(ssh,100);b.PrintInfo();运行结果:Name of Student: sshNumber of Student: 100#includeusing namespace std;class Personpublic:Person();person(string newname);void PrintName();private:string name;person:person(string newname)name=newname;void person:person(string newname)coutname of student:nameendl;void Student:public Personpublic:student(string newname,string sname,long int newnumber):Person(newname);Student1(sname);Number=number;void printinfo;private:person student1;long int Number;void student:Printinfo()coutthe students name is as followsendl;student1.printname();coutstudents registration number is as followsendl;coutNumber of student:Numberendl;int main()student:b(ssh,100);b.Printinfo();2. 编程体会包含内嵌对象多继承派生类的带参构造函数和析构函数调用顺序。声明三个基类Base1、Base2和Base3,然后从这三个基类按照公有方式派生出类Derived。在三个基类中分别定义带一个整型参数的构造函数和析构函数输出提示信息,提示信息中需要包含整型参数的数值。在派生类Derived中需要包含三个基类的内嵌对象,和相应的构造函数和析构函数用于输出提示信息,在构造函数中包含七个整型参量,其中六个用于初始化派生类和内嵌对象。测试程序:int main() Derived objD(1,2,3,4,5,6,7); return 0;运行结果:Base2 constructor called 3Base1 constructor called 2Base3 constructor called 4Base3 constructor called 7Base1 constructor called 5Base2 constructor called 6Derived constructor called 1Derived destructor calledBase2 destructor calledBase1 destructor calledBase3 destructor calledBase3 destructor calledBase1 destructor calledBase2 destructor called#includeusing namespace std;class Basepublic:Base(int i)coutBase constructor callediendl;Base(void)coutBase destructor calledendl;class Base1:public BaseBase1(int i,int j):Base(j)coutBase1 constructor callediendl;Base1(void)coutBase1 destructor calledendl;class Base2public:Base2(int i)coutBase2 constructor callediendl;Base2(void)coutBase2 destructor calledendl;class Base3public:Base3(int i)coutBase3 constructor callediendl;Base3(void)coutBase3 destructor calledendl;class Derived:public Base2,public Base1,public Base3public:Derived(int i,int a,int b,int c,int d,int e,int f,int g):Base1(g,a),member2(e),member1(g,d),Base2(b),member3(f),Base3(c),member(i)private:Base1 member1;Base2 member2;Base3 member3;Base member;int main()Derived obj(1,2,3,4,5,6,7)return 0;第17周:设计一复数类 Complex,实现复数的+,-,+等运算。#includeusing namespace std;class Complexpublic:Complex(double r=0.0,double i=0.0)real=r;imag=i;Complex operator+(c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/CSEA 35-2025纺织机械零件热喷涂陶瓷涂层 技术条件
- T/UNP 415-2024高性能汽车燃油喷射系统用不锈钢功能材料
- T/GSET 001-2025不同品牌、不同型号电梯群控调度方法和要求
- T/CI 864-2024市政管道清淤施工及验收技术规范
- T/CAFFCI 70-2023食品添加剂 缬草油
- T/CSAE 350-2024轮胎静态刚度特性通用试验方法
- 2025~2026学年江苏省南通市通州区苏教版五年级上册期中数学试卷【附答案】
- 2026学年湖南省常宁市六年级数学期末点睛提升能力提升卷详细参考解析详细答案和解析
- Photoshop专业技能考试试题及详细答案
- 2026年国家公务员政治理论应知应会知识试题库与答案
- 机器学习 课件全套 张旭东 第1-18讲 -概述、统计基础 -深度生成模型-3
- 2026住院医师规培-重庆-重庆住院医师规培(口腔全科)历年参考题库含答案详解
- 2025年10月 手机媒体概论(14237)自考真题
- 2026-2027学年五年级语文上册第三单元习作《故事新编》教学教学课件
- 2026建设银行校招笔试真题及答案
- GB/T 48023-2026数据中心冷板式液冷系统技术规范
- 2026年小学必读书目《安徒生童话》阅读测试试题及答案
- 食品加工厂厂长产品质量与生产效率绩效衡量表
- 旋挖成孔灌注桩施工监理实施细则
- DBJ33∕T 1020-2022 建设工程地质钻探安全技术规程 附条文说明
- 急救、生命支持类设备应急调配手册
评论
0/150
提交评论