部分oj题及答案_第1页
部分oj题及答案_第2页
部分oj题及答案_第3页
部分oj题及答案_第4页
部分oj题及答案_第5页
已阅读5页,还剩213页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一1.Description编写函数把华氏温度转换为摄氏温度,公式为:c=(F-32)*5/9在主程序中用户输入一个华氏温度,转化后输出相应的摄氏温度。Input输入一个华氏温度数Output输出一个相应的摄氏温度数Sample Input77Sample Output25HINTAppend Code#includeusing namespace std;int main()int a,b;cina;b=(a-32)*5/9;coutbendl;return 0;2.Description用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入输出。递归公式:Input依次输入两个整数

2、,对应于n的值和x的值Output输出pn(x)对应的值Sample Input3 4Sample Output154HINTAppend Code#includeusing namespace std;double f(int k,int m)if(k=0)return 1;else if(k=1)return m;elsereturn (2*k-1)*m*f(k-1,m)-(k-1)*f(k-2,m)/k;int main()int n,x;cinnx;coutf(n,x)endl;return 0;3.Description编写递归函数getPower计算xy,在同一个程序汇总针对整型和

3、实型实现两个重载的函数:int getPower(int x,int y); /整型形式,当y0时,返回0double getPower(double x, double y);/实型形式在主程序中实现输入输出,分别输入一个整数a和一个实数b作为底数,再输入一个整数m作为指数,输出am和bm。Input分别输入一个整数a和一个实数b作为底数,再输入一个整数m作为指数Output输出am和bmSample Input235Sample Output32243HINTAppend Code#includeusing namespace std;int getPower(int x,int y)if

