题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
转换成52进制数进行排序,注意要转换成double正负1.7*10e308,而int会超限正负2^31.
#include <iostream>
#include<map>
#include<vector>
#include<math.h>
using namespace std;
/*string smaS(string &s1,string &s2)//定义比较函数
{
string smS;
return smS;
}*/
double getInd(string& str) { //暂不设置位数,可补充
double Ind=0;
int len=0;
len = str.length();
int j = 0;
for (int i = 99; i > 99 - len; i--) {
double Num;
int Asc,Num1;
char chT;
chT = str[j];
Asc = (int)(chT);
if (65 <= chT &&
chT <= 90) { //字母对应26*2进制数字;小写一律排大写字母后面;0-53
Num1 = chT - 'A' ;
} else {
Num1 = chT - 'A' - 6;
}
Num=(double)Num1;
Ind = Ind + Num * pow(52, i);
j++;
}
return Ind;
}
int main() {
int n;
cin >> n;
string strM;
vector<pair<double,string>> vecH;
//map <double, string> ZiD; //用map去排序。去重了不行呀
int i=0;
while(i<n) { //用for循环输入有问题
string strT;
cin>>strM;
strT=strM;
double Ind;
Ind = getInd(strT);//排序
pair<double, string> pT(Ind, strT);
vecH.push_back(pT);
//ZiD.insert(pT);//index算得都一样,所以被顶住了。其中52^99次方超整形量的限了,建议换成double:+-1.7*10e308
i++;
}
int size=0;
size=vecH.size();
for (int i=0;i<size;++i) {//冒泡输出
pair<double,string> pT2;
for(vector<pair<double, string>>::iterator iter2 = vecH.begin(); iter2 != vecH.end(); ++iter2)
{
double N1,N2;
N1=iter2->first;
N2=(iter2+1)->first;
if(N1<N2)//前后互换,小的向前
{
pT2=*iter2;
*iter2=*(iter2+1);
*(iter2+1)=pT2;
}
}
pair<double,string> pT3;
pT3=vecH.back();
vecH.pop_back();
string strOut;
strOut=pT3.second;
cout << strOut << endl;
}
}
// 64 位输出请用 printf("%lld")
