题目:所谓的递推查数数列如下所示:
1, 11, 21, 1211, 111221, ....
1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.Given an integer n, generate the nth sequence.Note: The sequence of integers will be represented as a string.
分析:按照字符串先后顺序计算相邻相同数字数目,按照“个数+数字+个数+数字......”顺序为下一个数列。
代码:
-
#include
-
-
using namespace std;
-
-
classTest
-
{
-
public:
-
bool nextSeq(constchar*src,char*dest)
-
{
-
char tmp=NULL;
-
intcount=0;
-
-
if((NULL==src)||(NULL==dest))
-
returnfalse;
-
-
while(*src!='\0')
-
{
-
if((*src>='1')&&(*src<='9'))
-
{
-
if(*src==tmp)
-
{
-
count++;
-
src++;
-
}
-
else
-
{
-
if(count>0)
-
{
-
*dest++='0'+count;
-
*dest++=tmp;
-
}
-
tmp=*src;
-
count=1;
-
src++;
-
}
-
}
-
}
-
if(count>0)
-
{
-
*dest++='0'+count;
-
*dest++=tmp;
-
}
-
*dest='\0';
-
returntrue;
-
}
-
};
-
-
intmain()
-
{
-
cout<<"hello world"<
-
-
char str[100]={0};
-
classTest my_test;
-
cout<
-
cout<
-
}
阅读(530) | 评论(0) | 转发(0) |