2013北理上机-字典排序

写了好久...因为函数搞错...

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
/*
    输入一组英文单词,按字典顺序(大小写相同地位)
    示例:
        输入:Information Info Inform suite suit Suite
        输出:Info Inform Information suit Suite
*/

int main(){

    string Str, str, S[100],s[100];
    getline(cin, Str);
    Str+='#';
    str.assign(Str);

    //先转换为全小写,方便比较
    for(int i=0; i<str.length(); i++){
        if(str[i]>='A' && str[i]<='Z'){
            str[i]=str[i]-'A'+'a';
        }
    }

    int cnt = 0;
    int st=-1, ed=-1;
    int flag = 0;
    for(int i=0; i<str.length(); i++){
        char c = str[i];
        if(!flag){
            if(c != ' '){
                flag = 1;
                st = i;
            }
        }else{
            if(c == ' '||c == '#'){
                flag = 0;
                ed = i;
                s[cnt].assign(str,st,ed-st);    //注:str.assign(s,2,5); 表示将s的第2位开始的5位赋值给str
                S[cnt].assign(Str,st,ed-st);
                cnt++;
            }
        }
    }

    //冒泡排序
    for(int i=0; i<cnt; i++){
        for(int j=0; j<cnt-i-1; j++){   //注:j<cnt-i-1
            int k = s[j].compare(s[j+1]);
            if(k > 0){
                swap(s[j],s[j+1]);
                swap(S[j],S[j+1]);
            }
        }
    }

    //输出排序后原数组
    for(int i=0; i<cnt; i++){
        cout << S[i] << ' ';
    }
    cout << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值