您好,欢迎访问三七文档
一1.Description编写函数把华氏温度转换为摄氏温度,公式为:c=(F-32)*5/9在主程序中用户输入一个华氏温度,转化后输出相应的摄氏温度。Input输入一个华氏温度数Output输出一个相应的摄氏温度数SampleInput77SampleOutput25HINTAppendCode#includeiostreamusingnamespacestd;intmain(){inta,b;cina;b=(a-32)*5/9;coutbendl;return0;}2.Description用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入输出。递归公式:Input依次输入两个整数,对应于n的值和x的值Output输出pn(x)对应的值SampleInput34SampleOutput154HINTAppendCode#includeiostreamusingnamespacestd;doublef(intk,intm){if(k==0)return1;elseif(k==1)returnm;elsereturn((2*k-1)*m*f(k-1,m)-(k-1)*f(k-2,m))/k;}intmain(){intn,x;cinnx;coutf(n,x)endl;return0;}3.Description编写递归函数getPower计算xy,在同一个程序汇总针对整型和实型实现两个重载的函数:intgetPower(intx,inty);//整型形式,当y0时,返回0doublegetPower(doublex,doubley);//实型形式在主程序中实现输入输出,分别输入一个整数a和一个实数b作为底数,再输入一个整数m作为指数,输出am和bm。Input分别输入一个整数a和一个实数b作为底数,再输入一个整数m作为指数Output输出am和bmSampleInput235SampleOutput32243HINTAppendCode#includeiostreamusingnamespacestd;intgetPower(intx,inty){if(y0)return0;if(y==0)return1;elsereturnx*getPower(x,y-1);}doublegetPower(doublex,doubley){if(y0)return0;if(y==0)return1;elsereturnx*getPower(x,y-1);}intmain(){inta,m,c;doubleb,d;cinabm;c=getPower(a,m);d=getPower(b,double(m));coutcendldendl;return0;}4.Description编写一个完整的程序,运行时向用户提问你考试考了多少分?(0-100)接受输入后判断其等级并显示出来等级:优:90=分数=100良:80=分数90中:60=分数80差:0=分数60Input输入任意一个整数分数值,显示等级;再输入任意一个整数分数值,显示等级;....直到测试数据较充分,可输入-1止。Output对任意输入的分数值,输出对应的等级,直到输入的数为-1时才退出运行.SampleInput10210090807060500-80-1SampleOutputgradmustbetween0and100优优良中中差差gradmustbetween0and100gradmustbetween0and100HINTAppendCode#includeiostreamusingnamespacestd;intmain(){inta=1,s[10000],i,j;s[0]=1;for(i=0;s[i]!=-1;){i++;cins[i];}for(j=1;j=i;j++){if(s[j]=100&&s[j]=90)cout优endl;elseif(s[j]90&&s[j]=80)cout良endl;elseif(s[j]80&&s[j]=60)cout中endl;elseif(s[j]=0&&s[j]60)cout差endl;elsecoutgradmustbetween0and100endl;}return0;}5.Description用穷举法找出1-100的质数并显示出来.Input无Output依次输出1-100之间的所有的质数,用空格分隔.SampleInputSampleOutput2357111317192329313741434753596167717379838997HINTAppendCode#includeiostreamusingnamespacestd;intmain(){inti,n;for(i=1;i=100;i++){for(n=2;ni;n++)if(i%n==0)break;if(i==n)couti;}return0;}二1.Description定义一个tree类,有成员ages,成员函数grow(intyears)对ages加上years,age()显示tree对象的ages的值。intmain(){Treet(12);t.age();t.grow(4);return0;}InputOutput这棵树的年龄为12这棵树的年龄为16SampleInputSampleOutput这棵树的年龄为12这棵树的年龄为16HINTAppendCode#includeiostreamusingnamespacestd;classTree{private:intages;public:Tree(int);voidgrow(intyears){cout这棵树的年龄为years+agesendl;}voidage();};Tree::Tree(inta){ages=a;}voidTree::age(){cout这棵树的年龄为agesendl;}intmain(){Treet(12);t.age();t.grow(4);return0;}2.Description定义一个Dog类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。intmain(){DogJack(2,10);coutJackisaDogwhois;coutJack.GetAge()yearsoldand;coutJack.GetWeight()poundsweight.\n;Jack.SetAge(7);Jack.SetWeight(20);coutNowJackis;coutJack.GetAge()yearsoldand;coutJack.GetWeight()poundsweight.;return0;}InputOutput输出dog类的年龄和重量SampleInputSampleOutputJackisaDogwhois2yearsoldand10poundsweight.NowJackis7yearsoldand20poundsweight.HINT注意头文件不要使用iostream.h,需要使用标准c++头文件编写相应的构造,析构以及各个操作函数,使得主函数能正确输出相应结果。AppendCode#includeiostreamusingnamespacestd;classDog{private:intage,weight;public:Dog(int,int);intGetAge(){returnage;}intGetWeight(){returnweight;}voidSetAge(inta);voidSetWeight(intw);};Dog::Dog(inta,intw){age=a;weight=w;}voidDog::SetAge(inta){age=a;}voidDog::SetWeight(intw){weight=w;}intmain(){DogJack(2,10);coutJackisaDogwhois;coutJack.GetAge()yearsoldand;coutJack.GetWeight()poundsweight.\n;Jack.SetAge(7);Jack.SetWeight(20);coutNowJackis;coutJack.GetAge()yearsoldand;coutJack.GetWeight()poundsweight.;return0;}3.Description设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。intmain(){RectangleMyRectangle(50,20,100,80);intArea=MyRectangle.GetArea();coutArea:Area\n;return0;}InputOutput输出矩形的面积SampleInputSampleOutputArea:3000HINT编写相应的构造和操作函数AppendCode#includeiostreamusingnamespacestd;classRectangle{private:intx1,y1,x2,y2;public:Rectangle(int,int,int,int);intGetArea();};Rectangle::Rectangle(inta,intb,intc,intd){x1=a;y1=b;x2=c;y2=d;}intRectangle::GetArea(){return(x2-x1)*(y2-y1);}intmain(){RectangleMyRectangle(50,20,100,80);intArea=MyRectangle.GetArea();coutArea:Area\n;return0;}4.Description定义一个Circle类,有数据成员半径Radius,成员函数GetArea(),计算圆的面积,构造一个Circle的对象进行测试。intmain(){floatradius;cinradius;Circlep(radius);coutthisradiusis:radiusanditsareais:p.GetArea()endl;}Input圆的半径Output圆的半径和面积SampleInput5SampleOutputthisradiusis:5anditsareais:78.5HINT实现构造函数,getarea函数AppendCode#includeiostreamusingnamespacestd;classCircle{private:floatRadius;public:Circle(float);floatGetArea(){return3.14*Radius*Radius;}};Circle::Circle(floatr){Radius=r;}intmain(){floatradius;cinradius;Circlep(radius);coutthisradiusis:radiusanditsareais:p.GetArea()endl;}三1.Description设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。设计Point类需支持一下操作:Point::Point()无参构造。Point::Point(double,double)两个坐标参数构造。Point::showPoint()按格式输出Poin
本文标题:部分oj题及答案
链接地址:https://www.777doc.com/doc-2011792 .html