计蒜客 难题题库 023 计数和数数

“伯爵说”序列如下:1, 11, 21, 1211, 111221, ...1 读作 "one 1" 或者 11。11 读作 "two 1s" 或者21。21 读作 "one 2, one 1" 或者 1211。

格式:多组输入,读到文件结束。每组输入给定一个整数n,输出第n个序列。(1<=n<=30)

注意:整数序列以字符串的形式表示。

PS:相信你已经看懂了题目,如果没看懂,小提示下,

其实类似于求”菲波拉契“数列的第n项哦~

样例1

输入:

6

输出:

312211


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

string numToStr(int n){
    if(n == 0){
        return "0";
    }
    string res;
    while(n){
        res.push_back(n % 10 + '0');
        n /= 10;
    }
    reverse(res.begin(), res.end());
    return res;
}

string  bole(string s){
    string res;
    char last_char = s[0];
    int count = 1;
    int len = s.length();
    for(int i = 1; i < len; ++i){
        if(s[i] != last_char){
            res += numToStr(count) + last_char;
            last_char = s[i];
            count = 1;
        }else{
            count++;
        }
    }
    res += numToStr(count) + last_char;
    return res;
}

int main(){
    int n;
    while(cin >> n){
        string s = "1";
        while(--n){
            s = bole(s);
        }
        cout << s << endl;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值