杭电1020

Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method: 

1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

2. If the length of the sub-string is 1, '1' should be ignored.
 
Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
 
Output
For each test case, output the encoded string in a line.
 
Sample Input
2 ABC ABBCCC
 
Sample Output
ABC A2B3C
 
水题,一开始误解了意思,以为是求一串字符串中相同字母的个数并输出,其实是求连续字母的个数并输出。
代码如下:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 10001
 4 int main(){
 5     int n=0;
 6     scanf("%d",&n);
 7     while(n--){
 8         char s[max];
 9         scanf("%s",s);
10         int count=1,
11             i;
12         for(i=0;i<strlen(s);i++){
13             if(s[i]==s[i+1])count++;
14             else{
15                 if(count==1)printf("%c",s[i]);
16                     else printf("%d%c",count,s[i]);
17                     count=1;
18             }
19         }
20         printf("
");
21     }
22     return 0;
23 
24 }
View Code
原文地址:https://www.cnblogs.com/hongrunhui/p/5125740.html