北理复试上机题2007年

1、一个小球,从高为H的地方下落,下落弹地之后弹起高度为下落时的一半,比如第一次弹起高度为H/2,如此反复,计算从小球H高度下落到n次弹地往返的总路程。

要求:(1)用递归的方法实现。(2)输入H和n,输出结果。(3)注意程序的健壮性。(4)可以用C/C++实现。

这题:个人觉得总路程可能因为‘第n次弹起的末位置在哪’的考虑不同,结果不同,但总的来说是掌握递归。

#include<iostream>
using namespace std;
 
double fun(double h, int n){
	if(n==1) return h;
	else{
		return h+h/2.0+fun(h/2.0, n-1);
	}
}
 
int main(){
	double h;
	int n;
	cout<<"请输入起始高度和反弹次数:";
	cin>>h>>n;
	cout<<"总路程为:"<<fun(h,n)<<endl;
	
	return 0;
}

上面的题,可能有其他的说法,这里给出两个参考:参考一  参考二

2、创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要加入自己的成员变量或成员函数。

要求:(1)输入两个点的坐标,输出两个点之间的距离。(2)重载运算符为“-”。

#include<iostream>
#include<cmath>
using namespace std;
/*
2、创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,
运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。
可以根据需要加入自己的成员变量或成员函数。
要求:(1)输入两个点的坐标,输出两个点之间的距离。(2)重载运算符为“-”。
*/
class CPoint{
	private:
		int m_x;
		int m_y;
	public:
		CPoint(){}
		CPoint(int x, int y):m_x(x),m_y(y){}
		void assign(int x, int y){
			m_x=x;
			m_y=y;
		}
		double operator-(CPoint &a);//这里计算两个点之间的距离 
};
double CPoint::operator-(CPoint &a){
	return sqrt(pow(abs(this->m_x-a.m_x), 2)+pow(abs(this->m_y-a.m_y), 2));
}
int main(){
	
	CPoint c1,c2;
	cout<<"请输入两个点的坐标:\n第一个点:";
	int x, y;
	cin>>x>>y;
	c1.assign(x, y);
	cout<<"第二个点:";
	cin>>x>>y;
	c2.assign(x, y); 
	cout<<"距离:"<<c1-c2<<endl;
	return 0;
}

3、创建一个CTriangle类,需要用到第二题中创建的类,即用3点来代表一个三角形,输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。可以根据需要加入自己的成员变量或成员函数。

要求:(1)输入三个点的坐标,输出周长并给出是否是直角三角形的信息。(2)注意程序的健壮性。
 

#include<iostream>
#include<cmath>
using namespace std;
class CPoint{
	private:
		int m_x;
		int m_y;
	public:
		CPoint(){}
		CPoint(int x, int y):m_x(x),m_y(y){}
		void assign(int x, int y){
			m_x=x;
			m_y=y;
		}
		double operator-(CPoint &a);//这里计算两个点之间的距离 
};
double CPoint::operator-(CPoint &a){
	return sqrt(pow(abs(this->m_x-a.m_x), 2)+pow(abs(this->m_y-a.m_y), 2));
}
class CTriangle{
	private:
		CPoint c1, c2, c3;
	public:
		CTriangle(CPoint _c1, CPoint _c2, CPoint _c3):c1(_c1),c2(_c2),c3(_c3){}
		void judge(){
			double b1 = c1-c2;
			double b2 = c1-c3;
			double b3 = c2-c3;
			if(b1+b2<b3||b1+b3<b2||b2+b3<b1){
				cout<<"不能构成三角形"<<endl;
				return;
			}
			if(abs(pow(b1,2)+pow(b2,2)-pow(b3,2))<0.0001||abs(pow(b1,2)+pow(b3,2)-pow(b2,2))<0.0001||abs(pow(b3,2)+pow(b2,2)-pow(b1,2))<0.0001){
				cout<<"是直角三角形"<<endl;
			}else cout<<"不是直角三角形"<<endl;
			cout<<"周长是:"<<b1+b2+b3<<endl;
		}
};

int main(){
	
	CPoint c1,c2, c3;
	cout<<"请输入三个点的坐标:\n第一个点:";
	int x, y;
	cin>>x>>y;
	c1.assign(x, y);
	cout<<"第二个点: ";
	cin>>x>>y;
	c2.assign(x, y); 
	cout<<"第三个点: ";
	cin>>x>>y;
	c3.assign(x, y); 
	CTriangle c(c1, c2, c3);
	c.judge();
	return 0;
}

4、请自定义一个Student类,属性包括,Char name[10],int num。编程实现学生信息的输入、查询、浏览,其中浏览分为:升序和降序两种

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstddef>//unllptr
using namespace std;
/*
4、请自定义一个Student类,属性包括,Char name[10],int num。编程实现学生信息的输入、查询、浏览,其中浏览分为:升序和降序两种
*/ 
class Student{
	private:
		char name[10];
		int num;
	public:
		Student(){}
		Student(char _name[], int _num){
			for(int i=0;i<10;i++)
				name[i]=_name[i];
			num=_num;
		}
		void assign(char _name[10], int _n){
			for(int i=0;i<10;i++)
				name[i]=_name[i];
			num=_n;
		}
		char* getName(){
			return name;
		}
		int getNum(){
			return num;
		}
	public:
		Student getInfoByNum(int n);
};
bool comAscend(Student a, Student b){
	return a.getNum()<b.getNum();
}
bool comDescend(Student a, Student b){
	return a.getNum()>b.getNum();
}
void browser(vector<Student>& table){
	cout<<"\n请选择按照什么方式:1.升序排序。2.降序排序。\n";
	int se;
	cin>>se;
	if(se==1){
		sort(table.begin(), table.end(), comDescend);//直接传入函数名字 
	}else if(se==2){
		sort(table.begin(), table.end(), comAscend);
	}else{
		cout<<"没有这个选项"<<endl;
		return;
	}
	for(vector<Student>::iterator it=table.begin();it!=table.end();it++){
		cout<<(*it).getName()<<" "<<(*it).getNum()<<endl;
	}
}
void select(vector<Student> table){
	cout<<"请输入你要查询的学生的编号:";
	int num;
	cin>>num;
	for(int i=0;i<table.size();i++){
		Student s = table[i];
		if(s.getNum()==num)
			cout<<s.getName()<<" "<<s.getNum()<<endl;
	}
} 
void typeIn(vector<Student>& table){
	cout<<"正在键入学生信息:你需要键入几个学生的信息:";
	int total;
	cin>>total;
	while(total){
		cout<<"请输入学生姓名和学号:";
		char name[10];
		cin>>name;
		int num;
		cin>>num;
		Student c(name, num);
		table.push_back(c);
		total--;
	}
}

int main(){
	vector<Student> table;
	while(1){
		cout<<"请输入所需要的功能:1.输入学生信息 2.查询学生信息 3.浏览学生信息。输入其他则退出:"<<endl;
		int option;
		cin>>option;
		if(option==1){
			typeIn(table); 
		}else if(option==2){
			browser(table);
		}else if(option==3){
			select(table);
		}else break;
	} 
	return 0;
}

作者:无涯明月

上篇: 北理复试上机题2006年

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/88345121