Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1413930
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2013-12-25 09:57:21

题目:所谓的递推查数数列如下所示:

                                            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.
分析:按照字符串先后顺序计算相邻相同数字数目,按照“个数+数字+个数+数字......”顺序为下一个数列。

代码:

点击(此处)折叠或打开

  1. #include

  2. using namespace std;

  3. classTest
  4. {
  5.     public:
  6.         bool nextSeq(constchar*src,char*dest)
  7.         {
  8.             char tmp=NULL;
  9.             intcount=0;

  10.             if((NULL==src)||(NULL==dest))
  11.                 returnfalse;

  12.             while(*src!='\0')
  13.             {
  14.                 if((*src>='1')&&(*src<='9'))
  15.                 {
  16.                     if(*src==tmp)
  17.                     {
  18.                         count++;
  19.                         src++;
  20.                     }
  21.                     else
  22.                     {
  23.                         if(count>0)
  24.                         {
  25.                             *dest++='0'+count;
  26.                             *dest++=tmp;
  27.                         }
  28.                         tmp=*src;
  29.                         count=1;
  30.                         src++;
  31.                     }
  32.                 }
  33.             }
  34.             if(count>0)
  35.             {
  36.                 *dest++='0'+count;
  37.                 *dest++=tmp;
  38.             }
  39.             *dest='\0';
  40.             returntrue;
  41.         }
  42. };

  43. intmain()
  44. {
  45.     cout<<"hello world"<

  46.     char str[100]={0};
  47.     classTest my_test;
  48.     cout<
  49.     cout<
  50. }


阅读(499) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~