华为OJ 字符串排序

来源:互联网 发布:js radio 多选 编辑:程序博客网 时间:2024/05/16 08:00

描述

编写一个程序,将输入字符串中的字符按如下规则排序。

规则1:英文字母从AZ排列,不区分大小写。

      如,输入:Type 输出:epTy

规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。

    如,输入:BabA 输出:aABb

规则3:非英文字母的其它字符保持原来的位置。

    如,输入:By?e 输出:Be?y

样例:

    输入:

   A Famous Saying: Much Ado About Nothing(2012/8).

    输出:

   A aaAAbc dFgghhiimM nNn oooos Sttuuuy (2012/8).


知识点字符串,排序运行时间限制10M内存限制128输入


输出


样例输入A Famous Saying: Much Ado About Nothing (2012/8).样例输出A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).思路:

将大小写字母都提出来,然后用稳定的冒泡排序对字母进行排序,排完之后再插入到原有的序列中即可。


#include <iostream>#include <string>using namespace std;int main(){    string input;    getline(cin,input);    string temp;    for(int i=0;i<input.length();i++){        if(isalpha(input[i]))            temp += input[i];    }    for(int i=0;i<temp.length();i++){        for(int j=0;j<temp.length()-1-i;j++){            if(toupper(temp[j]) > toupper(temp[j+1])){                char c = temp[j];                temp[j] = temp[j+1];                temp[j+1] = c;            }        }    }    int k = 0;    for(int i=0;i<input.length();i++){        if(isalpha(input[i]))            input[i] = temp[k++];    }    cout<<input<<endl;    return 0;}



0 0
原创粉丝点击