编程介的小学生 2019-04-24 11:09 采纳率: 20.5%
浏览 268

字母顺序的编码的一个问题的C语言的程序的设计解决的办法,怎么做的呢?

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条回答 默认 最新

  • Italink 2019-04-24 12:17
    关注
    #include<stdio.h>
    #include<string.h>
    
    int main() {
        int n,count,len;
        char str[10001];
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%s", &str);
            len = strlen(str);
            for (int j = 0; j < len; ++j) {
                count = 1;
                while (j<len&&str[j] == str[j + 1] && ++j) 
                    count++;
                if (count > 1)printf("%d", count);
                printf("%c", str[j]);
            }
            printf("\n");
        }
        return 0;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?