1754:字符串数组排序问题http://www.noi.openjudge.cn/

1754:字符串数组排序问题

总时间限制: 

1000ms

内存限制: 

65536kB

描述

给定一组字符串,按指定的排序方式输出这些字符串。排序可是自然顺序(inc)、自然逆序(dec)、忽略大小写顺序(ncinc)、忽略大小写逆序(ncdec)等。

输入

输入有多行,第一行为一个表明排序方式的字符串见题面,第二行为字符串的数目。
其余各行每行一个字符串,字符串中间可能空格,前后也可能有空格,但前后的空格要忽略。

输出

输出也有多行,按指定的顺序输出输入的字符串。

样例输入

ncdec
3
Hello World!
You're right!
haha! you're wrong!

样例输出

You're right!
Hello World!
haha! you're wrong!

来源

JP06

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct str{
    char s[10000];
};
int n;
char a[10],b,x[10000][10000],tmp;
str y[10000];
bool cmp(str c,str d){
    int len=min(strlen(c.s),strlen(d.s));
    for(int i=0;i<len;i++){
        if(c.s[i]!=d.s[i]){
            return c.s[i]<d.s[i];
        }
    }
    return strlen(c.s)<strlen(d.s);

}
bool cmp2(str c,str d){
    int len=min(strlen(c.s),strlen(d.s));
    for(int i=0;i<len;i++){
        if(tolower(c.s[i])!=tolower(d.s[i])){
            return tolower(c.s[i])<tolower(d.s[i]);
        }
    }
    return strlen(c.s)<strlen(d.s);
}
int main(){
    cin>>a>>n>>b;
    cin.putback(b);
    for(int i=0;i<n;i++){
        cin.getline(x[i],100000);
        int l=0,r=strlen(x[i])-1,k=0;
        while(x[i][l]==' '){
            l++;
        }
        while(x[i][r]==' '){
            r--;
        }
        for(int j=l;j<=r;j++){
            y[i].s[k++]=x[i][j];
        }
        y[i].s[k]='\0';
    }

    switch(a[0]){
        case 'i':
            sort(y,y+n,cmp);
            for(int i=0;i<n;i++)
                cout<<y[i].s<<endl;
            break;
        case 'd':
            sort(y,y+n,cmp);
            for(int i=n-1;i>=0;i--)
                cout<<y[i].s<<endl;
            break;
        case 'n':
            sort(y,y+n,cmp2);
            if(a[2]=='i'){
                for(int i=0;i<n;i++)
                    cout<<y[i].s<<endl;
            }
            else{
                for(int i=n-1;i>=0;i--)
                    cout<<y[i].s<<endl;
            }
            break;
    }

} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值