牛客网 - PPY的字符串(字符串处理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzyws739307453/article/details/85473391

题目链接:https://ac.nowcoder.com/acm/contest/322/E
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Siry特别喜欢数学, 在他很小的时候他就对数字特别感兴趣, 他喜欢念数字。
具体念法是这样的: 给你一个数字, 依次念出每个数字有几个相邻(Siry会大声说出a个b, c个d...), 组合起来形成一个新的数字。
如:
2331的念法就是1个2,2个3,1个1, 形成的新数字就是122311。 再念一次就是1个1,2个2,1个3, 2个1, 形成的数字是11221321。
现在Siry大声的念出了第一次的数字x, Siry总共想要念n次, 你能快速的知道第n次的数字是多少吗?

输入描述:

每行输入两个数字x,n。
1≤ x≤ 109,1≤ n≤ 30

输出描述:

输出一行,包括第n个数字的位数和这个数字。 位数和数字之间用空格隔开。

输入

222 2

输出

2 32

说明

第一次念出的数字是222,第二次就会念3个2,形成的数字就是32,位数是两位数。

解题思路

类似于伯爵说序列,类似题:https://blog.csdn.net/lzyws739307453/article/details/83582870,都是一些关于字符串处理的题。

#include <stdio.h>
#include <string.h>
char str[10010], tmp[10010];
void edge()
{
    int cnt = 1, idx = 0;
    int len = strlen(str);
    for (int i = 1; i < len; i++)
    {
        if (str[i] != str[i - 1])
        {
            tmp[idx++] = cnt + '0';
            tmp[idx++] = str[i - 1];
            cnt = 1;
        }
        else cnt++;
    }
    tmp[idx++] = cnt + '0';
    tmp[idx++] = str[len - 1];
    tmp[idx] = '\0';
}
int main()
{
    int n;
    while (~scanf("%s%d", str, &n))
    {
        for (int i = 1; i < n; i++)
        {
            edge();
            strcpy(str, tmp);
        }
        printf("%d %s\n", strlen(str), str);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzyws739307453/article/details/85473391