面向对象的C++题目以及解法2

01串排序

将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCI码值排序。输入数据中含有一些01串,01串的长度不大于256个字符。重新排列01串的顺序。使得串按基本猫述的方式排序。

#include <iostream>  
#include <vector>  
#include <algorithm>  
#include <string>  
using namespace std;
class String {
public:
    string str;
    String(const string& s) : str(s) {}
    int le() const {
        return str.length();
    }
    int count1() const {
        return count(str.begin(), str.end(), '1');
    }
    bool operator<(const String& other) const {
        if (le() != other.le()) {
            return le() < other.le();
        }
        if (count1() != other.count1()) {
            return count1() < other.count1();
        }
        return str < other.str;
    }
};
struct Str {
    bool operator()(const String& a, const String& b) {
        return a < b;
    }
};
int main() {
    vector<String> Strings;
    string s;
    while (getline(cin, s)) {
        Strings.emplace_back(s);
        if (s == "")break;
    }
    sort(Strings.begin(), Strings.end(), Str());
    for (const auto& bs : Strings) {
        cout << bs.str << endl;
    }
    return 0;
}

运行结果:
 

按绩点排名 

#include <iostream>  
#include <vector>  
#include <algorithm>  
#include <iomanip>  
#include <string>  
using namespace std;
class Student {
public:
    string name;
    double gpa;
    Student(string n, double g) : name(n), gpa(g) {}
    bool operator<(const Student& other) const {
        if (gpa != other.gpa) {
            return gpa > other.gpa;
        }
        else {
            return name < other.name;
        }
    }
};
int main() {
    int a;
    cin >> a;
    for (int c = 1; c <= a; ++c) {
        int n, m;
        cin >> n;
        vector<int> credits(n);
        for (int i = 0; i < n; ++i) {
            cin >> credits[i];
        }
        cin >> m;
        vector<Student> students;
        for (int i = 0; i < m; ++i) {
            string name;
            vector<int> scores(n);
            cin >> name;
            for (int j = 0; j < n; ++j) {
                cin >> scores[j];
            }
            double gpa = 0.0;
            for (int j = 0; j < n; ++j) {
                if (scores[j] >= 60) {
                    gpa += (scores[j] - 50.0) / 10.0 * credits[j];
                }
            }
            gpa /= 10.0;
            students.emplace_back(name, gpa);
        }
        sort(students.begin(), students.end());
        cout << "class " << c << ":" << endl;
        for (const auto& student : students) {
            cout << left << setw(10) << student.name << " " <<
                fixed << setprecision(2) << student.gpa << endl;
        }
        if (c < a) {
            cout << endl;
        }
    }
    return 0;
}

运行结果:

  • 25
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值