编码
来源:互联网 发布:ubuntu 用户提权 编辑:程序博客网 时间:2024/06/05 13:01
编码
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码:
1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。
2、 如果K为1,不输出K
Input
输入有多组,直到文件结束。每组一个字符串,长度为10000以内
Output
输出编码后的字符串。
Example Input
ABCABBCCC
Example Output
ABCA2B3C
Hint
Author
lin
#include <stdio.h>
#include <string.h>
int main ()
{
int i ,n ,k;
char s[10001] ;
while (gets(s))
{
n = strlen(s);
k = 0;
for (i = 0; i < n; i++)
{
k = 0;
if (s[i]==s[i+1])
{
for (int j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
k++;
}
else
break;
}
i = i + k;
}
if (k)printf("%d" , k+1);
printf("%c" , s[i]);
}
printf("\n");
}
return 0;
}
#include <string.h>
int main ()
{
int i ,n ,k;
char s[10001] ;
while (gets(s))
{
n = strlen(s);
k = 0;
for (i = 0; i < n; i++)
{
k = 0;
if (s[i]==s[i+1])
{
for (int j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
k++;
}
else
break;
}
i = i + k;
}
if (k)printf("%d" , k+1);
printf("%c" , s[i]);
}
printf("\n");
}
return 0;
}
阅读全文