题目描述
给定n个字符串,请对n个字符串按照字典序排列。
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
输入
#输入 9 cap to cat card two too up boat boot
输出
#输出 boat boot cap card cat to too two up
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
using namespace std;
void mySort(vector <string> srcVector, int count);
int myCmp(string src1, string src2);
void mySwap(string *src1, string *src2);
int main()
{
int count = 0;
vector <string> srcVector(100);
cin >> count;
for(int i = 0; i < count; i++)
cin >> srcVector[i];
mySort(srcVector, count);
return 0;
}
void mySort(vector <string> srcVector, int count)
{
for(int i = 0; i < count; i++)
{
for(int j = 0; j < count; j++)
{
if(myCmp(srcVector[j], srcVector[j+1]) > 0)
{
mySwap(&srcVector[j], &srcVector[j + 1]);
}
}
}
for(int i = 0; i < count; i++)
cout << srcVector[i] <<endl;
}
int myCmp(string src1, string src2)
{
int len = 0;
int ret = 0;
len = (src1.size() > src2.size())?src2.size():src1.size();
for (int i = 0; i < len; i++)
{
if(strcmp(src1.c_str(), src2.c_str()) > 0)
{
ret = 1;
break;
}
else
{
ret = 0;
break;
}
}
return ret;
}
void mySwap(string *src1, string *src2)
{
string myString;
myString = *src1;
*src1 = *src2;
*src2 = myString;
}