sdut 2761 编码

编码
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 s[10000];
int k,p,len,i; //多加一个标记变量,当不相同字符出现时,输出前面相同部分
while(gets(s))
{
k = 1;p=0;
len = strlen(s);
for(i=0;i<len;i++)
{
if(s[i]s[i+1])
{
k++;
}
else p=1;
if(p
1)
{
if(k == 1)
{
printf("%c",s[i]);
}
else
{
printf("%d%c",k,s[i]);
k = 1;
}
}
p = 0;

    }
    printf("\n");
}

return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_43750277/article/details/84972917