Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码:
1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。
2、 如果K为1,不输出K
Input
输入有多组,直到文件结束。每组一个字符串,长度为10000以内
Output
输出编码后的字符串。
Sample Input
ABC
ABBCCC
Sample Output
ABC
A2B3C
Hint
Source
lin
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[10001];
int n,i;
while(gets(a)!=NULL)
{
int k = 1;
n = strlen(a);
for(i=0;i<n;i++)
{
if(a[i]==a[i+1])
k++;
else if(k!=1)
{
printf("%d%c",k,a[i]);
k = 1;
}
else printf("%c",a[i]);
}
printf("\n");
}
return 0;
}
运行结果:
ABC
ABC
ABBCCC
A2B3C
^Z
Process returned 0 (0x0) execution time : 12.152 s
Press any key to continue.