题目描述
Lily上课时使用字母数字图片教小朋友们学习英语单词,每次都需要把这些图片按照大小(ASCII码值从小到大)排列收好。请大家给Lily帮忙,通过C语言解决。
输入描述:
Lily使用的图片包括"A"到"Z"、"a"到"z"、"0"到"9"。输入字母或数字个数不超过1024。
输出描述:
Lily的所有图片按照从小到大的顺序输出
示例1
输入
Ihave1nose2hands10fingers
输出
0112Iaadeeefghhinnnorsssv
思路一:手撸字符快排压压惊,顺便复习一下。注意容易错误的地方
#include <stdio.h>
#include <string.h>
//快排一次分组,返回划分元下标
int QuickPartition(char *s, int low, int high)
{
char value = s[low]; //取首元素作为划分元
while (low < high)
{
while (low < high && s[high] >= value) //low<high不可省略
--high;
if (low < high) s[low++] = s[high];
while (low < high && s[low] <= value)
++low;
if (low < high) s[high--] = s[low]; //易错
}
//划分元置入正确位置
s[low] = value; //易错易漏
return low;
}
//快排
void QuickSort(char *s, int low, int high)
{
if (low < high) //易错
{
int i = QuickPartition(s, low, high);
QuickSort(s, low, i - 1);
QuickSort(s, i + 1, high);
}
}
int main()
{
char str[1024];
while (scanf("%s", str) != EOF)
{
int len = strlen(str);
QuickSort(str, 0, len - 1);
printf("%s\n", str);
}
return 0;
}
思路二:STL、泛型算法,没技术含量
链接:https://www.nowcoder.com/questionTerminal/2de4127fda5e46858aa85d254af43941
来源:牛客网
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string data;
while(cin>>data)
{
sort(data.begin(),data.end());
cout<<data<<endl;
}
}