#45742: 自定義funtion


1121228@stu.wghs.tp.edu.tw (你知道我是誰嗎!!??)

學校 : 臺北市立建國高級中學
編號 : 266561
來源 : [60.248.154.143]
最後登入時間 :
2025-03-28 09:48:03
n327. The Tower of Names -- 板橋高中教學題 | From: [60.248.154.139] | 發表日期 : 2025-04-07 12:51

#include <bits/stdc++.h>
using namespace std;
bool com(const string& a, const string& b) {
    if (a.size() == b.size()) {
        return a < b; // 長度相同時按字母順序排序
    }
    return a.size() < b.size(); // 按照長度排序
}
int main() {
    int n;
    cin >> n;
    cin.ignore();
    string name[n];
    for(int i=0; i<n; i++){
        getline(cin,name[i]);
    }
    sort(name, name+n, com);
    for(auto i : name){
        cout << i << endl;
    }
    return 0;
}

 
ZeroJudge Forum