1084 外观数列(附详细注释,逻辑分析)

写在前面
  • 理解题意耗费些时间
    • 没有太好的解法。Step1,弄懂题意;Step2厘清逻辑
    • 遍历字符串,计算外观数列,并更新下一迭代数列字符串。
      • 迭代字符串
      • 更换数列值
      • 判断字符串遍历结束,更新最新下标值(k)
      • to_string等函数,思路略显臃肿。有更优方法
测试用例
input:
1 8

output:
1123123111

compute process:
1
11
12
1121
122111
112213
12221131
1123123111
ac代码
  • 思绪有点儿混乱,拼凑出来
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int d, n;
    scanf("%d%d", &d, &n);

    int cnt = 0;
    string src_ds, result_ds, cur_ds;

    src_ds = to_string(d);
    for(int i=1; i<n; i++)
    {
        for(int k=0; k<src_ds.length(); k++)
        {
            cur_ds = src_ds.substr(k,1);
            for(int j=k; j<src_ds.length(); j++)
            {
                if(cur_ds == src_ds.substr(j,1)) cnt++;
                else break;
            }
            k += cnt-1;
            result_ds += (cur_ds + to_string(cnt));
            cnt = 0;
        }
        src_ds = result_ds;
        result_ds = "";
    }

    cout << src_ds << endl;

    return 0;
}
参考代码
#include <iostream>
using namespace std;
int main() {
    string s;
    int n, j;
    cin >> s >> n;
    for (int cnt = 1; cnt < n; cnt++) {
        string t;
        for (int i = 0; i < s.length(); i = j) {
            for (j = i; j < s.length() && s[j] == s[i]; j++);
            t += s[i] + to_string(j - i);
        }
        s = t;
    }
    cout << s;
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xsimah

创作不易,感谢客官的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值