PAT(A)1140 Look-and-say Sequence (20分)(思维)

在这里插入图片描述

Sample Input

1 8

Sample Output

1123123111

思路:
其实是字符串处理,
11 23 12 31 11
1 222 11 3 1
11 22 1 3
1 22 111
11 2 1
1 2
11
1
倒推可发现规律,就是连续数字计数。
代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>

using namespace std;

typedef long long ll;
#define error 100001
#define endl '\n'


int main()
{
    int n;
    string d;
    cin >> d >> n;
    for (int k = 1; k < n; ++k)
    {
        string ans;
        // ans += d[0];
        int cnt = 0;
        char c = d[0];
        for (int i = 0; i < d.size(); ++i)
        {
            if (d[i] == c) cnt++;
            else
            {
                ans += c;
                ans += cnt + '0';
                cnt = 1;
                c = d[i];
            }
        }
        if (cnt > 0)
        {
            ans += c;
            ans += cnt + '0';
        }
        d = ans;
    }
    cout << d << endl;
    getchar(); getchar();
    return 0;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7096

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104047417