博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华为OJ 字符串排序
阅读量:4298 次
发布时间:2019-05-27

本文共 2610 字,大约阅读时间需要 8 分钟。

写完之后,总觉得有点复杂。。

要求:
编写一个程序,将输入字符串中的字符按如下规则排序。
规则1:英文字母从A到Z排列,不区分大小写。
如,输入:Type输出:epTy
规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入:BabA输出:aABb
规则3:非英文字母的其它字符保持原来的位置。
如,输入:By?e输出:Be?y
样例:
输入:
A Famous Saying: Much Ado About Nothing(2012/8).
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy(2012/8).

#include 
#include
using namespace std;void strSort(char *str){ int length = strlen(str); char temp; for (int i = 0; i < length; i++) { for (int j = 0; j < length - i - 1; j++){ if (!(str[j] - str[j + 1] == 32 || str[j + 1] - str[j] == 32)) { if (str[j] >= 'a'&&str[j] <= 'z'&&str[j + 1] >= 'a'&&str[j + 1] <= 'z') { if (str[j] - 32>str[j + 1] - 32) { temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } else if (str[j] >= 'a'&&str[j] <= 'z') { if (str[j] - 32 > str[j + 1]) { temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } else if (str[j + 1] >= 'a'&&str[j + 1] <= 'z') { if (str[j] > str[j + 1] - 32) { temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } else{ if (str[j] > str[j + 1] - 32) { temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } } } }}int main(){ //字符串排序 string str; getline(cin, str); char *outPutStr = new char[str.size() + 1]; char *temp = new char[str.size() + 1]; for (int i = 0; i < str.size(); i++) { outPutStr[i] = 'a'; } outPutStr[str.size()] = '\0'; int index = 0; //先把除字母意外的字符放在最终的位置上 for (int i = 0; i < str.size(); i++) { if (str[i]<'A' || (str[i]>'Z'&&str[i]<'a') || str[i]>'z') { outPutStr[i] = str[i]; } else{ temp[index] = str[i]; index++; } } temp[index] = '\0'; //对temp排序 strSort(temp); index = 0; for (int i = 0; i < str.size(); i++) { if (outPutStr[i] == 'a') { outPutStr[i] = temp[index++]; } } cout << outPutStr << endl; return 0;}

转载地址:http://vinws.baihongyu.com/

你可能感兴趣的文章
期货市场技术分析07_摆动指数和相反意见理论
查看>>
满屏的指标?删了吧,手把手教你裸 K 交易!
查看>>
不吹不黑 | 聊聊为什么要用99%精度的数据回测
查看>>
X 分钟速成 Python
查看>>
对于模拟交易所引发的思考
查看>>
高频交易的几种策略
查看>>
网格马丁格尔交易法
查看>>
一行代码让 Python 的运行速度提高100倍
查看>>
一行 Python 实现并行化 -- 日常多线程操作的新思路
查看>>
期货市场的运作机制
查看>>
一文精通 crontab从入门到出坑
查看>>
股票连续跌停后开板表现
查看>>
东航期货行情接口和交易接口(20190509)
查看>>
ubnutu系统完美克隆至新硬盘,系统备份迁移至新硬盘
查看>>
ubnutu系统完美克隆至新硬盘,系统备份迁移至新硬盘
查看>>
东航期货模拟交易brockerid(期货公司的客户号)
查看>>
史上最全量化资源整理
查看>>
vnpy2.0安装后报错ModuleNotFoundError: No module named 'vnpy.api.ctp.vnctpmd'
查看>>
VNPY2.0火币期货交易接口配置使用
查看>>
win10和ubuntu18双系统时间同步(20190604亲测可行)
查看>>