PAT(Basic Level) Practice : 1084 外观数列 (20分)

1084 外观数列 (20分)

难点在理解题意

简而言之,从一个字符串起点开始数,有几个相同的字符,例如11223,结果是122231(1有两个,2有两个,3有1个),组成新的字符串,不断的更新

代码

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
//scanf printf防止超时
#include <algorithm>
//vector的sort
#include <sstream>
//转换
using namespace std;

#include<iomanip>
//精度

#include<cmath>
//round四舍五入取整
#include <map>


int main()
{
    string str;
    int num;
    cin>>str>>num;



    for(int i=0;i<num-1;i++)
    {
        string res="";
//cout<<"str: "<<str<<endl;
//cout<<"time:  "<<i<<endl;
        for(int j=0;j<str.size();j++)
        {
//cout<<"KK"<<endl;
            if(j==str.size()-1)
            {
                res+=str[j];
                res+="1";
            }else{
                int count=1;
                //cout<<"ww"<<endl;
                for(int k=j+1;k<str.size();k++)
                {
                   if(str[k]==str[j])
                   {
                       count++;
                   }else
                   {
                       break;
                   }
                }
//cout<<"count"<<count<<endl;
                res+=str[j];
                string temp;
                stringstream ss;
                ss<<count;
                ss>>temp;
                res+=temp;
                j+=count-1;
            }
        }
        str=res;
    }
    cout<<str<<endl;
    return 0;
}

posted @ 2020-10-01 00:19  韩天尊  阅读(96)  评论(0)    收藏  举报