# 计算字符串逆序数 例如:字符串中的内容为:a1Ab1D2,1 #include using namespace std; int solve(string s) { if (s.length() == 0) return 0; int n = 0; for (int i = 1; i < s.length(); i++) ___________________; return n; } int main() { string s = "a1Ab1D2"; int n = solve(s); cout << n << endl; return 0; } ``` ## template ```cpp #include #include 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; } ``` ## 答案 ```cpp if (s.c_str()[i] < s.c_str()[i - 1]) n++; ``` ## 选项 ### A ```cpp if (s.c_str()[i] > s.c_str()[i - 1]) n++; ``` ### B ```cpp if (s.c_str()[i] < s.c_str()[i + 1]) n++; ``` ### C ```cpp if (s.c_str()[i] > s.c_str()[i + 1]) n++; ```