【HDOJ】1020 Encoding
简单字符串处理。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXLEN 10005 5 6 char buf[MAXLEN]; 7 char des[MAXLEN]; 8 char tmp[MAXLEN]; 9 10 void myitoa(int a, char str[]) { 11 int i=0, tmp=a; 12 char ch; 13 while (tmp) { 14 ch = tmp%10+'0'; 15 str[i++] = ch; 16 tmp = tmp / 10; 17 } 18 str[i] = '\0'; 19 } 20 21 int main() { 22 int case_n; 23 int i, j, k, len, num; 24 char ch; 25 26 scanf("%d", &case_n); 27 28 while (case_n--) { 29 scanf("%*c%s", buf); 30 len = strlen(buf); 31 j = 0; 32 ch = buf[0]; 33 num = 1; 34 for (i=1; i<=len; ++i) { 35 if (buf[i] == ch) { 36 ++num; 37 } else { 38 if (num > 1) { 39 myitoa(num, tmp); 40 k = strlen(tmp); 41 while (k--) { 42 des[j++] = tmp[k]; 43 } 44 } 45 des[j++] = ch; 46 ch = buf[i]; 47 num = 1; 48 } 49 } 50 des[j] = '\0'; 51 printf("%s\n", des); 52 } 53 54 return 0; 55 }