北京理工大学复试上机--2007

1、一个小球,从高为H的地方下落,下落弹地之后弹起高度为下落时的一半,比如第一次弹起高度为H/2,如此反复,计算从小球H高度下落到n次弹地往返的总路程。
#include <iostream>
#include <cmath>
using namespace std;
int h;
double calH(int n) {
    if (n == 1)
        return h;
    else
        return calH(n - 1) + 2 * h / pow(2, n - 1);
}
int main() {
    int n;
    while (cin >> h >> n) {
        printf("%.2f
", calH(n));
    }
    return 0;
}
2、创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要加入自己的成员变量或成员函数。
#include <iostream>
#include <cmath>
using namespace std;

class CPoint
{
public:
    int x, y;
    double operator-(const CPoint c) {
        return sqrt((c.x - x) * (c.x - x) + (c.y - y) * (c.y - y));
    }
};

int main() {
    CPoint cp1, cp2;
    cout << "请输入第一个点坐标:";
    cin >> cp1.x >> cp1.y;
    cout << "请输入第二个点坐标:";
    cin >> cp2.x >> cp2.y;
    cout << "两点之间距离为:" << cp1 - cp2;
    return 0;
}
3、创建一个CTriangle 类,需要用到第二题中创建的类,即用3点来代表一个三角形,输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。
输入: 输入6个整数分别表示三个点的横纵坐标。
输出: 对于每个样例输出两行,第一行根据是否直角三角形输出Yes或No,第二行输出三角形的周长,保留小数点后两位。
input: 
0 0 3 0 0 4
output: 
Yes
12.00
#include <iostream>
#include <cmath>
using namespace std;

class CPoint
{
public:
    int x, y;
    double operator-(const CPoint c) {
        return sqrt((c.x - x) * (c.x - x) + (c.y - y) * (c.y - y));
    }
};

class CTriangle
{
public:
    CPoint p1, p2, p3;
    bool isT () {
        return ((p1 - p2) * (p1 - p2) + (p1 - p3) * (p1 - p3) == (p3 - p2) * (p3 - p2))
        || ((p1 - p2) * (p1 - p2) + (p3 - p2) * (p3 - p2) == (p1 - p3) * (p1 - p3))
        || ((p3 - p2) * (p3 - p2) + (p1 - p3) * (p1 - p3) == (p1 - p2) * (p1 - p2));
    }
    void show() {
        printf("%.2f", (p1 - p2) + (p1 - p3) + (p3 - p2));
    }
};

int main() {
    CPoint cp1, cp2, cp3;
    cin >> cp1.x >> cp1.y >> cp2.x >> cp2.y >>cp3.x >> cp3.y;
    CTriangle ct;
    ct.p1 = cp1;
    ct.p2 = cp2;
    ct.p3 = cp3;
    if(ct.isT()) cout << "Yes" << endl;
    else cout << "No" << endl;
    ct.show();
    return 0;
}
4、请自定义一个Student类,属性包括,Char name[10],int num。编程实现学生信息的输入、查询、浏览,其中浏览分为:升序和降序两种
 
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

class student
{
public:
    char name[10];
    int num;
    void insert(char na[], int n) {
        strcpy(name, na);
        num = n;
    }
};
bool cmpu(student s1, student s2) {
    return s1.num < s2.num;
}
bool cmpd(student s1, student s2) {
    return s1.num > s2.num;
}
vector<student> v;

void sortUp() {
    sort(v.begin(), v.end(), cmpu);
    for(int i = 0; i < v.size(); i++) {
        cout << v[i].name << " " << v[i].num << endl;
    }
}
void sortDown() {
    sort(v.begin(), v.end(), cmpd);
    for(int i = 0; i < v.size(); i++) {
        cout << v[i].name << " " << v[i].num << endl;
    }
}
void find(int num) {
    for(int i = 0; i < v.size(); i++) {
        if(v[i].num == num) {
            cout << v[i].name << " " << v[i].num << endl;
        }
    }
}

int main() {
    
    student stu;
    char name[10];
    int num;
    while(cin >> name >> num) {
        if(num == 0) break;
        stu.insert(name, num);
        v.push_back(stu);
    }
    cout << "请输入1、查询(输入学号num)  2、升序  3、降序  0、退出" << endl;
    int x;
    while(cin >> x) {
        if(x == 1) {
            int cnum;
            cout << "输入学号num查询: ";
            cin >> cnum;
            find(cnum);
        }
        if(x == 2) {
            cout << "升序排序为:" << endl;
            sortUp();
        }
        if(x == 3) {
            cout << "降序排序为:" << endl;
            sortDown();
        }
        if(x == 0) {
            v.clear();
            break;
        }
        cout << "请输入1、查询(输入学号num)  2、升序  3、降序  0、退出" << endl;
    }
    return 0;
}

冲吧!尽快写完!

原文地址:https://www.cnblogs.com/ache/p/12542234.html