A1140 Look-and-say Sequence

用string操作很方便,没什么坑点,看懂题意就行了。主要就是统计连续的一片区域有几个连续的数字。

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
	string now,next;
	int n;
	cin>>now>>n;
	while(--n){
		//cout<<now<<endl;
		next = "";
		for(int i=0;i<now.length();){
			char temp = now[i];
			int cnt = 0;
			while(now[i]==temp&&i<now.length()){
				cnt++;
				i++;
			}
			char num = '0'+cnt;
			next += temp;//这两个+要分开写因为temp+num会编程另外一个char字符
			next += num;
		}
		now = next;
	}
	cout<<now;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43108373/article/details/84204415