# 【笔试】两种排序方法

“car” < “carriage” < “cats” < “doggies < “koala”
2.根据字符串的长度排序。例如：
“car” < “cats” < “koala” < “doggies” < “carriage”

3
a
aa
bbb

both

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<string>strVec;
int stringNum;
cout<<"请输入字符串的个数：";
cin>>stringNum;
for(int i = 0; i < stringNum; i++)
{
string str1;
cin>>str1;
strVec.push_back(str1);
}
vector<string>strVectemp(strVec);
sort(strVectemp.begin(),strVectemp.end());
bool bRetlexicographically = false;
bool bRetlengths = false;
if(strVectemp == strVec)
bRetlexicographically = true;
/////
vector<int>iVec; /////用于存放每个字符串的长度
for(int i = 0; i < strVec.size();i++)
{
iVec.push_back(strVec[i].length());
}
vector<int>iVecTemp(iVec);///////
sort(iVecTemp.begin(),iVecTemp.end());
if(iVecTemp == iVec)
bRetlengths = true;
/////////判断到底是按照什么排序的
if(bRetlengths == true && bRetlexicographically == true)
cout<<"both"<<endl;
else if(bRetlengths == false && bRetlexicographically == true)
cout<<"lexicographically"<<endl;
else if(bRetlengths == true && bRetlexicographically == false)
cout<<"lengths"<<endl;
else
cout<<"none"<<endl;
return 0;
}


#### VB 两种排序方法的优化

2010年06月07日 38KB 下载

06-11 374

12-01 181

06-23 21

10-22 1354

01-26 719

11-28 1048

11-24 3169

11-08 875

12-11 259