字典序排列

来源:互联网 发布:linux vi如何保存退出 编辑:程序博客网 时间:2024/06/05 05:25
/*字典序排列输入要比较的字符串数目并输入,输出它前面字典序比他小的字符串个数*/#include <stdio.h>#include <iostream>#include <stdlib.h>#include <string>#include<vector>using namespace std;//s1>s2返回1 否则返回-1int 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;}