EOJ2990-文献排序

题目

现在你的导师给你了一个待排序的参考文献列表,要你排好序给他。
文献列表中每一条文献记录只占一行。排序的规则和string类型字符串的比较规则一致(在排序时如果该字符串中包含大写字母,则当作小写字母处理,保证没有相同大小的字符串,但是输出结果不能改变任一字符串),按升序排列。

输入输出

input
第 1 行:一个整数 T (1≤T≤10) 为问题数。
对于每组测试数据:每组第一行包括一个整数n,(1≤n≤200),接下来有n行,每行包括一行文献记录,文献记录的长度s (1≤s≤200)。
output
对每组输入,输出一行问题的编号(0开始编号,格式:case #0: 等)然后输出排好序的文献记录。

样例

input
3
3
abc hello!
Abc hellz!
bbc hello!
2
Acc hello!
Abc hellz!
3
Abc Hi!
abc Hallo!
aAc hello!

output
case #0:
abc hello!
Abc hellz!
bbc hello!
case #1:
Abc hellz!
Acc hello!
case #2:
aAc hello!
abc Hallo!
Abc Hi!

代码

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

bool cmp(string a, string b)
{
    //先把string全部转换为小写再比较
    transform(a.begin(), a.end(), a.begin(), ::tolower);
    transform(b.begin(), b.end(), b.begin(), ::tolower);
    if (a.compare(b) <= 0) return true;
    return false;
}
int main() {

    string a[200];
    string temp;
    int n, i;
    int num, j;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> num;
        getchar();
        for (j = 0; j < num; j++)
            getline(cin, a[j]);

        sort(a, a + num, cmp);
        cout << "case #" << i << ":" << endl;
        for (j = 0; j < num; j++)
            cout << a[j] << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Caiyii530/article/details/105465197