/*字典序排列
输入要比较的字符串数目并输入,输出它前面字典序比他小的字符串个数*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include<vector>
using namespace std;
//s1>s2返回1 否则返回-1
int fun(string s1, string s2)
{
int len = s1.length()<s2.length() ? s1.length() : s2.length();
for (int i = 0; i<len; i++)
{
if (s1[i]>s2[i])
{
return 1;
}
else if(s1[i]<s2[i])
{
return -1;
}
else if (s1[i] == s2[i])
{
continue;
}
}
//能运行到这说明之前都相等
if (s1.length() > s2.length())
{
return 1;
}
else
{
return -1;
}
}
int main(){
int n ;
cin >> n;
string *str = new string [n]; //string数组
vector<int >count(n, 0);
int k = 0;
for (int i = 0; i<n; i++){
cin >> str[i];
}
for (int i = 0; i<n; i++){
for (int j = 0; j<i; j++){
if (i == 0) count[i] = 0;
else{
k = fun(str[i], str[j]);
if (k>0) count[i]++;
}
}
}
for (int i = 0; i<n; i++){
cout << count[i] << endl;
}
system("pause");
delete []str;
return 0;
}
字典序排列
最新推荐文章于 2022-01-27 14:17:48 发布