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

1、输入任意 4 个字符(如:abcd),并按反序输出(如:dcba)。

#include <iostream>
using namespace std;
int main() {
    string s;
    while(cin >> s) {
        for(int i = s.length() - 1; i >= 0; i--) {
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}

2、设 a、b、c 均是 0 到 9 之间的数字,abc、bcc 是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c 的值。

#include <iostream>
using namespace std;
int main() {
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            for(int k = 0; k < 10; k++) {
                if((i * 100 + j * 10 + k + j * 100 + k * 10 + k) == 532)
                    cout << "a = " << i << "  b = " << j << "  c = " << k << endl;
            }
        }
    }
    return 0;
}

3、一个数如果恰好等于它的各因子(该数本身除外)子和,如: 6=3+2+1,则称其为“完数”;若因子之和大于该数,则称其为“盈数”。求出 2 到 60 之间所有“完数”和“盈数”,并以如下形式输出: E: e1 e2 e3 ......(ei 为完数) G: g1 g2 g3 ......(gi 为盈数) 。

#include <iostream>
using namespace std;
int main() {
    int e[60], g[60];
    int cnt1 = 0, cnt2 = 0;
    for(int i = 2; i <= 60; i++) {
        int sum = 0;
        for(int j = 1; j < i; j++) {
            if(i % j == 0) sum += j;
        }
        if(sum == i) e[cnt1++] = i;
        if(sum > i) g[cnt2++] = i;
    }
    cout << "E:";
    for(int i = 0; i < cnt1; i++) {
        cout << " " << e[i];
    }
    cout << "(ei 为完数)" << endl;
    cout << "g:";
    for(int i = 0; i < cnt2; i++) {
        cout << " " << g[i];
    }
    cout << "(gi 为盈数)" << endl;
    return 0;
}

4、从键盘输入 4 个学生的数据( 包括姓名、年龄和成绩),并存放在文件 file1上。从该文件读出这些数据,按成绩从高到低排序,并输出其中成绩次高者的所有数据(文件的读写操作)

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

struct student {
    int age, score;
    string name;
};

bool cmp(student s1, student s2) {
    return s1.score > s2.score;
}

int main() {
    ifstream cin("./file1.txt"); //这里最好是绝对路径,因为这个路径调了老半天
    student stu[100];
    int i = 0;
    while(cin >> stu[i].name >> stu[i].age >> stu[i].score) {
        i++;
    }
    sort(stu, stu + i, cmp);
    for(int j = 0; j < i; j++) {
        cout << stu[j].name << " " << stu[j].age << " " << stu[j].score <<endl;
    }
    cout << "成绩次高者信息:" << stu[1].name << " " << stu[1].age << " " << stu[1].score <<endl;
    return 0;
}

参考了CSDN博主北极星以南南

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