酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

c语言输入压缩后的字符串,输出压缩前的字符串。 压缩的方法是把连续的相同字母压缩为"长度+字母"的形式,在本题中,单个的字母不需要压缩。 例如:3a5bx,解压后的结果为:aaabbbbbx; 例如:12ab10c2ax 解压后的结果为:aaaaaaaaaaaabbbbbbbbbbccax 。

```c #include <stdio.h> #include <string.h> #include <ctype.h> void decompress(char *s) { int len = strlen(s); int i = 0; while (i < len) { if (isdigit(s[i])) { int count = s[i] - '0'; i++; while (isdigit(s[i])) { count = count * 10 + (s[i] - '0'); i++; } for (int j = 0; j < count; j++) { printf("%c", s[i]); } i++; } else { printf("%c", s[i]); i++; } } } int main() { char s[100]; printf("Enter the compressed string: "); scanf("%s", s); printf("Decompressed string: "); decompress(s); return 0; } ``` 这段代码实现了输入压缩后的字符串,输出压缩前的字符串的功能。通过`decompress`函数遍历输入的字符串,根据数字和字母的顺序进行解压缩,输出原始字符串。 [2024-11-14 18:24:57 | AI写代码神器 | 600点数解答]

相关提问