22、<简单>总结相同的数字

📅 2026/7/6 1:26:53 👁️ 阅读次数 📝 编程学习
22、<简单>总结相同的数字

#include <iostream> #include <string> using namespace std; int main() { // 程序功能:字符串压缩,单个字母不标数字,连续重复标长度+字母 cout << "输入要求:仅小写英文字母,长度不超过255" << endl; cout << "请输入待压缩字符串:"; string str; cin >> str; string result; int length = str.size(); int index = 0; while (index < length) { char current = str[index]; int count = 1; // 统计当前字母连续出现多少次 while (index + 1 < length && str[index + 1] == current) { count++; index++; } // 多于1个才拼接数字 if (count > 1) { result = result + to_string(count); } result += current; index++; } cout << "\n压缩完成!压缩后的字符串:" << result << endl; return 0; }