题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805262018265088
题解:
1 #include <iostream> 2 #include<cstdio>//getchar所需的头文件 3 #include<string> 4 using namespace std; 5 6 int main() { 7 char select; 8 cin >> select; 9 getchar();//读入回车键 10 string str; 11 getline(cin, str); 12 if (select == ‘C‘) { 13 int sum = 0; 14 for (int i = 0; i < str.size(); i++) { 15 if (str[i + 1] == str[i]) sum++; 16 else { 17 if (sum == 0) cout << str[i]; 18 else { 19 cout << sum + 1 << str[i]; 20 } 21 sum = 0; 22 } 23 } 24 } 25 else { 26 for (int i = 0; i < str.size(); i++) { 27 int sum = 0; 28 while (1) { 29 if (str[i] >= ‘0‘&&str[i] <= ‘9‘) 30 sum = sum * 10 + str[i++] - ‘0‘;//单个字符的个数可能不是一位数 31 else break; 32 } 33 if (sum == 0) cout << str[i]; 34 for (int j = 0; j < sum; j++) { 35 cout << str[i]; 36 } 37 } 38 } 39 return 0; 40 }
原文地址:https://www.cnblogs.com/Gzu_zb/p/9594962.html
时间: 2024-10-28 08:03:55