2007年北理复试上机题

来源:互联网 发布:p2p下载软件推荐 编辑:程序博客网 时间:2024/05/01 15:12

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

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

#include <iostream>#include <cmath>using namespace std;double f(double n,double h){    if(n==1)        return h+h/2;    else        return f(n-1,h)+h/pow(2,n)+h/pow(2,n-1);}// h+h/2  h+h/2+ h/2+h/4  h+h/2+ h/2+h/4 + h/4+h/8int main(){    double h,n;    cout<<"请输入高度:"<<endl;    while(cin>>h)    {        if(h==0)            break;        cout<<"请输入次数:"<<endl;        cin>>n;        cout<<"总路程为:"<<f(n,h)<<endl;        cout<<"请输入高度:"<<endl;    }    return 0;}

#include <iostream>#include <math.h>using namespace std;int main(){    int n;    double h;    while(cin>>h)    {        if(h==0)            break;        cin>>n;        double sum=0;        for(int i=0; i<=n; i++)        {            if(i==0)                sum+=h;            else if(i==n)                sum+=h/pow(2,i);            else                sum+=h/pow(2,i-1);        }        cout<<sum<<endl;    }    return 0;}
2、创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要加入自己的成员变量或成员函数。

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

#include <iostream>#include <cmath>using namespace std;class Cpoint{private:    double x;    double y;public:    Cpoint() {};    Cpoint(double a,double b):x(a),y(b) {};    double operator-(Cpoint a)    {        return (sqrt(pow(x-a.x,2)+pow(y-a.y,2)));    }};int main(){    double a,b;    cout<<"请输入平面上第一个点的坐标:"<<endl;    cin>>a>>b;    Cpoint c(a,b);    cout<<"请输入平面上的第二个点:"<<endl;    cin>>a>>b;    Cpoint d(a,b);    cout<<"该两点之间的距离为:"<<c-d<<endl;    return 0;}

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

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

#include<iostream>#include <cmath>#include<algorithm>using namespace std;class Cpoint{private:    double x;    double y;public:    Cpoint() {};    Cpoint(double a,double b):x(a),y(b) {};    double operator-(Cpoint a)    {        return (sqrt(pow(x-a.x,2)+pow(y-a.y,2)));    }};int main(){    double a,b,aa[3];    cout<<"请输入平面上第一个点的坐标:"<<endl;    cin>>a>>b;    Cpoint c(a,b);    cout<<"请输入平面上的第二个点的坐标:"<<endl;    cin>>a>>b;    Cpoint d(a,b);    cout<<"请输入平面上第三个点的坐标:"<<endl;    cin>>a>>b;    Cpoint e(a,b);    aa[0]=c-d;    aa[1]=c-e;    aa[2]=d-e;    sort(aa,aa+3);    if(pow(aa[2],2)==(pow(aa[0],2)+pow(aa[1],2)))        cout<<"这是一个直角三角形。"<<endl;    else        cout<<"这不是直角三角形。"<<endl;}

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

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;class student{private:    string name;    int num;public:    student() {};    student(string a,int i):name(a),num(i) {};    string showname()    {        return name;    }    int shownum()    {        return num;    }};bool key1(student a,student b){    return(a.shownum()>b.shownum());}bool key2(student a,student b){    return(a.shownum()<b.shownum());}int main(){    vector<student> stu;    string a;    int i,n=-1,key;    cout<<"请输入学生信息姓名学号,以0 0结束:"<<endl;    while(cin>>a>>i)    {        if(a=="0")            break;        student s(a,i);        stu.push_back(s);    }    cout<<"请输入要查询学生的学号,以0结束:"<<endl;    while(cin>>n)    {        if(n==0)            break;        vector<student>::iterator j;        for(j=stu.begin(); j!=stu.end(); j++)        {            if((*j).shownum()==n)                cout<<"这是"<<(*j).showname()<<"的学号"<<endl;        }        cout<<"请输入要查询学生的学号,以0结束:"<<endl;    }    cout<<"请输入所需要的功能:1、按学号升序浏览,2、按学号降序浏览。输入其他则退出:"<<endl;    while(cin>>key)    {        if(key==1)        {            sort(stu.begin(),stu.end(),key2);            vector<student>::iterator j;            for(j=stu.begin(); j!=stu.end(); j++)                cout<<" 学 号 :"<<(*j).shownum()<<" 姓 名 : "<<(*j).showname()<<endl;        }        else if(key==2)        {            sort(stu.begin(),stu.end(),key1);            vector<student>::iterator j;            for(j=stu.begin(); j!=stu.end(); j++)                cout<<" 学 号 :"<<(*j).shownum()<<" 姓 名 : "<<(*j).showname()<<endl;        }        else            break;        cout<<"请输入所需要的功能:1、按学号升序浏览,2、按学号降序浏览:"<<endl;    }    return 0;}
0 0