PAT甲级 1140 Look-and-say Sequence 字符串

PAT甲级 1140 Look-and-say Sequence 字符串_第1张图片

代码如下:

//字符串
#include
#include
#include
using namespace std;

int d,n;

int main(){
    cin>>d>>n;
    int cnt=1;
    string last_s="";
    last_s+=d+'0';
    string s;
    while(cnt<n){
        s="";
        char last_ch=last_s[0];
        int ans=1;
        if(last_s.length()==1){
            s+=last_ch;
            s+=ans+'0';
        }
        for(int i=1;i<last_s.length();i++){
            if(last_s[i]==last_ch){
                ans++;
            }else{
                s+=last_ch;
                s+=ans+'0';
                last_ch=last_s[i];
                ans=1;
            }
            if(last_s[i]==last_ch&&i==last_s.length()-1){
                s+=last_ch;
                s+=ans+'0';
            }
        }
        last_s=s;
        cnt++;
    }

    if(n==1){
        cout<<d;
    }
    cout<<s;

}

你可能感兴趣的:(字符串,PAT,PAT甲级)