2015北航机试

代码均为自做

题目一

求“相亲数”
即输入两个正整数a和b,若a的所有约数(包括1,不包括a本身)的和,等于b,且b的所有约数(包括1,不包括b本身)的和等于a,则两个数是相亲数。要求分别输出两个正整数的约数和的式子,再换行输出1或0,表示这两个数是否为相亲数。
输入:
220 284
输出:
220,1+2+4+5+10+20+22+44+55+110=284
284,1+2+4+71+142=220
1

#include <iostream>

using namespace std;
void printSum(int x, int &sum) {
    sum = 0;
    for (int i = 1; i <= x/2; i++) {
        if (x % i == 0) {
            if (sum != 0) cout << '+';
            cout << i;
            sum += i;
        }
    }
    cout << '=' << sum << endl;
    return;
}
int main()
{
    int a, b;
    int aSum = 0, bSum = 0;
    cin >> a >> b;
    printSum(a, aSum);
    printSum(b, bSum);
    if ((aSum == b) && (bSum == a)) cout << '1' << endl;
    else cout << '0' << endl;
    return 0;
}
题目二

模拟鼠标点击桌面时桌面窗口的叠放次序
先输入一个数字n,表示桌面窗口的数量,再输入n行,每行5个数,分别为窗口ID,窗口左下横坐标,左下角纵坐标,右上角横坐标,右上角纵坐标(坐标均以平魔左下角为0点),先输入的窗口叠放在后输入的窗口上面。再输入m行,表示m次点击,每行两个数,分别表示点击的横坐标和纵坐标,要求按窗口叠放顺序从高到低输出窗口ID.
输入:
2
1 5 1 1 5
2 7 1 3 5
3
1 2
4 3
6 4
输出:
2 1

#include <iostream>

using namespace std;
struct Screen { // 屏幕
    int id;
    int leftX, leftY;
    int rightX, rightY;
};
int getIndexOfClickScreen(int x, int y, Screen sc[], const int &n) { // 获得所点击屏幕的下标
    for (int i = 0; i < n; i++) {
        if ((x >= sc[i].rightX && x <= sc[i].leftX) && (y >= sc[i].leftY && y <= sc[i].rightY)) {
            return i;
        }
    }
    return -1; // 没有点击到窗口上
}
void moveToTop(Screen sc[], int index, const int &n) { // 被点击的窗口到最上面去了
    if (index < 0 || index >= n) return;
    Screen tmp = sc[index];
    for (int i = 0; i < index; i++) {
        sc[i+1] = sc[i];
    }
    sc[0] = tmp;
}
void print(Screen sc[], const int &n) { // 依次打印窗口的id
    for (int i = 0; i < n; i++) {
        cout << sc[i].id << ' ';
    }
    cout << endl;
}
int main()
{
    int n;
    cin >> n;
    Screen *sc = new Screen[n];
    for (int i = 0; i < n; i++) {
        cin >> sc[i].id >> sc[i].leftX >> sc[i].leftY >> sc[i].rightX >> sc[i].rightY;
    }
    int m;
    cin >> m;
    int x, y;
    int index;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        index = getIndexOfClickScreen(x, y, sc, n);
        moveToTop(sc, index, n);
    }
    print(sc, n);
    return 0;
}
/*
2
1 5 1 1 5
2 7 1 3 5
3
1 2
4 3
6 4
*/
题目三

统计词语
输入一段含标点的英文语段(若干行,以ctrl+z结束),统计这段话出现的所有词语,并按照字典顺序输出所有词语,每输出一个词换一行

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
bool strCmp(const string &a, const string &b) {
    if (a.compare(b) < 0) return true;
    else return false;
}
int main()
{
    char ch;
    string str;
    str.clear();
    vector<string> v;
    while ((ch = getchar()) != EOF) {
        if (isalpha(ch)) {
            str.push_back(ch);
        }
        else {
            if (str.size() != 0) {
                if (find(v.begin(), v.end(), str) == v.end()) {
                    v.push_back(str);
                }
                str.clear();
            }
        }
    }
    sort(v.begin(), v.end(), strCmp);
    vector<string>::iterator it;
    for (it = v.begin(); it != v.end(); it++) {
        cout << *it << endl;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值