北京理工大学计算机学院复试上机题目
由于编者水平有限,如有错误,请多多包涵。欢迎各位指正,转载请注明,谢谢合作!
1.一个小球,从高为 H 的地方下落,下落弹地之后弹起高度为下落时的一半,比如第一次弹起高度为 H/2,如此往复,计算从小球 H 高度下落到第 n 次弹地往返的总路程。
要求:1。用递归的方法实现
2。输入 H 和 n,输出结果
3。注意程序的健壮性
4。可以用 C 或 C++实现
#include<iostream>
using namespace std;
double f(double h,int n){
if(n==0)
return h;
return h+f(h/2,n-1);
}
int main(){
double h;
int n;
cout<<"请输入起始高度和反弹次数:";
cin>>h>>n;
cout<<"总路程为:"<<f(h,n)<<endl;
return 0;
}
2.创建一个 CPoint 类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,
运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要
加入自己的成员变量或成员函数
要求: 1.输入两个点的坐标,输出两个点之间的距离;
2.重载运算符为“-”。
#include<iostream>
#include<math.h>
using namespace std;
class CPoint{
public:
double x; // 横坐标
double y; // 纵坐标
CPoint(){}
CPoint(int xl,int yl){
x=xl;
y=yl;
}
double operator -(CPoint& A) const{
return sqrt(pow(x-A.x,2)+pow(y-A.y,2));
}
};
int main(){
int x1,y1,x2,y2;
cout<<"请输入两点的坐标:";
cin>>x1>>y1>>x2>>y2;
CPoint p1(x1,y1),p2(x2,y2);
cout<<"距离为:"<<p1-p2<<endl;
return 0;
}
3.创建一个 CTriangle 类,需要用到第二题中创建的类,即用 3 点来代表一个三角形,
输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。
可以根据需要加入自己的成员变量或成员函数
要求:1.输入三个点的坐标,输出周长并给出是否直角三角形的信息;
2.注意程序的健壮性。
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
class CPoint{
public:
double x; // 横坐标
double y; // 纵坐标
CPoint(){}
CPoint(int xl,int yl){
x=xl;
y=yl;
}
double operator -(CPoint& A) const{
return sqrt(pow(x-A.x,2)+pow(y-A.y,2));
}
};
class Ctriangle{
public:
CPoint p1,p2,p3;
double length[3]; // 三条边的长度
double c; // 周长
Ctriangle(CPoint pf,CPoint ps,CPoint pt){
p1=pf;
p2=ps;
p3=pt;
}
// 判断是否为直角三角形
bool IsRight(){
// 计算三条边的长度
length[0]=p1-p2;
length[1]=p1-p3;
length[2]=p2-p3;
sort(length,length+3);
// 是否是直角三角形
if(pow(length[2],2)==pow(length[0],2)+pow(length[1],2))
return true;
return false;
}
// 计算周长
void cc(){
c=length[0]+length[1]+length[2];
}
};
int main(){
int x1,y1,x2,y2,x3,y3;
cout<<"请输入三角形的三个点的坐标:";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
CPoint p1(x1,y1),p2(x2,y2),p3(x3,y3);
Ctriangle t(p1,p2,p3);
if(t.IsRight()){
cout<<"该三角形是直角三角形,周长为:";
t.cc();
cout<<t.c<<endl;
}
else{
cout<<"该三角形不是直角三角形,周长为:";
t.cc();
cout<<t.c<<endl;
}
return 0;
}
4.请自定义一个 Student 类,属性包括:Charname[10],int num。编程实现学生信息的输
入、查询、浏览,其中浏览分为:升序和降序两种。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Student{
public:
char name[10];
int num;
friend ostream& operator >>(ostream&,Student&);
};
ostream& operator <<(ostream& output,Student& s){
output <<s.num<<" "<<s.name;
return output;
}
// 定义升序排序规则
bool cmp1(Student A,Student B){
return A.num<B.num;
}
// 定义降序排序规则
bool cmp2(Student A,Student B){
return A.num>B.num;
}
int main(){
vector<Student> v;
vector<Student>::iterator it;
Student temp;
int choose;
while(1){
cout<<"1.输入学生信息 2.查询学生信息 3.浏览学生信息: ";
cin>>choose;
switch(choose){
// 输入
case 1:
cout<<"输入姓名、学号: ";
cin>>temp.name>>temp.num;
v.push_back(temp);
break;
// 查询
case 2:
int no;
cout<<"请输入需要查询的学号: ";
cin>>no;
for(it=v.begin();it!=v.end();it++)
if((*it).num==no){
cout<<(*it).num<<" "<<(*it).name<<endl;
break;
}
break;
// 浏览
case 3:
int c;
cout<<"1.升序 2.降序: ";
cin>>c;
// 升序输出
if(c==1){
sort(v.begin(),v.end(),cmp1);
for(it=v.begin();it!=v.end();it++)
cout<<*it<<endl;
}
// 降序输出
else if(c==2){
sort(v.begin(),v.end(),cmp2);
for(it=v.begin();it!=v.end();it++)
cout<<*it<<endl;
}
else{
cout<<"输入无效!"<<endl;
}
break;
default:
cout<<"输入无效!"<<endl;
break;
}
}
return 0;
}