Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141048
  • 博文数量: 66
  • 博客积分: 1571
  • 博客等级: 上尉
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-24 22:55
文章分类

全部博文(66)

文章存档

2012年(66)

我的朋友

分类: C/C++

2012-09-06 22:36:09


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6.     cout<<"Enter the numbers"<<endl;
  7.     string str;
  8.     char ch[50];
  9.     int count[50];
  10.     getline(cin,str);
  11.     int i=0,j,n=1,t=0,m=0;
  12.     int len=str.length();
  13.     for(i=1,j=0;i<len;i++)
  14.     {
  15.         if(str[j]==str[i])
  16.         {
  17.          n++;
  18.          if(i==len-1) //如果没有边界控制,当到达边界时最后连续相等的会因为for循环结束而未存储
  19.             {
  20.                 ch[m]=str[len-1];
  21.                 count[m]=n;
  22.             }    
  23.         }
  24.         else{        
  25.             ch[t]=str[j];
  26.             count[t++]=n;
  27.             m++;
  28.             j=i;
  29.             n=1;
  30.             if(i==len-1)//如果没有边界控制,最后一个不连续相等的一位由于for循环结束而未存储
  31.             {
  32.                 ch[m]=str[len-1];
  33.                 count[m]=n;
  34.             }            
  35.         }
  36.     }
  37.     for(i=0;i<=m;i++)
  38.     {
  39.         cout<<ch[i]<<count[i];
  40.     }    
  41. }

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