4、(y0)return 0;if(y=0)return 1;elsereturn x*getPower(x,y-1);double getPower(double x, double y)if(yabm;c=getPower(a,m);d=getPower(b,double (m);coutcendldendl;return 0;4.Description编写一个完整的程序,运行时向用户提问你考试考了多少分?(0-100)接受输入后判断其等级并显示出来等级:优:90=分数=100良:80=分数90中:60=分数80差:0=分数60Input输入任意一个整数分数值,显示等级; 再输入任意一个整数分

5、数值,显示等级; .直到测试数据较充分,可输入-1止。Output对任意输入的分数值,输出对应的等级,直到输入的数为-1时才退出运行.Sample Input10210090807060500-80-1Sample Outputgrad must between 0 and 100优优良中中差差grad must between 0 and 100grad must between 0 and 100HINTAppend Code#includeusing namespace std;int main()int a=1,s10000,i,j;s0=1;for(i=0;si!=-1;)i+;ci

6、nsi;for(j=1;j=i;j+)if(sj=90)cout优endl;else if(sj=80)cout良endl;else if(sj=60)cout中=0&sj60)cout差endl;elsecoutgrad must between 0 and 100endl;return 0;5.Description用穷举法找出1-100的质数并显示出来.Input无Output依次输出1-100之间的所有的质数,用空格分隔.Sample InputSample Output2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

7、 79 83 89 97HINTAppend Code#includeusing namespace std;int main()int i,n;for(i=1;i=100;i+)for(n=2;ni;n+)if(i%n=0)break;if(i=n)couti ;return 0;二1.Description定义一个tree类,有成员ages,成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值。int main()Tree t(12);t.age();t.grow(4);return 0;InputOutput这棵树的年龄为12这棵树的年

8、龄为16Sample InputSample Output这棵树的年龄为12这棵树的年龄为16HINTAppend Code#includeusing namespace std;class Treeprivate:int ages;public:Tree(int);void grow(int years)cout这棵树的年龄为years+agesendl;void age();Tree:Tree(int a)ages=a;void Tree:age()cout这棵树的年龄为agesendl;int main()Tree t( 12);t.age();t.grow(4);return 0;2.

9、Description定义一个Dog 类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。int main()Dog Jack(2,10);cout Jack is a Dog who is ;cout Jack.GetAge() years old and ;cout Jack.GetWeight() pounds weight.n;Jack.SetAge(7);Jack.SetWeight(20);cout Now Jack is ;cout Jack.GetAge() years old and ;cout Jack.GetWeight() pounds w

10、eight.;return 0;InputOutput输出dog类的年龄和重量Sample InputSample OutputJack is a Dog who is 2 years old and 10 pounds weight.Now Jack is 7 years old and 20 pounds weight.HINT注意头文件不要使用iostream.h,需要使用标准c+头文件编写相应的构造,析构以及各个操作函数,使得主函数能正确输出相应结果。Append Code#includeusing namespace std;class Dogprivate:int age,weig

11、ht;public:Dog(int ,int );int GetAge()return age;int GetWeight()return weight;void SetAge(int a);void SetWeight(int w);Dog:Dog(int a,int w)age=a;weight=w;void Dog:SetAge(int a)age=a ;void Dog:SetWeight(int w )weight=w;int main()Dog Jack(2,10);cout Jack is a Dog who is ;cout Jack.GetAge() years old an

12、d ;cout Jack.GetWeight() pounds weight.n;Jack.SetAge(7);Jack.SetWeight(20);cout Now Jack is ;cout Jack.GetAge() years old and ;cout Jack.GetWeight() pounds weight.;return 0;3.Description设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。int main()Rectangle MyRectangle (50, 20, 100, 80 );int Area =

13、 MyRectangle.GetArea();cout Area: Area n;return 0;InputOutput输出矩形的面积Sample InputSample OutputArea: 3000HINT编写相应的构造和操作函数Append Code#includeusing namespace std;class Rectangleprivate:int x1,y1,x2,y2;public:Rectangle(int, int, int, int);int GetArea();Rectangle:Rectangle(int a, int b , int c , int d)x1=

14、a;y1=b;x2=c;y2=d;int Rectangle:GetArea()return (x2-x1)*(y2-y1);int main()Rectangle MyRectangle (50, 20, 100, 80 );int Area = MyRectangle.GetArea();cout Area: Area radius;Circle p(radius);cout this radius is:radius and its area is: p.GetArea () endl;Input圆的半径Output圆的半径和面积Sample Input5Sample Outputthi

15、s radius is:5 and its area is:78.5HINT实现构造函数,getarea函数Append Code#includeusing namespace std;class Circleprivate:float Radius;public:Circle(float);float GetArea()return 3.14*Radius*Radius;Circle:Circle(float r)Radius=r;int main()float radius;cin radius;Circle p(radius);cout this radius is:radius and

16、 its area is: p.GetArea () cases;for(int i = 1; i point_type;if(point_type = 2)cinxy;Point p(x, y);p.showPoint();if(point_type = 3)cinxyz;Point_3D p(x, y, z);p.showPoint();#include using namespace std; class Point private: int x,y; public: Point(); Point(double a,double b)x=a;y=b; void showPoint() c

17、out2D Point (x,y)endl; ; class Point_3D private: int x,y,z; public: Point_3D(); Point_3D(double a,double b,double c)x=a;y=b;z=c; void showPoint() cout3D Point (x,y,z)cases; for(int i = 1; i point_type; if(point_type = 2) cinxy; Point p(x, y); p.showPoint(); if(point_type = 3) cinxyz; Point_3D p(x, y

18、, z); p.showPoint(); 2.Description设计一个时间类和一个日期类,用于读取输入的数据,按格式输出日期和时间。设计日期类Date需支持以下操作:Date:Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。Date:showDate()按格式输出Date对象。设计时间类Time需支持以下操作:Time:Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。Time:showTime()按格式输出Time对象。-你设计Date类和Time类,使得main()函数能够正确运行。函数调用格式见appen

19、d.cc。append.cc中已给出main()函数。Input输入的第一个整数n,表示有n组测试数据。后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。Output每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。Sample Input31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01Sample Output1982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:

20、01HINT输出格式用头文件中流操作算子:setw(w) :设置数据的输出宽度为w个字符setfill(c):设置用字符c作为填充字符Append Codeappend.cc,int main()int cases;cin cases;for(int ca = 0; ca year month day;Date date(year, month, day);date.showDate();cout hour minute second;Time time(hour, minute, second);time.showTime();cout endl;#include #include usin

21、g namespace std; class Date private: int x,y,z; public: Date(int x1 ,int y1,int z1)x=x1;y=y1;z=z1; void showDate() coutx-setw(2)setfill(0)y-setw(2)setfill(0)z; ; class Time private: int a,b,c; public: Time(int a1,int b1,int c1) a=a1; b=b1; c=c1; void showTime() coutsetw(2)setfill(0)a:setw(2)setfill(

22、0)b:setw(2)setfill(0) cases; for(int ca = 0; ca year month day; Date date(year, month, day); date.showDate(); cout hour minute second; Time time(hour, minute, second); time.showTime(); cout endl; 3.Description设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;设计DateTime类需支持

23、以下操作:DateTime:DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;DateTime:DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;DateTime:DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;DateTime:showDateTime()方法:按格式输出DateTime对象;DateTime:setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;Da

24、teTime类包含了两个类:Date类和Time类设计日期类Date需支持以下操作:Date:Date()无参构造方法:初始化为1年1月1日Date:Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。Date:showDate()方法:按格式输出Date对象。Date:setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值设计时间类Time需支持以下操作:Time:Time()无参构造方法:初始化为0时0分0秒Time:Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。Time:s

25、howTime()方法:按格式输出Time对象。Time:setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值-你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。函数调用格式见append.cc。append.cc中已给出main()函数。Input输入的第一个整数n,表示有n组测试数据。后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。Output每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。S

26、ample Input31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01Sample Output1000-10-10 01:01:011982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01HINT输出格式用头文件中流操作算子:setw(w) :设置数据的输出宽度为w个字符setfill(c):设置用字符c作为填充字符Append Codeappend.cc,int main()Date date(1000, 10, 10);Time time(1, 1, 1);DateTime d

27、ate_time(date, time);date_time.showDateTime();cout cases;for(int ca = 0; ca year month day;int hour, minute, second;cin hour minute second;if(flag = 0)flag = 1;DateTime dt(year, month, day, hour, minute, second);dt.showDateTime();else if(flag = 1)flag = 0;date_time.setDateTime(year, month, day, hour

28、, minute, second).showDateTime();cout endl;#include #include using namespace std; class Date private: int year,month,day; public: Date(int y=1,int m=1,int d=1) year=y; month=m; day=d; void showDate() coutsetw(4)year-setw(2)setfill(0)month-setw(2)setfill(0)day ; ; class Time public: Time(int h=0,int

29、m=0,int s=0) hour=h;minute=m;second=s; void showTime() coutsetw(2)setfill(0)hour:setw(2)setfill(0)minute:setw(2)setfill(0)second; private: int hour,minute,second; ; class DateTime private: Date date; Time time; public: DateTime(int year1,int month1,int day1,int hour1,int minute1,int second1) Date da

30、tes(year1, month1, day1); date= dates; Time times(hour1, minute1, second1); time = times; DateTime(Date date1,Time time1) date=date1;time=time1; void showDateTime() date.showDate(); time.showTime(); DateTime setDateTime(int year1,int month1,int day1,int hour1,int minute1,int second1) DateTime dateti

31、me(year1,month1,day1,hour1,minute1,second1); return datetime; ; int main() Date date(1000, 10, 10); Time time(1, 1, 1); DateTime date_time(date, time); date_time.showDateTime(); cout cases; for(int ca = 0; ca year month day; int hour, minute, second; cin hour minute second; if(flag = 0) flag = 1; Da

32、teTime dt(year, month, day, hour, minute, second); dt.showDateTime(); else if(flag = 1) flag = 0; date_time.setDateTime(year, month, day, hour, minute, second).showDateTime(); cout endl; /已经看完了4.Description设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;设计DateTime类需支持以下操

33、作:DateTime:DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;DateTime:DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;在上述两个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d, hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。DateTime:DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;在这个DateTime类

34、的构造函数中输出:“CREATE DateTime : (y, m, d) (hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。DateTime:showDateTime()方法:按格式输出DateTime对象;DateTime:setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;DateTime类包含了两个类:Date类和Time类设计日期类Date需支持以下操作:Date:Date()无参构造方法:初始化为1年1月1日Date:Date(int,int,

35、int)构造方法:传入的参数依次为年月日,用参数将日期初始化。在Date类的构造函数中输出:“CREATE Date : (y, m, d)”,其中y、m、d为初始化对象时的年月日值。参见输出。Date:showDate()方法:按格式输出Date对象。Date:setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值设计时间类Time需支持以下操作:Time:Time()无参构造方法:初始化为0时0分0秒Time:Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。在Time类的构造函数中输出:“CREATE Time

36、: (h, m, s)”,其中h、m、s为初始化对象时的时分秒值。参见输出Time:showTime()方法:按格式输出Time对象。Time:setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值-你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。函数调用格式见append.cc。append.cc中已给出main()函数Input输入的第一个整数n,表示有n组测试数据。后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。Output每组测试数据对应一行输出。日期的输出格式为“

37、yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。Sample Input31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01Sample OutputCREATE Time : (0, 0, 0)CREATE Date : (1, 1, 1)CREATE DateTime : (1, 1, 1, 0, 0, 0)0001-01-01 00:00:001982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01HINT输出格式用头文件中流操作算子:setw(w) :设置数据的输出宽度为w个字符setfill(c):设置用字符c作为填充字符Append Codeappend.cc,int main()DateTime date_time;date_time.showDateTime();cout cases;for(int ca = 0; ca year month day;int hour, minute,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论