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

来源:互联网 发布:eminem与光明会 知乎 编辑:程序博客网 时间:2024/05/16 12:55

编程题#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个字符串,字符串之间以空格间隔(不会出现字符串首字母相同的情况)

样例输入

24a bnss ds tsdfasg5aaa bbbb ccccd sa q

样例输出

Case: 1a ds bnss tsdfasga bnss ds tsdfasgCase: 2q sa aaa bbbb ccccdaaa 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

阅读全文
0 0
原创粉丝点击