* 期末考试 编程题#7:字符串排序(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;函数对象作参数)

编程题#7:字符串排序

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述
请按照要求对输入的字符串进行排序。

#include <iostream>
#include <string>
#include <list>
using namespace std;
class A{
private:
        string name;
public:
        A(string n) :name(n){}
        friend bool operator < (const class A& a1, const class A &a2);
        friend bool operator == (const class A &a1, const class A &a2){
                if (a1.name.size() == a2.name.size())
                        return true;
                else
                        return false;
        }
        friend ostream & operator << (ostream &o, const A &a){
                o << a.name;
                return o;
        }
        string get_name() const{
                return name;
        }
        int get_size() const{
                return name.size();
        }
};
// 在此处补充你的代码
int main(int argc, char* argv[])
{
        list<A> lst;
        int ncase, n, i = 1;
        string s;
        cin >> ncase;
        while (ncase--){
                cout << "Case: "<<i++ << endl;
                cin >> n;
                for (int i = 0; i < n; i++){
                         cin >> s;
                         lst.push_back(A(s));
                }
                lst.sort();
                Show(lst.begin(), lst.end(), Print());
                cout << endl;
                lst.sort(MyLarge<A>());
                Show(lst.begin(), lst.end(), Print());
                cout << endl;
                lst.clear();
        }
        return 0;
}

输入
第一行是正整数T,表示测试数据的组数
每组测试数据输入共两行,
第一行是正整数N,表示字符串个数
第二行是N个字符串, 字符串间用空格分离

输出
对于每组测试数据,先输出一行:
Case: n
如对第一组数据就输出Case: 1
第二行按照字符串长度从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串长度相同的情况)
第三行按照字符串首字符ASCII码序从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串首字母相同的情况)

样例输入

2
4
a bnss ds tsdfasg
5
aaa bbbb ccccd sa q

样例输出

Case: 1
a ds bnss tsdfasg
a bnss ds tsdfasg
Case: 2
q sa aaa bbbb ccccd
aaa bbbb ccccd q sa

程序解答:

#include <iostream>
#include <string>
#include <list>
using namespace std;

class A{
private:
    string name;
public:
    A(string n) :name(n){}
    friend bool operator < (const class A& a1, const class A &a2);
    friend bool operator == (const class A &a1, const class A &a2){
        if (a1.name.size() == a2.name.size())
            return true;
        else
            return false;
    }
    friend ostream & operator << (ostream &o, const A &a){
        o << a.name;
        return o;
    }
    string get_name() const{
        return name;
    }
    int get_size() const{
        return name.size();
    }
};

// 在此处补充你的代码
bool operator < (const class A& a1, const class A &a2){
    return(a1.get_size() < a2.get_size());
}

//此处参考 http://zh.cppreference.com/w/cpp/algorithm/for_each struct Sum...
struct Print{
    void operator()(A& a){
        //cout << a.get_name() << " ";
        cout << a << " ";
    }
};

//此处参考 http://zh.cppreference.com/w/cpp/algorithm/for_each for_each...
template<class InputIt, class UnaryFunction>
UnaryFunction Show(InputIt first, InputIt last, UnaryFunction f){
    for (; first != last; ++first) {
        f(*first);
    }
    return f;
}

template<class InputIt>
struct MyLarge {
    bool operator()(const InputIt & a1, const InputIt & a2){
        //按首字母比大小
        return a1.get_name() < a2.get_name();
        //return a1.get_name().at(0) < a2.get_name().at(0);  //默认先比较的就是首字母
    }
};

int main(int argc, char* argv[]){
    list<A> lst;
    int ncase, n, i = 1;
    string s;
    cin >> ncase;
    while (ncase--){
        cout << "Case: " << i++ << endl;
        cin >> n;
        for (int i = 0; i < n; i++){
            cin >> s;
            lst.push_back(A(s));
        }
        lst.sort();
        Show(lst.begin(), lst.end(), Print());

        cout << endl;
        lst.sort(MyLarge<A>());
        Show(lst.begin(), lst.end(), Print());
        cout << endl;
        lst.clear();
    }

    return 0;
}

其他补充说明参考:
http://blog.csdn.net/NNNNNNNNNNNNY/article/details/50677055

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值