函数的功能是计算字符串逆序数。例如:字符串中的内容为:a1Ab1D2,1<A,A<b 1<D 则调用该函数后,返回码为:3。
#include <iostream>
#include <string>
using namespace std;
int solve(string s)
{
if (s.length() == 0) return 0;
int n = 0;
for (int i = 1; i < s.length(); i++)
if (s.c_str()[i] < s.c_str()[i - 1]) n++;
return n;
}
int main()
{
string s = "a1Ab1D2";
int n = solve(s);
cout << n << endl;
return 0;
}